fork download
  1. import java.time.LocalDateTime;
  2. import java.time.OffsetDateTime;
  3. import java.time.ZoneOffset;
  4. import java.time.format.DateTimeFormatter;
  5. import java.util.Locale;
  6.  
  7. public class Main {
  8. public static void main(String[] args) {
  9. String date = "9/13/2012";
  10. String time = "5:48pm";
  11.  
  12. DateTimeFormatter dtf = DateTimeFormatter.ofPattern("M/d/u h:ma", Locale.UK);
  13. LocalDateTime ldtSource = LocalDateTime.parse(date + " " + time, dtf);
  14.  
  15. OffsetDateTime odtSource = ldtSource.atOffset(ZoneOffset.UTC);
  16. OffsetDateTime odtTarget = odtSource.withOffsetSameInstant(ZoneOffset.of("+08:00"));
  17.  
  18. System.out.println(odtTarget);
  19.  
  20. // In a custom format
  21. System.out.println(odtTarget.format(dtf));
  22. }
  23. }
Success #stdin #stdout 0.14s 57000KB
stdin
Standard input is empty
stdout
2012-09-14T01:48+08:00
9/14/2012 1:48am