Florian Schaetz
Florian Schaetz

Reputation: 10662

Convert Date and Time with different timezones to timestamp

got a little problem in Java: I've got two strings, representing a date (for example "2019-11-10") and a time (for example "01:23:45.123"). Converting those into a UTC timestamp is easy enough, but there is one problem:

The date is given in a local timezone, for example "America/New_York", while the time is given in UTC, so the following information...

date = "2019-11-10", time="01:23:45.123" (assuming a local timezone of "America/New_York" for the date part)

...would actually be the UTC time "2019-11-11 01:23:45.123" (since only this would be a New York date of "2019-11-10").

Does the Java Time API or joda time offer any convenient method to do this conversion while keeping also details like DST, etc. in mind? Is there a (relatively) simple way of doing this at all?

Edit:

Perhaps this was a bit unclear. Let me try to explain it from the other side:

Assuming I have a DateTime x (in UTC) of "2019-11-11 01:23:45.123".

Then I can get the date part y (as seen in "America/New_York") by simply converting x into the appropriate timezone and formatting is accordingly -> "2019-11-10".

I can also get the time part z (as seen in UTC) by simply formatting is accordingly - "01:23:45.123".

Now, assuming I've got y and z - How do I get x?

Edit 2:

I just realized, this will never work perfectly. Reason:

UTC "2019-11-04 04:01:00.000" -> US/NY : "2019-11-03 23:01"

UTC "2019-11-03 04:01:00.000" -> US/NY : "2019-11-03 00:01"

So, if I only got the information "date in US/NY" is "2019-11-03" plus "time in UTC" is "04:01:00.000" there are actually two possible solutions and not only one.

Upvotes: 0

Views: 374

Answers (2)

JodaStephen
JodaStephen

Reputation: 63465

You have to adjust the date to be in the New York time zone which can be done by choosing an arbitrary time. Then convert the time to New York by choosing an arbitrary date. The two can then be merged as they have the same zone. And convert back to UTC

    LocalDate date = LocalDate.parse("2019-11-10");
    LocalTime time = LocalTime.parse("01:23:45.123");
    ZoneId zone = ZoneId.of("America/New_York");

    ZonedDateTime dateInZone = date.atStartOfDay(zone);
    ZonedDateTime timeInZone = time.atOffset(ZoneOffset.UTC).atDate(date).atZoneSameInstant(zone);
    ZonedDateTime resultNY = dateInZone.with(timeInZone.toLocalTime());
    ZonedDateTime resultUTC = resultNY.withZoneSameInstant(ZoneOffset.UTC)
    System.out.println(resultUTC);
    // 2019-11-11T01:23:45.123Z

Upvotes: 2

Basil Bourque
Basil Bourque

Reputation: 340070

A date cannot have a time zone. (Think about it.)

Perhaps what you mean is the first moment of the day for that date as seen in UTC.

LocalDate ld = LocalDate.parse( "2019-11-10" ) ;
ZonedDateTime zdt = ld.atStartOfDay( ZoneOffset.UTC ) ; 

Adjust that moment to be seen in a time zone.

ZoneId z = ZoneId.of( "America/New_York" ) ;
LocalTime lt = LocalTime.parse( "01:23:45.123" ) ;
ZonedDateTime zdt2 = zdt.with( lt ) ;

Upvotes: 1

Related Questions