Andy Longwill
Andy Longwill

Reputation: 634

joda time to Date inconsistent time zones

I have a load of dates that I'd like to store in a database running on a server using BST:

  1. 2015-09-23
  2. 2024-05-07
  3. 2024-03-13

However they are stored in the DB as:

  1. 2015-09-23 01:00:00
  2. 2024-05-07 01:00:00
  3. 2024-03-13 00:00:00 <-- I need this to be 01:00:00

The values are converted to Date prior to being stored in the DB. I noticed the following when debugging:

TimeZone timeZone = Calendar.getInstance().getTimeZone();
System.out.println(timeZone.getDisplayName(false, TimeZone.SHORT));

System.out.println(new SimpleDateFormat("zzz").format(new Date()));

DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM-dd").withZone(DateTimeZone.UTC);
System.out.println(formatter.parseDateTime("2015-09-23").toDate());
System.out.println(formatter.parseDateTime("2024-05-07").toDate());
System.out.println(formatter.parseDateTime("2024-03-13").toDate());

The first two dates are using BST and the last one is GMT. Is is possible to make them all use the same time zone?

GMT
BST
Wed Sep 23 01:00:00 BST 2015
Tue May 07 01:00:00 BST 2024
Wed Mar 13 00:00:00 GMT 2024

Upvotes: 2

Views: 677

Answers (2)

user7605325
user7605325

Reputation:

First of all, keep in mind that java.util.Date doesn't have a timezone (more details about it can be read here).

What happens is that Date.toString() method uses the system's default timezone to print its value (check the value of TimeZone.getDefault() in your JVM, it'll probably be Europe/London).

And in Europe/London timezone, the offset is equals to UTC in the winter (which is printed as GMT) and is +01:00 in the summer (which is printed as BST, aka British Summer Time). These different 3-letter names denotes the offset change, but it doesn't mean the dates "changed" their timezone.

Also consider that timezone is not only the offset or the name, but the set of all offset changes that occur in a region during history (when the changes occur, and the offsets before and after each change).

So, the dates doesn't have different timezones, because:

  1. In the same timezone there can be more than 1 offset. And some changes in the offset cause the change in the 3-letter name - although the use of these 3-letter names is widely used, they're ambiguous and not standard.
  2. java.util.Date doesn't have a timezone, so it can't change it.

If you want to save these objects in a DB, what you should care about is the timestamp (the number of milliseconds from 1970-01-01T00:00:00Z), which is preserved when converting to Date.

If you check the timestamp millis in the objects created, you'll see that it wasn't changed:

DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM-dd").withZone(DateTimeZone.UTC);
DateTime d1 = formatter.parseDateTime("2015-09-23");
DateTime d2 = formatter.parseDateTime("2024-05-07");
DateTime d3 = formatter.parseDateTime("2024-03-13");

// comparing timestamp millis between DateTime and java.util.Date
System.out.println(d1.getMillis() == d1.toDate().getTime());
System.out.println(d2.getMillis() == d2.toDate().getTime());
System.out.println(d3.getMillis() == d3.toDate().getTime());

All 3 cases above prints true, meaning that they represent the same instant in time (so the dates hasn't changed).

Actually, you can see that all DateTime objects were in UTC:

System.out.println(d1);
System.out.println(d2);
System.out.println(d3);

This prints:

2015-09-23T00:00:00.000Z
2024-05-07T00:00:00.000Z
2024-03-13T00:00:00.000Z

Conclusion:

  • you can save the Date objects without any problem, as their values are correct
  • if you want to display the dates to the user, you can use the DateTime objects (and use a DateTimeFormatter if you want a different format), because they don't use the default TimeZone in the toString() method.

Upvotes: 1

Elvis Yuan
Elvis Yuan

Reputation: 3

Try this: SimpleTimeZone UTCTimeZone = new SimpleTimeZone(0, "UTC"); TimeZone.setDefault(UTCTimeZone); All the date object will use UTC as default timezone for you backend code

Upvotes: 0

Related Questions