I have a date string and I want to parse it to normal date use the java Date API,the following is my code:
public static void main(String[] args) { String date="2010-10-02T12:23:23Z"; String pattern="yyyy-MM-ddThh:mm:ssZ"; SimpleDateFormat sdf=new SimpleDateFormat(pattern); try { Date d=sdf.parse(date); System.out.println(d.getYear()); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
However I got an exception: java.lang.IllegalArgumentException: Illegal pattern character 'T'
So I wonder if I have to split the string and parse it manually?
BTW, I have tried to add a single quote character on either side of the T:
String pattern="yyyy-MM-dd'T'hh:mm:ssZ";
It also does not work.
Update for Java 8 and higher
You can now simply do Instance.parse("2015-04-28T14:23:38.521Z")
and get the correct thing now, especially since you should be using Instance
instead of the broken java.util.Date
with the most recent versions of Java.
You should be using DateTimeFormatter
instead of SimpleDateFormatter
as well.
The explanation below is still valid as as what the format represents.
This works with the input with the trailing Z
as demonstrated:
In the pattern the T
is escaped with '
on either side.
The pattern for the Z
at the end is actually XXX
as documented in the JavaDoc for SimpleDateFormat
, it is just not very clear on actually how to use it since Z
is the marker for the old TimeZone
information as well.
import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.GregorianCalendar; import java.util.TimeZone; public class Q2597083 { /** * All Dates are normalized to UTC, it is up the client code to convert to the appropriate TimeZone. */ public static final TimeZone UTC; /** * @see Combined Date and Time Representations */ public static final String ISO_8601_24H_FULL_FORMAT = "yyyy-MM-dd'T'HH:mm:ss.SSSXXX"; /** * 0001-01-01T00:00:00.000Z */ public static final Date BEGINNING_OF_TIME; /** * 292278994-08-17T07:12:55.807Z */ public static final Date END_OF_TIME; static { UTC = TimeZone.getTimeZone("UTC"); TimeZone.setDefault(UTC); final Calendar c = new GregorianCalendar(UTC); c.set(1, 0, 1, 0, 0, 0); c.set(Calendar.MILLISECOND, 0); BEGINNING_OF_TIME = c.getTime(); c.setTime(new Date(Long.MAX_VALUE)); END_OF_TIME = c.getTime(); } public static void main(String[] args) throws Exception { final SimpleDateFormat sdf = new SimpleDateFormat(ISO_8601_24H_FULL_FORMAT); sdf.setTimeZone(UTC); System.out.println("sdf.format(BEGINNING_OF_TIME) = " + sdf.format(BEGINNING_OF_TIME)); System.out.println("sdf.format(END_OF_TIME) = " + sdf.format(END_OF_TIME)); System.out.println("sdf.format(new Date()) = " + sdf.format(new Date())); System.out.println("sdf.parse(\"2015-04-28T14:23:38.521Z\") = " + sdf.parse("2015-04-28T14:23:38.521Z")); System.out.println("sdf.parse(\"0001-01-01T00:00:00.000Z\") = " + sdf.parse("0001-01-01T00:00:00.000Z")); System.out.println("sdf.parse(\"292278994-08-17T07:12:55.807Z\") = " + sdf.parse("292278994-08-17T07:12:55.807Z")); } }
Produces the following output:
sdf.format(BEGINNING_OF_TIME) = 0001-01-01T00:00:00.000Z sdf.format(END_OF_TIME) = 292278994-08-17T07:12:55.807Z sdf.format(new Date()) = 2015-04-28T14:38:25.956Z sdf.parse("2015-04-28T14:23:38.521Z") = Tue Apr 28 14:23:38 UTC 2015 sdf.parse("0001-01-01T00:00:00.000Z") = Sat Jan 01 00:00:00 UTC 1 sdf.parse("292278994-08-17T07:12:55.807Z") = Sun Aug 17 07:12:55 UTC 292278994
tl;dr
Instant.parse( "2010-10-02T12:23:23Z" )
ISO 8601
That format is defined by the ISO 8601 standard for date-time string formats.
Both:
…use ISO 8601 formats by default for parsing and generating strings.
You should generally avoid using the old java.util.Date/.Calendar & java.text.SimpleDateFormat classes as they are notoriously troublesome, confusing, and flawed. If required for interoperating, you can convert to and fro.
java.time
Built into Java 8 and later is the new java.time framework. Inspired by Joda-Time, defined by JSR 310, and extended by the ThreeTen-Extra project.
Instant instant = Instant.parse( "2010-10-02T12:23:23Z" ); // `Instant` is always in UTC.
Convert to the old class.
java.util.Date date = java.util.Date.from( instant ); // Pass an `Instant` to the `from` method.
Time Zone
If needed, you can assign a time zone.
ZoneId zoneId = ZoneId.of( "America/Montreal" ); // Define a time zone rather than rely implicitly on JVM’s current default time zone. ZonedDateTime zdt = ZonedDateTime.ofInstant( instant , zoneId ); // Assign a time zone adjustment from UTC.
Convert.
java.util.Date date = java.util.Date.from( zdt.toInstant() ); // Extract an `Instant` from the `ZonedDateTime` to pass to the `from` method.
Joda-Time
Here is some example code in Joda-Time 2.8.
org.joda.time.DateTime dateTime_Utc = new DateTime( "2010-10-02T12:23:23Z" , DateTimeZone.UTC ); // Specifying a time zone to apply, rather than implicitly assigning the JVM’s current default.
Convert to old class. Note that the assigned time zone is lost in conversion, as j.u.Date cannot be assigned a time zone.
java.util.Date date = dateTime_Utc.toDate(); // The `toDate` method converts to old class.
Time Zone
If needed, you can assign a time zone.
DateTimeZone zone = DateTimeZone.forID( "America/Montreal" ); DateTime dateTime_Montreal = dateTime_Utc.withZone ( zone );