fork download
  1. import java.time.*;
  2. import java.time.format.DateTimeFormatter;
  3. import java.util.Locale;
  4.  
  5. public class Main {
  6. private static final DateTimeFormatter FORMATTER =
  7. DateTimeFormatter.ofPattern("dd/MM/uuuu", Locale.ENGLISH);
  8.  
  9. public static void main(String[] args) {
  10. // Test
  11. ZonedDateTime zdt = convertTimeZone("23/04/2013", "23:00",
  12. ZoneId.of("America/New_York"), // source time zone
  13. ZoneId.of("Europe/London")); // target time zone
  14. System.out.println(zdt);
  15.  
  16. // In custom format
  17. System.out.println(zdt.format(FORMATTER) + " " + zdt.toLocalTime());
  18. }
  19.  
  20. private static ZonedDateTime convertTimeZone(String strDate, String strTime, ZoneId sourceTz, ZoneId targetTz) {
  21.  
  22. LocalDate date = LocalDate.parse(strDate, FORMATTER);
  23. LocalTime time = LocalTime.parse(strTime);
  24. ZonedDateTime zdt = ZonedDateTime.of(date, time, sourceTz);
  25. return zdt.withZoneSameInstant(targetTz);
  26. }
  27. }
Success #stdin #stdout 0.17s 59612KB
stdin
Standard input is empty
stdout
2013-04-24T04:00+01:00[Europe/London]
24/04/2013 04:00