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'm receiving a date as a String like this :2015-07-22.06.05.56.344. I wrote a parsing code like this

 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd.hh.MM.ss.ms");
    try {
        Date date = sdf.parse("2015-07-22.06.05.56.344");
        System.out.println(date);
    } catch (ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

And I got the output like this:

Fri May 22 06:03:44 IST 2015

Why is it reading it wrongly? Is it an issue with my code or java cannot recognize this date format?

See Question&Answers more detail:os

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

1 Answer

  • Your MM/mm are around the wrong way, mm is for "Minute in hour" and MM is for "Month in year"
  • SS is for "Millisecond" (or ms, which means nothing)
  • I'd also recommend using HH instead of hh as HH is for "Hour in day (0-23)"

So, using...

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd.HH.mm.ss.SS");

It outputs Wed Jul 22 06:05:56 EST 2015 for me


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