-1

I am working with converting Date Strings to Epoch times then back again, but it seems like when I convert to Epoch the month is ignored when I parse the date String

String timeString = "15 Aug 2005 13:07:58.035";
Date dateTime = null;
SimpleDateFormat sdf = new SimpleDateFormat("DD MMM yyyy hh:mm:ss", Locale.US);
try {
    dateTime = sdf.parse(timeString);
} catch (ParseException e) {
    e.printStackTrace();
}
long convert = dateTime.getTime();
System.out.println(str);
System.out.println(convert);
Date epochToString = new Date(convert);
System.out.println(epochToString.toString());

Here is the output

15 Aug 2005 13:07:58.035
1105812478000
Sat Jan 15 13:07:58 EST 2005

When I calculated the epoch time by hand the date was indeed Jan 15 2005, so for some reason the MMM value is being ignored in my format.

Edit: I should also note that I changed the format token to MM and tried a numerical representation in the string instead, but had the same exact results.

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
pianoisland
  • 163
  • 1
  • 1
  • 10

1 Answers1

1

This is because DD means day in year. You want dd for day in month. See SimpleDateFormat Javadoc

I suggest you use HH for 24 hour time.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130