Reputation: 33
I'm using java 7 and I'm trying to parse a date from a flat file.
I this file's dates have the following format "30-Mar-2012 14:05:02". I can't change this, it depends on another software.
According to the javadoc this format : "dd-MMM-yyyy HH:mm:ss" is the one to use.
But for unknown reason this is not working.
Here is a little program which reproduce the issue.
package javaapplication2;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.logging.Level;
import java.util.logging.Logger;
public class JavaApplication2 {
public static void main(String[] args) {
Date d = null;
try {
SimpleDateFormat df;
df = new SimpleDateFormat("dd-MMM-yyyy HH:mm:ss", Locale.FRANCE);
d = df.parse("30-Mar-2012 14:05:02");
} catch (ParseException ex) {
System.err.println(ex.getMessage());
}
if (d != null) {
System.out.println(d.toString());
}
}
}
The problem does not happen when i put an "s" after "Mar" in the original date ("Mars" is the full name of March in French, this also work with "March" and Locale.US in the format).
Am-I doing something wrong ? Is it really possible to do this that way ? I there an undocumented issue in java about this ?
Upvotes: 3
Views: 1219
Reputation: 14413
Test your code
Calendar calendar = Calendar.getInstance();
calendar.set(2012, 2, 30);
try {
SimpleDateFormat df;
df = new SimpleDateFormat("dd-MMM-yyyy HH:mm:ss", Locale.FRANCE);
String s=df.format(calendar.getTime());
System.out.println(s);
}
result prints : 30-mars-2012 23:17:46
So it's expecting mars
instead of Mar
'Mar' is Locale.US
Upvotes: 1
Reputation: 135992
Explanation is here
DateFormatSymbols dfs = new DateFormatSymbols(Locale.FRENCH);
for (String s : dfs.getShortMonths()) {
System.out.print(s + " ");
}
output
janv. f?vr. mars avr. mai juin juil. ao?t sept. oct. nov. d?c.
as you can see shortened form for mars = mars.
Upvotes: 2