tl;dr
After fixing your faulty input text, this value can be parsed using the modern java.time classes.
ZonedDateTime.parse(
"Mon Jan 01 00:00:00 CET 2001" // Changed incorrect "Sat" to correct "Mon".
,
DateTimeFormatter.ofPattern(
"EEE MMM dd HH:mm:ss z uuuu"
,
Locale.US // Specify a `Locale` to determine human language and cultural norms used in translation.
) // Returns a `DateTimeFormatter` object to be passed as second argument to `parse` method.
) // Returns a `ZonedDateTime` object.
.toString() // Generates a `String` using standard ISO 8601 format extended by appending zone name in square brackets.
2001-01-01T00:00+01:00[Europe/Paris]
Wrong input
Your example input string "Sat Jan 01 00:00:00 CET 2001"
is incorrect. January 1, 2001 was a Monday, not a Saturday. See calendar display.
Furthermore, early in your Question you use the year 2000, while later you use 2001.
Details
The Answer by Lazarus is correct, but uses now-outmoded classes. It is correct in that your formatting pattern failed to match the input string.
First we must fix your faulty input string, changing "Sat" to be "Mon".
String input = "Mon Jan 01 00:00:00 CET 2001"; // After altering the original input string, fixing "Sat" to be "Mon".
Use only java.time classes for your date-time work. These replace Date
& Calendar
etc.
Define a formatting pattern to match your input string. Specify a Locale
to determine the human language and cultural norms used in translating from the input string.
DateTimeFormatter f = DateTimeFormatter.ofPattern( "EEE MMM dd HH:mm:ss z uuuu" , Locale.US );
ZonedDateTime zdt = ZonedDateTime.parse( input , f );
Generate a String
in standard ISO 8601 format extended by appending the name of the time zone in square brackets.
String output = zdt.toString() ;
System.out.println(output);
2001-01-01T00:00+01:00[Europe/Paris]
To see the same moment in UTC, extract a Instant
.
Instant instant = zdt.toInstant() ;
instant.toString(): 2000-12-31T23:00:00Z
Tips
- Your input string uses a terrible format. Always use standard ISO 8601 formats for exchanging date-time values as text.
- Specify a proper time zone name in the format of
continent/region
, such as America/Montreal
, Africa/Casablanca
, or Pacific/Auckland
. Never use the 3-4 letter abbreviation such as CET
, EST
, or IST
as they are not true time zones, not standardized, and not even unique(!).
About java.time
The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date
, Calendar
, & SimpleDateFormat
.
The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.*
classes.
Where to obtain the java.time classes?
The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval
, YearWeek
, YearQuarter
, and more.