Skip to content Skip to sidebar Skip to footer

Milliseconds To Date In Gmt In Java

I need to covert milliseconds to GMT date (in Android app), example: 1372916493000 When I convert it by this code: Calendar cal = Calendar.getInstance(); cal.setTimeZone(TimeZone.g

Solution 1:

If result which looks incorrect means System.out.println(date) then it's no surprise, because Date.toString converts date into string representation in local timezone. To see result in GMT you can use this

SimpleDateFormatdf=newSimpleDateFormat("hh:ss MM/dd/yyyy");
df.setTimeZone(TimeZone.getTimeZone("GMT"));
Stringresult= df.format(millis);

Solution 2:

It seemed you were messed up with your home timezone and the UTC timezone during the conversion.

Let's assume you are in London (currently London has 1 hour ahead of GMT) and the milliseconds is the time in your home timezone (in this case, London).

Then, you probably should:

Calendarcal= Calendar.getInstance();
// Via this, you're setting the timezone for the time you're planning to do the conversion
cal.setTimeZone(TimeZone.getTimeZone("Europe/London"));
cal.setTimeInMillis(1372916493000L);
// The date is in your home timezone (London, in this case)Datedate= cal.getTime();


TimeZonedestTz= TimeZone.getTimeZone("GMT");
// Best practice is to set Locale in case of messing up the date displaySimpleDateFormatdestFormat=newSimpleDateFormat("HH:mm MM/dd/yyyy", Locale.US);
destFormat.setTimeZone(destTz);
// Then we do the conversion to convert the date you provided in milliseconds to the GMT timezoneStringconvertResult= destFormat.parse(date);

Please let me know if I correctly get your point?

Cheers

Solution 3:

tl;dr

Instant.ofEpochMilli( 1_372_916_493_000L )         // Moment on timeline in UTC.

2013-07-04T05:41:33Z

…and…

Instant.ofEpochMilli( 1_372_916_493_000L )          // Moment on timeline in UTC..atZone( ZoneId.of( "Europe/Berlin" ) )  // Same moment, different wall-clock time, as used by people in this region of Germany.

2013-07-04T07:41:33+02:00[Europe/Berlin]

Details

You are using troublesome old date-time classes now supplanted by the java.time classes.

java.time

If you have a count of milliseconds since the epoch reference date of first moment of 1970 in UTC, 1970-01-01T00:00Z, then parse as an Instant. The Instant class represents a moment on the timeline in UTC with a resolution of nanoseconds (up to nine (9) digits of a decimal fraction).

Instantinstant= Instant.ofEpochMilli( 1_372_916_493_000L ) ;

instant.toString(): 2013-07-04T05:41:33Z

To see that same simultaneous moment through the lens of a particular region’s wall-clock time, apply a time zone (ZoneId) to get a ZonedDateTime.

ZoneIdz= ZoneId.of( "Europe/Berlin" ) ; 
ZonedDateTimezdt= instant.atZone( z ) ;

zdt.toString(): 2013-07-04T07:41:33+02:00[Europe/Berlin]


About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

Where to obtain the java.time classes?

Solution 4:

try this

publicclassTest{
    publicstaticvoidmain(String[] args) throws IOException {
        Test test=newTest();
        Date fromDate = Calendar.getInstance().getTime();
        System.out.println("UTC Time - "+fromDate);
        System.out.println("GMT Time - "+test.cvtToGmt(fromDate));
    }
    privateDatecvtToGmt(Date date )
        {
           TimeZone tz = TimeZone.getDefault();
           Date ret = newDate( date.getTime() - tz.getRawOffset() );

           // if we are now in DST, back off by the delta.  Note that we are checking the GMT date, this is the KEY.if ( tz.inDaylightTime( ret ))
           {
              Date dstDate = newDate( ret.getTime() - tz.getDSTSavings() );

              // check to make sure we have not crossed back into standard time// this happens when we are on the cusp of DST (7pm the day before the change for PDT)if ( tz.inDaylightTime( dstDate ))
              {
                 ret = dstDate;
              }
           }

           return ret;
        }
}

Test Result : UTC Time - Tue May 15 16:24:14 IST 2012 GMT Time - Tue May 15 10:54:14 IST 2012

Solution 5:

Date date = cal.getTime();

returns date created via

public final DategetTime() {
    returnnewDate(getTimeInMillis());
}

where getTimeInMillis() returns milliseconds without any TimeZone.

I would suggest looking here for how to do what you want how-to-handle-calendar-timezones-using-java

Post a Comment for "Milliseconds To Date In Gmt In Java"