fork download
  1. import java.time.LocalDateTime;
  2. import java.time.format.DateTimeFormatter;
  3. import java.time.format.DateTimeFormatterBuilder;
  4. import java.util.Locale;
  5.  
  6. public class Main {
  7. public static void main(String[] args) {
  8. final String strDateTime = "24 Oct 2016 7:31 pm";
  9. DateTimeFormatter dtfWithDefaultLocale = null;
  10.  
  11. System.out.println("JVM's Locale: " + Locale.getDefault());
  12. // Using DateTimeFormatter with the default Locale
  13. dtfWithDefaultLocale = getDateTimeFormatterWithDefaultLocale();
  14. System.out.println("DateTimeFormatter's Locale: " + dtfWithDefaultLocale.getLocale());
  15. System.out.println(
  16. "Parsed with JVM's default locale: " + LocalDateTime.parse(strDateTime, dtfWithDefaultLocale));
  17.  
  18. // Setting the JVM's default locale to Locale.FRANCE
  19. Locale.setDefault(Locale.FRANCE);
  20.  
  21. // Using DateTimeFormatter with Locale.ENGLISH explicitly (recommended)
  22. DateTimeFormatter dtfWithEnglishLocale = getDateTimeFormatterWithEnglishLocale();
  23. System.out.println("JVM's Locale: " + Locale.getDefault());
  24. System.out.println("DateTimeFormatter's Locale: " + dtfWithEnglishLocale.getLocale());
  25. LocalDateTime zdt = LocalDateTime.parse(strDateTime, dtfWithEnglishLocale);
  26. System.out.println("Parsed with Locale.ENGLISH: " + zdt);
  27.  
  28.  
  29. System.out.println("JVM's Locale: " + Locale.getDefault());
  30. // Using DateTimeFormatter with the default Locale
  31. dtfWithDefaultLocale = getDateTimeFormatterWithDefaultLocale();
  32. System.out.println("DateTimeFormatter's Locale: " + dtfWithDefaultLocale.getLocale());
  33. System.out.println(
  34. "Parsed with JVM's default locale: " + LocalDateTime.parse(strDateTime, dtfWithDefaultLocale));
  35. }
  36.  
  37. static DateTimeFormatter getDateTimeFormatterWithDefaultLocale() {
  38. return new DateTimeFormatterBuilder()
  39. .parseCaseInsensitive()
  40. .appendPattern("d MMM uuuu h:m a")
  41. .toFormatter(); // Using default Locale
  42. }
  43.  
  44. static DateTimeFormatter getDateTimeFormatterWithEnglishLocale() {
  45. return new DateTimeFormatterBuilder()
  46. .parseCaseInsensitive()
  47. .appendPattern("d MMM uuuu h:m a")
  48. .toFormatter(Locale.ENGLISH); // Using Locale.ENGLISH
  49. }
  50. }
Runtime error #stdin #stdout #stderr 0.27s 41760KB
stdin
Standard input is empty
stdout
JVM's Locale: en_US
DateTimeFormatter's Locale: en_US
Parsed with JVM's default locale: 2016-10-24T19:31
JVM's Locale: fr_FR
DateTimeFormatter's Locale: en
Parsed with Locale.ENGLISH: 2016-10-24T19:31
JVM's Locale: fr_FR
DateTimeFormatter's Locale: fr_FR
stderr
Exception in thread "main" java.time.format.DateTimeParseException: Text '24 Oct 2016 7:31 pm' could not be parsed at index 3
	at java.base/java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:2049)
	at java.base/java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1951)
	at java.base/java.time.LocalDateTime.parse(LocalDateTime.java:492)
	at Main.main(Main.java:34)