1

I have one date let say

Date date = new Date(); //Thu Mar 31 04:34:37 AKDT 2016

I want to convert it to another date:

Thu Mar 31 00:00:00 UTC 2016

Means I did two changes:

  1. reset time to 00
  2. rest timezone to UTC, but date should be same i.e 31 .

I wrote this code:

Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
        calendar.setTime(date);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND,0);
        calendar.clear(Calendar.MILLISECOND);
        return calendar.getTime();

but it is giving Wed Mar 30 10:30:00 AKDT 2016 but I want Wed Mar 31 00:00:00 UTC 2016.

How would I achieve this?

assylias
  • 310,138
  • 72
  • 642
  • 762
Abhishek Singh
  • 1,087
  • 1
  • 19
  • 39

2 Answers2

4

You should specify timezone and proper format in your formatter, and output according to that format:

Date date = new Date();
TimeZone utc = TimeZone.getTimeZone("UTC");
Calendar calendar = Calendar.getInstance(utc);
calendar.setTime(date);

// ... erase time as you do now

// here is a format you need (taken from `Date.toString()`)
DateFormat df = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy");
df.setTimeZone(utc);
System.out.println(df.format(calendar.getTime()));  // Thu Mar 31 00:00:00 UTC 2016

When you try to print a Date as date.toString(), you will always get result in your default timezone, because Date itself is just a moment in time and it doesn't keep any information about timezone.

If you just need to adjust current datetime to midnight in UTC, your code is correct. 10:30:00 AKDT should not confuse you -- it means only that when it is midnight in UTC, in your timezone it is 10:30.

Alex Salauyou
  • 13,786
  • 4
  • 41
  • 67
  • Above code is correct, but this thing is in String format and I want it in date, when I convert it into date, it's format changes. – Abhishek Singh Mar 31 '16 at 13:46
  • can u please help me, how can I remove timezone from my date?. and result should be in Date format not String. – Abhishek Singh Mar 31 '16 at 13:47
  • @AbhishekRajawat `Date` itself *does not* keep timezone. You see timezone only when you perform `date.toString()`--so timezone is a part of *representation of date*, not *date* itself. So question about replacing/removing timezone from `Date` is senseless. – Alex Salauyou Mar 31 '16 at 13:50
  • the problem is, I have to save this date in database, and column in DB has Date type. so I can't save it in String. – Abhishek Singh Mar 31 '16 at 13:53
  • @AbhishekRajawat no problem, save it as a `Date`, not string. When you create a canendar which timezone is UTC, then erase time values, resulting `Date` is a midnight in UTC (but may be not midnight in another time zones) – Alex Salauyou Mar 31 '16 at 13:54
  • @AbhishekRajawat when there is a midnight in UTC, in your timezone it is 10:30, that's why `date.toString()` prints 10:30. – Alex Salauyou Mar 31 '16 at 14:02
2

This looks like a problem with printing/string conversion.

Internally Calendar and Date will store the the date-time as milliseconds since the epoch (January 1, 1970, 00:00:00 GMT). When converting the Date to a string the output will always be converted to your timezone.

Your date Thu Mar 31 04:34:37 AKDT 2016 is stored as (1459427677000) and would print Thu Mar 31 14:34:37 CEST 2016 for a JVM running in CEST timezone.

Your code looks correct to to me, the returned Date's getTime() should be 1459382400000. In your case, you can do a quick check using date.getGMTString() which should print Mar 31 00:00:00 UTC 2016.

nyname00
  • 2,311
  • 1
  • 21
  • 24