java.time
The question and existing answers use SimpleDateFormat
which was the correct thing to do in 2012. In Mar 2014, the java.util
date-time API and their formatting API, SimpleDateFormat
were supplanted by the modern Date-Time API. Since then, it is strongly recommended to stop using the legacy date-time API.
Solution using the modern date-time API: As you can learn from the documentation, the symbol Y
is used for week-based-year whereas we need y
(year-of-era) in this case. However, I prefer u
to y
.
Note that your date-time string has just date and time units (no time-zone or time-zone offset etc.). The java.time
API provides you with LocalDateTime
to represent such an object.
In case, you need to obtain a date-time object with time-zone or one representing just a moment in time, java.time
provides you with specific types. You can check overview of java.time
types here.
With Java 8, java.util
date-time API was also upgraded to make it easy to switch to java.time
API e.g. if you need java.util.Date
instance from a an Instant
, you can use Date#from
.
Demo:
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.util.Date;
import java.util.Locale;
class Main {
public static void main(String[] args) {
String strDateTime = "1992-03-11 12:00:12.123";
DateTimeFormatter parser = DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss.SSS", Locale.ENGLISH);
// An alternative parser
DateTimeFormatter ldtParser = new DateTimeFormatterBuilder().append(DateTimeFormatter.ISO_LOCAL_DATE)
.appendLiteral(' ').append(DateTimeFormatter.ISO_LOCAL_TIME).toFormatter(Locale.ENGLISH);
LocalDateTime ldt = LocalDateTime.parse(strDateTime, parser);
System.out.println(ldt);
// Parsing using the alternative parser
ldt = LocalDateTime.parse(strDateTime, ldtParser);
System.out.println(ldt);
// Converting LocalDateTime to a ZonedDateTime
// Replace ZoneId.systemDefault() with applicable ZoneId e.g.
// ZoneId.of("America/New_York")
ZoneId zoneId = ZoneId.systemDefault();
ZonedDateTime zdt = ldt.atZone(zoneId);
System.out.println(zdt);
// Alternatively,
zdt = ZonedDateTime.of(ldt, zoneId);
System.out.println(zdt);
// Obtaining an Instant
Instant instant = zdt.toInstant();
System.out.println(instant);
// In case you need an instance of java.util.Date
Date date = Date.from(instant);
}
}
Output in my time-zone, Europe/London:
1992-03-11T12:00:12.123
1992-03-11T12:00:12.123
1992-03-11T12:00:12.123Z[Europe/London]
1992-03-11T12:00:12.123Z[Europe/London]
1992-03-11T12:00:12.123Z
ONLINE DEMO
Learn more about the modern Date-Time API from Trail: Date Time.
Note: Check this answer and this answer to learn how to use java.time
API with JDBC.