Reputation: 2211
Consider this program that format the current date, and try to parse it again. It succeeds in French, but fails in English and I don't understand why.
import java.util.Locale;
import java.text.DateFormat;
import java.time.Instant;
import java.util.Date;
import java.text.SimpleDateFormat;
public class HelloWorld{
public static void main(String []args){
try{
SimpleDateFormat formatter = new SimpleDateFormat("dd MMM yyyy HH:mm:ss", Locale.ENGLISH);
DateFormat.getDateTimeInstance(DateFormat.DEFAULT,DateFormat.DEFAULT, Locale.ENGLISH).parse(formatter.format(new Date()));
System.out.println("English - success");
}catch(Exception ex){
System.out.println(ex);
}
try{
SimpleDateFormat formatter = new SimpleDateFormat("dd MMM yyyy HH:mm:ss", Locale.FRENCH);
DateFormat.getDateTimeInstance(DateFormat.DEFAULT,DateFormat.DEFAULT, Locale.FRENCH).parse(formatter.format(new Date()));
System.out.println("French - success");
}catch(Exception ex){
System.out.println(ex);
}
System.out.println(Locale.getDefault());
}
}
Output:
java.text.ParseException: Unparseable date: "11 Feb 2015 11:09:26"
French -success
en_US
Please look at http://docs.oracle.com/javase/7/docs/api/java/text/DateFormat.html#parse%28java.lang.String,%20java.text.ParsePosition%29 before telling me that I should use a pattern or anything else. This method is meant to parse a String
without a pattern.
Upvotes: 0
Views: 666
Reputation: 3510
I suspect your input format string is wrong.
As per documentation Jun 30, 2009 7:03:47 AM
would be a valid format for en_US on Default
settings.
You could always check if your format is right by formatting a given Date
first.
For example
System.out.println(DateFormat.getDateTimeInstance(DateFormat.DEFAULT,DateFormat.DEFAULT, Locale.ENGLISH).format(new Date()));
gives Feb 11, 2015 12:34:48 PM
, which doesn't fit 11 Feb 2015 11:09:26
.
This should be your correct formatted string for en_US parsing: Feb 11, 2015 11:09:26 AM
. Remember, this is AM / PM 12 hour time format, which can be annoying.
Upvotes: 3
Reputation: 40378
The default format for English is like this:
SimpleDateFormat formatter = new SimpleDateFormat("MMM dd, yyyy HH:mm:ss a", Locale.ENGLISH);
Upvotes: 0
Reputation: 5798
are you sure that dd MMM yyyy HH:mm:ss is a correct english format dont they use month before days like US format ?
Upvotes: 0