I know I will always get the following date format from a server.
2017-10-16
To run simpleDateFormat.parse("2017-10-16")
in client device, and returns a date to represent year 2017 month October date sixteen
I was wondering, should I use
new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH);
or
new SimpleDateFormat("yyyy-MM-dd", Locale.US);
I had tested both, they work fine.
Here's the test code I'm using
public class JavaApplication23 {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
for (Locale locale : Locale.getAvailableLocales()) {
Locale.setDefault(locale);
// This breaks the thing!
//SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
// This is OK.
//SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH);
// This is OK too.
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.US);
Date date;
try {
date = simpleDateFormat.parse("2017-10-16");
if(
date.getYear() != 117 ||
date.getMonth() != 9 ||
date.getDate() != 16
) {
System.out.println(locale + " locale having problem to parse " + date);
}
} catch (ParseException ex) {
Logger.getLogger(JavaApplication23.class.getName()).log(Level.SEVERE, null, ex);
ex.printStackTrace();
}
}
}
}
Seem like using Locale.ENGLISH
or Locale.US
both OK.
However, I afraid I might miss out some edge case.
May I know, is Locale.ENGLISH
or Locale.US
more suitable to constrct locale independent SimpleDateFormat
for string parsing?