fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6. import java.time.LocalDate;
  7. import java.time.LocalDateTime;
  8. import java.time.format.DateTimeFormatter;
  9. import java.time.temporal.TemporalAccessor;
  10. import java.util.Locale;
  11.  
  12. /* Name of the class has to be "Main" only if the class is public. */
  13. public class Main {
  14. public static void main(String[] args) {
  15. parseDate("09:10:05.584 AM EST Wed Nov 29 2023", TIME_FORMAT);
  16. parseDate("09:10:05.584 AM Wed Nov 29 2023", TIME_FORMAT);
  17. parseDate("09:10:05.584 EST Wed Nov 29 2023", TIME_FORMAT);
  18. parseDate("09:10:05.584 Wed Nov 29 2023", TIME_FORMAT);
  19. }
  20. public static final String TIME_FORMAT = "HH:mm:ss.SSS[ a][ z][zzz] E MMM dd yyyy";
  21.  
  22. public static String parseDate(String input, String format) {
  23. String retVal = "";
  24. DateTimeFormatter formatter = DateTimeFormatter.ofPattern(format, Locale.ENGLISH);
  25. TemporalAccessor ta = formatter.parseBest(input, LocalDateTime::from, LocalDate::from);
  26. retVal = ta.toString();
  27. System.out.println("Parsed date is: " + retVal);
  28. return retVal;
  29. }
  30. }
Success #stdin #stdout 0.41s 71024KB
stdin
Standard input is empty
stdout
Parsed date is: 2023-11-29T09:10:05.584
Parsed date is: 2023-11-29T09:10:05.584
Parsed date is: 2023-11-29T09:10:05.584
Parsed date is: 2023-11-29T09:10:05.584