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 9103423
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T01:37:43+00:00 2026-06-17T01:37:43+00:00

Java 7 has introduced support in the SimpleDateFormat class for ISO 8601 format, via

  • 0

Java 7 has introduced support in the SimpleDateFormat class for ISO 8601 format, via the character X (instead of lower or upper case Z). Supporting such formats in Java 6 requires preprocessing, so the best approach is the question.

This new format is a superset of Z (uppercase Z), with 2 additional variations:

  1. The “minutes” field is optional (i.e., 2-digit instead of 4-digit timezones are valid)
  2. A colon character (‘:’) can be used for separating the 2-digit “hours” field from the 2-digit “minutes” field).

So, as one can observe from the Java 7 documentation of SimpleDateFormat, the following 3 formats are now valid (instead of only the second one covered by Z in Java 6) and, of course, equivalent:

  1. -08
  2. -0800
  3. -08:00

As discussed in an earlier question about a special case of supporting such an “expanded” timezone format, always with ‘:’ as a separator, the best approach for backporting the Java 7 functionality into Java 6 is to subclass the SimpleDateformat class and override its parse() method, i.e:

public Date parse(String date, ParsePosition pos)
{
    String iso = ... // Replace the X with a Z timezone string, using a regex

    if (iso.length() == date.length())
    {
        return null; // Not an ISO 8601 date
    }

    Date parsed = super.parse(iso, pos);

    if (parsed != null)
    {
        pos.setIndex(pos.getIndex()+1); // Adjust for ':'
    }

    return parsed;
}

Note that the subclassed SimpleDateFormat objects above must be initialized with the corresponding Z-based pattern, i.e. if the subclass is ExtendedSimpleDateformat and you want to parse dates complying to the pattern yyyy-MM-dd'T'HH:mm:ssX, then you should use objects instantiated as

new ExtendedSimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");

In the aforementioned earlier question the regex :(?=[0-9]{2}$) has been suggested for getting rid of the ‘:’ and in a similar question the regex (?<=[+-]\d{2})$ has been suggested for appending the “minute” field as 00, if needed.

Obviously, running the 2 replacements successfully can be used for achieving full functionality. So, the iso local variable in the overridden parse() method would be set as

iso = date.replaceFirst(":(?=[0-9]{2}$)","");

or

iso = iso.replaceFirst("(?<=[+-]\\d{2})$", "00");

with an if check in between to make sure that the pos value is also set properly later on and also for the length() comparison earlier.

The question is: can we use a single regular expression to achieve the same effect, including the information needed for not unnecessarily checking the length and for correctly setting pos a few lines later?

The implementation is intended for code that reads very large numbers of string fields that can be in any format (even totally non-date), selects only those which comply to the format and returns the parsed Java Date object.

So, both accuracy and speed are of paramount importance (i.e., if using the 2 passes is faster, this approach is preferrable).

  • 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-06-17T01:37:44+00:00Added an answer on June 17, 2026 at 1:37 am

    Seems that you can use this:

    import java.util.Calendar;
    import javax.xml.bind.DatatypeConverter;
    
    public class TestISO8601 {
        public static void main(String[] args) {
            parse("2012-10-01T19:30:00+02:00"); // UTC+2
            parse("2012-10-01T19:30:00Z");      // UTC
            parse("2012-10-01T19:30:00");       // Local
        }
        public static Date parse(final String str) {
            Calendar c = DatatypeConverter.parseDateTime(str);
            System.out.println(str + "\t" + (c.getTime().getTime()/1000));
            return c.getTime();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Java 5 has introduced support for asynchronous task execution by a thread pool in
Does java has library exception class which means actually not an error, but good
As you all know Adobe has introduced Adobe Flash builder 4.5 with Java Perspective.
Java has curly braces that encapsulate the entire class definition. So when using the
import java.util.Scanner; public class Admit { // the main method has minimal dialogue and
Recently, New API HttpsConnection has introduced in Java ME. I want to know what
I have several final properties defined in a Java class with constructor which has
Java 7 has introduced automatic resource management: try (BufferedReader br = new BufferedReader(new FileReader(path)))
Java has a Comparator<T> for providing comparison of objects external to the class itself,
Java 5 has introduced many features that can be make logging statements less cluttering,

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.