Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 708065
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T04:20:56+00:00 2026-05-14T04:20:56+00:00

I have a date string and I want to parse it to normal date

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T04:20:56+00:00Added an answer on May 14, 2026 at 4:20 am

    Update for Java 8 and higher

    You can now simply do Instant.parse("2015-04-28T14:23:38.521Z") and get the correct thing now, especially since you should be using Instant instead of the broken java.util.Date with the most recent versions of Java.

    You should be using DateTimeFormatter instead of SimpleDateFormatter as well.

    Original Answer:

    The explanation below is still valid as as what the format represents.
    But it was written before Java 8 was ubiquitous so it uses the old
    classes that you should not be using if you are using Java 8 or
    higher.

    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.

    Q2597083.java

    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 <a href="http://en.wikipedia.org/wiki/ISO_8601#Combined_date_and_time_representations">Combined Date and Time Representations</a>
         */
        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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 387k
  • Answers 388k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The smalltalk package is in the unstable branch of fink,… May 15, 2026 at 12:16 am
  • Editorial Team
    Editorial Team added an answer Facebook and Twitter are on both types. The web apps… May 15, 2026 at 12:16 am
  • Editorial Team
    Editorial Team added an answer There are two general solutions to this kind of problem:… May 15, 2026 at 12:16 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.