fork download
  1. import java.time.Instant;
  2. import java.time.LocalDateTime;
  3. import java.time.ZoneOffset;
  4. import java.time.format.DateTimeFormatter;
  5. import java.time.format.DateTimeFormatterBuilder;
  6. import java.util.Locale;
  7.  
  8. public class Main {
  9. public static void main(String[] args) {
  10. // Given date-time string
  11. String dateTimeString = "9/25/2020, 12:46:00 PM";
  12.  
  13. DateTimeFormatter dtf = new DateTimeFormatterBuilder()
  14. .parseCaseInsensitive()
  15. .appendPattern("M/d/u, h:m:s a")
  16. .toFormatter(Locale.ENGLISH);
  17.  
  18. // Convert the given date-time string to LocalDateTime
  19. LocalDateTime ldt = LocalDateTime.parse(dateTimeString, dtf);
  20. System.out.println(ldt);
  21.  
  22. // Convert to LocalDateTime Instant if required
  23. Instant instant = ldt.toInstant(ZoneOffset.UTC);
  24. System.out.println(instant);
  25. }
  26. }
Success #stdin #stdout 0.13s 54272KB
stdin
Standard input is empty
stdout
2020-09-25T12:46
2020-09-25T12:46:00Z