Convert String at time, change timezone and then back to String

I have String

one containing the time in the format:08:00:00

This time from US Eastern Time and I want to convert it to London time zone and end up from String

that time.

I converted String

in time using

Time.valueOf(t);

      

However, after doing this, I cannot change the timezone.

+3


source to share


2 answers


you can preempt time with using ZoneSameInstant



LocalTime myLocalTime = LocalTime.parse("08:00:00", DateTimeFormatter.ofPattern("HH:mm:ss"));
LocalTime londonTime = LocalDateTime.of(LocalDate.now(), myLocalTime).atZone(ZoneId.of("America/New_York"))
        .withZoneSameInstant(ZoneId.of("Europe/London")).toLocalTime();

System.out.println(myLocalTime);
System.out.println(londonTime);

      

+3


source


There are many details in this question.

The class Time

sets the date (day, month, and year) to 1 January st 1970. But to convert from EST to London local time, you must consider Daylight Saving Time.
The difference in hours is not always the same; it can change depending on the date - given this year (2017): from January 1st st to March 11th th the difference will be 5 hours, then from March 12th th to March 25th th the difference is 4 hours, then it goes back to 5 hours, then to October 29 th is the 4 o'clock and 5 November th over 5 hours to the end of the year.

This is because DST starts and ends in both time zones and different dates. And these dates change every year as well, so you need to know the date you are working from in order to make the correct conversion.

Another thing is that the new Java 8 API uses IANA time zone names (always in the format Region/City

like America/Sao_Paulo

or Europe/Berlin

). Avoid using three-letter abbreviations (for example, CST

or EST

) as they are ambiguous and not standard .

If you're using Java <= 7 , you can use the ThreeTen Backport , a great backport for Java 8's new date and time classes. And for Android there is ThreeTenABP (more on how to use it here ).

Below is the code below. The only difference is the package names (in Java 8 - java.time

, and in ThreeTen Backport (or Android ThreeTenABP) - org.threeten.bp

), but the class and method names are the same.

In the example below, I am using America/New_York

- one of the many time zones in which it is usedEST

(there are more than 30 time zones that have used or have used it). You can call ZoneId.getAvailableZoneIds()

to check all time zones and choose the best one for your case.

The code is very similar to @ ΦXocę 웃 Pepeúpa ツ's answer , well, because it's simple and there isn't much to change. I just wanted to add the information above.



// timezones for US and UK
ZoneId us = ZoneId.of("America/New_York");
ZoneId uk = ZoneId.of("Europe/London");
// parse the time string
LocalTime localTimeUS = LocalTime.parse("08:00:00");
// the reference date (now is the current date)
LocalDate now = LocalDate.now(); // or LocalDate.of(2017, 5, 20) or any date you want
// the date and time in US timezone
ZonedDateTime usDateTime = ZonedDateTime.of(now, localTimeUS, us);
// converting to UK timezone
ZonedDateTime ukDateTime = usDateTime.withZoneSameInstant(uk);
// get UK local time
LocalTime localTimeUK = ukDateTime.toLocalTime();
System.out.println(localTimeUK);

      

The output will be 13:00

(result localTimeUK.toString()

) because it toString()

omits seconds if the value is zero.

If you want to always output seconds, you can use DateTimeFormatter

:

DateTimeFormatter fmt = DateTimeFormatter.ofPattern("HH:mm:ss");
String time = fmt.format(localTimeUK);

      

In this case, the string Time

will be 13:00:00

.


LocalDate.now()

returns the current date using your system's default timezone. If you want the current date was in a certain area, you might cause LocalDate.now(us)

(or any zone that you want, or even explicitly use the default LocalDate.now(ZoneId.systemDefault())

)

+2


source







All Articles