import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.util.Locale;
public class Main {
public static void main
(String[] args
) { final String strDateTime
= "24 Oct 2016 7:31 pm"; DateTimeFormatter dtfWithDefaultLocale = null;
// Using DateTimeFormatter with the default Locale
dtfWithDefaultLocale = getDateTimeFormatterWithDefaultLocale();
System.
out.
println("DateTimeFormatter's Locale: " + dtfWithDefaultLocale.
getLocale()); "Parsed with JVM's default locale: " + LocalDateTime.parse(strDateTime, dtfWithDefaultLocale));
// Setting the JVM's default locale to Locale.FRANCE
// Using DateTimeFormatter with Locale.ENGLISH explicitly (recommended)
DateTimeFormatter dtfWithEnglishLocale = getDateTimeFormatterWithEnglishLocale();
System.
out.
println("DateTimeFormatter's Locale: " + dtfWithEnglishLocale.
getLocale()); LocalDateTime zdt = LocalDateTime.parse(strDateTime, dtfWithEnglishLocale);
System.
out.
println("Parsed with Locale.ENGLISH: " + zdt
);
// Using DateTimeFormatter with the default Locale
dtfWithDefaultLocale = getDateTimeFormatterWithDefaultLocale();
System.
out.
println("DateTimeFormatter's Locale: " + dtfWithDefaultLocale.
getLocale()); "Parsed with JVM's default locale: " + LocalDateTime.parse(strDateTime, dtfWithDefaultLocale));
}
static DateTimeFormatter getDateTimeFormatterWithDefaultLocale() {
return new DateTimeFormatterBuilder()
.parseCaseInsensitive()
.appendPattern("d MMM uuuu h:m a")
.toFormatter(); // Using default Locale
}
static DateTimeFormatter getDateTimeFormatterWithEnglishLocale() {
return new DateTimeFormatterBuilder()
.parseCaseInsensitive()
.appendPattern("d MMM uuuu h:m a")
.
toFormatter(Locale.
ENGLISH); // Using Locale.ENGLISH }
}