Question

I tried this snippet code and the output is very surprising. Why the output is different from the input? Did I make a mistake or is there a problem with SimpleDateFormat?

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd-HH.mm.ss.SSS");
try {
    Date date = sdf.parse("2013-04-18-19.01.33.080");
    System.out.println(sdf.format(date)); // output: 2012-04-18-19.01.33.80
} catch (ParseException e) {
    e.printStackTrace();
}

EDIT: In fact, I checked my code and I wrote yyyy-MM-dd-HH.mm.ss.S instead of yyyy-MM-dd-HH.mm.ss.SSS. It was a stupid mistake. Thanks all for your answers.

Was it helpful?

Solution

This works fine for me, If it doesn't work for you try setting Locale explicitly

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd-HH.mm.ss.SSS", Locale.ENGLISH);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top