Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I am using Joda to parse dates and have a format where leading zeros are not used, e.g.:

 Mon Nov 20 14:40:36 2006
 Mon Nov  6 14:40:36 2006

Note that the dayOfMonth field is left-padded with a blank.

Currently I seem to have to use two different formats and reparse if one fails

"EEE MMM dd HH:mm:ss yyyy"
"EEE MMM  d HH:mm:ss yyyy"

Is there a single format (or an API switch) which deals with both cases? (is the answer the same for SimpleDateFormat - which I don't use?)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
146 views
Welcome To Ask or Share your Answers For Others

1 Answer

I have just created a quick program to check this -

SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM d HH:mm:ss yyyy");

try {
    String source1 = "Mon Nov 20 14:40:36 2006";
    Date d1 = sdf.parse(source1);
    String source2 = "Mon Nov  6 14:40:36 2006";
    Date d2 = sdf.parse(source2);

    String res1 = sdf.format(d1);
    String res2 = sdf.format(d2);

    System.out.println(source1 +"="+ res1);
    System.out.println(source2 +"="+ res2);
} catch (ParseException e) {
    e.printStackTrace();
}

The output from this is -

Mon Nov 20 14:40:36 2006=Mon Nov 20 14:40:36 2006
Mon Nov  6 14:40:36 2006=Mon Nov 6 14:40:36 2006

So, even though source2 has the extra space, it is still parsed by

EEE MMM d HH:mm:ss yyyy

Hope that helps


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...