Sameervb
Sameervb

Reputation: 421

Which Java Date format is this "YYYY-MM-DD 00:00:00+00:00"?

I have some data which has date mentioned as "2013-06-30 00:00:00+00:00". I checked the different date formats , however was not able to find this one. Can someone please help ?

Upvotes: 10

Views: 50908

Answers (6)

rahul kunal
rahul kunal

Reputation: 36

Date and time expressed according to ISO 8601 is for example 2020-10-11T00:00:00+00:00 or 2020-10-11T00:00:00.000+00:00.

Converting the ISO 8601 to java.util.Date is bit tricky. We can use the simpleDateformat API of java for the conversion.

sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssXXX");

Upvotes: 2

Rohit Maurya
Rohit Maurya

Reputation: 750

I am adding this answer here because still many people using java 6, 7 in their project.

    Calendar cal = new GregorianCalendar();
    try {
        Date paredDate = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssXXX").parse(s);
        cal.setTime(paredDate);
        return cal;
    } catch (ParseException ex) {
        ex.printStackTrace();
        return null;
    }

You can use above format +OO:OO is for timeZone used

Upvotes: 3

Muku
Muku

Reputation: 562

If you are using java 7+, you can just use X pattern string for ISO8601 time zone. For your given String, below will work:

LocalDate lt = LocalDate.parse(date, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ssXXX"));

Upvotes: 1

Sandeep
Sandeep

Reputation: 1154

I guess it should be YYYY-MM-DD 00:00:00+0000 instead of YYYY-MM-DD 00:00:00+00:00. This format is yyyy-MM-dd HH:mm:ss.SSSZ

DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSSZ");
Date date = new Date();
System.out.println(dateFormat.format(date));

Other different Date formats are

yyyy-MM-dd 1969-12-31
yyyy-MM-dd 1970-01-01
yyyy-MM-dd HH:mm 1969-12-31 16:00
yyyy-MM-dd HH:mm 1970-01-01 00:00
yyyy-MM-dd HH:mmZ 1969-12-31 16:00-0800
yyyy-MM-dd HH:mmZ 1970-01-01 00:00+0000
yyyy-MM-dd HH:mm:ss.SSSZ 1969-12-31 16:00:00.000-0800
yyyy-MM-dd HH:mm:ss.SSSZ 1970-01-01 00:00:00.000+0000
yyyy-MM-dd'T'HH:mm:ss.SSSZ 1969-12-31T16:00:00.000-0800
yyyy-MM-dd'T'HH:mm:ss.SSSZ 1970-01-01T00:00:00.000+0000

Upvotes: 2

Basil Bourque
Basil Bourque

Reputation: 339888

Optional variation of ISO 8601

As for your question about "what format" this is, technically this format is an optional variation of ISO 8601. The standard allows the T to be replaced with a SPACE with mutual agreement between the communicating parties.

The use of a SPACE may make the string more readable by humans where proper numeric-savvy fonts are lacking. But strictly speaking this SPACE version is not standard and so the T should be included when exchanging data between systems or when serializing data as text.

Using java.time

Other answers are correct. Here is an easy alternative for parsing.

Your input string nearly complies with the standard ISO 8601 formats. Replace the SPACE in the middle with a T to comply fully.

String input = "2013-06-30 00:00:00+00:00".replace( " " , "T" );

The java.time classes supplant the troublesome old legacy date-time classes. These newer classes support ISO 8601 formats by default when parsing/generatihng strings.

OffsetDateTime odt = OffsetDateTime.parse( input );

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.

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.

Upvotes: 8

MarioDS
MarioDS

Reputation: 13073

This is an ISO 8601 formatted date with the T omitted between the date and time (see: In an ISO 8601 date, is the T character mandatory?)

Upvotes: 3

Related Questions