1

I get the following datetime String from a backend system: 2014-06-10+02:00.

Is this a valid datetime? There is no information about the time (I get only the date) but there is a time offset.

If it is valid according to which standard is this valid and what is the UTC time?

Thanks a lot

SteveSt
  • 1,277
  • 2
  • 16
  • 23

4 Answers4

3

This is a valid date, not a date-time.

An offset-from-UTC is relevant to a date. For any given moment the date varies around the globe by time zone. For example, a few minutes after midnight in Paris France is a new day while still “yesterday” in Montréal Québec.

By the way, an offset-from-UTC is not a time zone. A time zone is a history of changes (past, present, and future) to the offset used by a particular region. A time zone has a name in format of continent/region such as America/Montreal.

With a date and an offset, you can determine the range of all moments occurring in that day, all the points on the timeline.

Example code in Java.

ZoneOffset offset = ZoneOffset.parse( "+02:00" );
LocalDate ld = LocalDate.parse( "2014-06-10" ) ;
OffsetDateTime odt = OffsetDateTime.of( ld , LocalTime.MIN , offset );  
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
1

https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html

The output is valid Date but not a valid Time as per ISO Date Specification. Please see ISO_OFFSET_DATE.

UTC (Coordinated Universal Time) is a time standard is defined by International Telecommunications Union.

VivekRatanSinha
  • 596
  • 1
  • 4
  • 17
0

If it is valid according to which standard is this valid and what is the UTC time?

You have asked three questions in this line and the answer to these questions are as follows:

Is it valid?

Yes, it is a valid date string.

You have already mentioned in your question that it does not have a time part; rather, it has a (timezone) offset of +02:00 hours. So, it is just a valid date string, not a date-time string.

Which standard is this?

This is ISO 8601.

What is the UTC time?

A date starts with the start-of-the-day time which, in most cases, is 00:00 hours. However, for the timezones that observe DST, it may not be the case. Such timezones have generally one hour difference in the timezone offset between with and without DST.

Your string has a fixed (timezone) offset (+02:00); rather than a timezone itself (e.g. Africa/Cairo) and therefore, in this case, the start of the day is always 00:00 hours.

So, it can be written as 2014-06-10'T'00:00:00+02:00. As soon as you represent it in this way, I am sure you must have already guessed that it is equivalent to 2014-06-09'T'22:00:00Z where Z is the timezone designator for zero-timezone offset. It stands for Zulu and specifies the Etc/UTC timezone (which has the timezone offset of +00:00 hours).

Enough talking, let's write some code.

import java.time.OffsetDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.time.temporal.ChronoField;
import java.util.Locale;

public class Main {
    public static void main(String[] args) {
        String str = "2014-06-10+02:00";

        DateTimeFormatter dtf = new DateTimeFormatterBuilder()
                                .appendPattern("u-M-d['T'[H[:m[:s]]]]XXX")
                                .parseDefaulting(ChronoField.HOUR_OF_DAY, 0)
                                .parseDefaulting(ChronoField.MINUTE_OF_HOUR, 0)
                                .parseDefaulting(ChronoField.SECOND_OF_MINUTE, 0)
                                .toFormatter(Locale.ENGLISH);

        OffsetDateTime odt = OffsetDateTime.parse(str, dtf);
        System.out.println(odt);

        OffsetDateTime odtUtc = odt.withOffsetSameInstant(ZoneOffset.UTC);
        // The default format omits second and fraction-of-second if they are zero
        System.out.println(odtUtc);
        // Custom format
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("uuuu-MM-dd'T'HH:mm:ssXXX", Locale.ENGLISH);
        System.out.println(formatter.format(odtUtc));
    }
}

Output:

2014-06-10T00:00+02:00
2014-06-09T22:00Z
2014-06-09T22:00:00Z

Learn more about the the modern date-time API* from Trail: Date Time.


* For any reason, if you have to stick to Java 6 or Java 7, you can use ThreeTen-Backport which backports most of the java.time functionality to Java 6 & 7. If you are working for an Android project and your Android API level is still not compliant with Java-8, check Java 8+ APIs available through desugaring and How to use ThreeTenABP in Android Project.

Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
-1

yes it is correct date format.There are lot of place jerusalam..etc with the +2 hour you can find it in your system.