While working with a task involving string array of timestamps, I took reference from this tutorial to convert a String into a Date object. The conversion is going wrong in the month part of the date. I am using Java 11.
FYR Code :
import java.text.SimpleDateFormat;
import java.util.Date;
public class TimeStampConv {
public static void main(String[] args) {
String timeStamp = "02-01-2014 10:02:01:001"; // Date is 2nd January of the year 2014
try {
Date d = new SimpleDateFormat("dd-MM-yyyy HH:MM:SS:sss").parse(timeStamp);
System.out.println(d.toString()); // prints Sun Feb 02 10:00:01 IST 2014
}
catch(Exception e) {
e.printStackTrace();
}
}
}
CodePudding user response:
Your pattern is wrong, use small m for minutes:
new SimpleDateFormat("dd-MM-yyyy HH:mm:ss:SSS")
Link to docs: SimpleDateFormat
CodePudding user response:
java.time
I recommend that you use java.time, the modern Java date and time API, for your date and time work.
Use this formatter:
private static final DateTimeFormatter FORMATTER
= DateTimeFormatter.ofPattern("dd-MM-uuuu HH:mm:ss:SSS", Locale.ROOT);
Parse like this:
String timeStamp = "02-01-2014 10:02:01:001"; // Date is 2nd January of the year 2014
LocalDateTime ldt = LocalDateTime.parse(timeStamp, FORMATTER);
System.out.println(ldt);
Output is:
2014-01-02T10:02:01.001
What went wrong in your code?
Format pattern letters are case sensitive. No matter if using the outdated and troublesome SimpleDateFormat or the modern DateTimeFormatter you need:
- lower case
mmfor minute; upper caseMMis understood as month, which caused your minute of02to be understood as February; - lower case
ssfor second; - upper case
SSSfor fraction of second/millisecond; withSimpleDateFormatupper caseSmeant milliseocnd, withDateTimeFormatterit means fraction of second.
Since your second and millisecond were both 1, accidentally swapping them didn’t change the result in your particular cases. In most other cases it would make a difference of up to 999 seconds or some quarter of an hour.
Links
- Related questions:
- SimpleDateFormat returns wrong time from Firestore Timestamp - why? about trying to use
HH:MM:SSfor hours, minutes and seconds, though for formatting rather than parsing. - Java - SimpleDateFormat formatter to return epoch time with milliseconds [duplicate] about trying to use
sssfor milliseoncds.
- SimpleDateFormat returns wrong time from Firestore Timestamp - why? about trying to use
- Oracle tutorial: Date Time explaining how to use java.time.
- Java Specification Request (JSR) 310, where
java.timewas first described. - ThreeTen Backport project, the backport of
java.timeto Java 6 and 7 (ThreeTen for JSR-310). - Java 8 APIs available through desugaring
- ThreeTenABP, Android edition of ThreeTen Backport
- Question: How to use ThreeTenABP in Android Project, with a very thorough explanation.
CodePudding user response:
Replace your SimpleDateFormat pattern with this:
"dd-MM-yyyy HH:mm:ss:SSS"
It seems like it gets a bit confused because you used MM for both month and minutes. Docs here.
