8

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 ?

Sameervb
  • 381
  • 3
  • 5
  • 15

6 Answers6

7

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.

Community
  • 1
  • 1
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
3

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?)

Community
  • 1
  • 1
MarioDS
  • 12,895
  • 15
  • 65
  • 121
  • Hi ... Yes it looks very similar to the ISO 8601 format, however the data which I have does not have the T. Hence the question :) – Sameervb Dec 05 '14 at 10:01
  • Going through your link it is mentioned that we can skip T (with mutual agreement. Also there is a example mentioned just below which is : 1985-04-12T10:15:30+04:00 . So if we skip the T , the format I have pasted is ISO-8601 ? – Sameervb Dec 05 '14 at 10:17
2

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
Gabor Szarnyas
  • 4,410
  • 3
  • 18
  • 42
Sandeep
  • 1,154
  • 10
  • 16
2

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

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
Rohit Maurya
  • 730
  • 1
  • 9
  • 22
  • Please don’t teach the young ones to use the long outdated and notoriously troublesome `SimpleDateFormat` class. At least not as the first option. And not without any reservation. Today we have so much better in [`java.time`, the modern Java date and time API,](https://docs.oracle.com/javase/tutorial/datetime/) and its `DateTimeFormatter`. – Ole V.V. Jun 27 '20 at 02:32
  • Agreed @OleV.V. I added this answer here because still many people using java 6,7 in their project. So just wanted to give them alternative solution. – Rohit Maurya Jun 27 '20 at 19:14
1

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"));
Muku
  • 538
  • 4
  • 18
1

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");
Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
  • I recommend you don’t use `SimpleDateFormat` and `Date`. Those classes are poorly designed and long outdated, the former in particular notoriously troublesome. Instead use `OffsetDateTime` and `DateTimeFormatter`, both from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. Oct 16 '20 at 07:11