0

I have a Date, for example 2000-01-01T10:00:00Z . This stands for openinghours of a shop, so this means the shop opens at 10 o'clock. The other information is useless, it is a random date. I only need to represent this as a string so: 10:00. For this conversion i used a simple method:

public String dateToString(Date date){
    SimpleDateFormat ft =  new SimpleDateFormat ("HH:mm");
    String time= ft.format(date);
return time;
}

I thought this should work, the capital HH for 24 hour representation of the hours. But when i run this code the return value is 11:00 ! Why is this, and how to prevent it? Does the format function take a look at my time zone and is this set wrong in the Date (i think my phone settings are mgt+1, cause i live in Holland)? And how to ignore this?

Jasper
  • 2,389
  • 4
  • 25
  • 40
  • 1
    `Z` means GMT+0, so yep wrong timezone. – Voo Jul 28 '12 at 20:45
  • I had somehow the same problem in Iran. When I disable daylight saving on my OS (by using a TimeZone which does not support daylight saving) everything becomes right! – Amir Pashazadeh Jul 28 '12 at 20:56
  • @AmirPashazadeh Changing the default time zone on your host OS is not the best way to go about this. Instead (a) use the java.time classes, and (b) always specify the optional time zone arguments in the various methods on date-time classes. – Basil Bourque Sep 26 '16 at 21:33

2 Answers2

1

oke, found it! Android converses a Date to the timezone of the device. I had to overrule that:

This works:

public String dateToString(Date date){
    SimpleDateFormat ft =  new SimpleDateFormat ("HH:mm");
    ft.setTimeZone(TimeZone.getTimeZone("UTC"));
    String time= ft.format(date);
return time;
}
Jasper
  • 2,389
  • 4
  • 25
  • 40
0

The value 2000-01-01T10:00:00Z is not not at all “useless”. This describes an exact moment on the timeline, with a date, a time-of-day, and an offset-from-UTC which is UTC itself in this case.

ISO 8601

That string format is defined by the ISO 8601 standard. The Z on the end is short for Zulu and means UTC.

Instant

You can parse this string directly as a Instant. The Instant class represents a moment on the timeline in UTC with a resolution of nanoseconds. That means up to nine (9) digits of a decimal fraction.

Instant instant = Instant.parse( "2000-01-01T10:00:00Z" );

The Instant class is only the basic building-block class in Java. For more flexibility we should either:

  • Assign the instant an offset-from-UTC (ZoneOffset) to get an OffsetDateTime.
  • Assign the instant a time zone (ZoneId) to get a ZonedDateTime.

What is the difference? A time zone is an offset plus a set of rules for handling anomalies such as Daylight Saving Time (DST).

OffsetDateTime

So if you truly wanted the time-of-day for this moment as seen in UTC, then assign an offset of UTC (ZoneOffset.UTC) to get an OffsetDateTime, and then extract a LocalTime object. LocalTime represents a time-of-day without a date and without a time zone.

OffsetDateTime odt = instant.atOffset( ZoneOffset.UTC );
LocalTime lt = odt.toLocalTime();  // 10:00:00

ZonedDateTime

If you want to see the same moment through the lens of another time zone, to see that particular region’s wall-clock time, specify a ZoneId. For example, that same moment in Québec would be 5 hours behind UTC.

ZoneId z = ZoneId.of( "America/Montreal" );
ZonedDateTime zdt = instant.atZone( z );  // 2000-01-01T05:00:00-05:00[America/Montreal]
LocalTime lt = zdt.toLocalTime();  // 05:00:00

About java.time

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

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

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

Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport and further adapted to Android in ThreeTenABP (see How to use…).

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time.

Community
  • 1
  • 1
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154