0
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss`ZZ:ZZ`");
        sdf.parse("2020-12-16T16:27:57+00:00");

I am unsure what foramt the date is in but, I am pretty sure it's yyyy-MM-dd'T'HH:mm:ssZZ:ZZ or something like that. I was guessing with the z's from https://help.sumologic.com/03Send-Data/Sources/04Reference-Information-for-Sources/Timestamps%2C-Time-Zones%2C-Time-Ranges%2C-and-Date-Formats

What is the right format and how do I convert it to ms?

I have also tried

Instant instant = Instant.parse("2020-12-16T16:27:57+00:00");

I am grabbing it from a minecraft json file. I refuse to use joda time as minecraft was written in java 5 originally and didn't use joda time to do it. They later updated to java 7

Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
  • You don't seem to have those single quotes in the sample in the title. – 500 - Internal Server Error Dec 23 '20 at 08:41
  • alright but, could you please tell me what format the date is in? I am looking at a web json api with no documentation on this. everything else is done except this – nullsector76 Dec 23 '20 at 13:09
  • (1) 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` from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). (2) The format is ISO 8601, the international standard. – Ole V.V. Jan 09 '21 at 07:18

3 Answers3

1
OffsetDateTime.parse( "2020-12-16T16:27:57+00:00" )

Never use SimpleDateFormat or the other terrible date-time classes that were years ago supplanted by the modern java.time classes.

The java.time classes use standard ISO 8601 formats by default when parsing/generating strings that represent date-time values. Your input is in one of those standard formats. So no need to specify a formatting pattern.


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.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.* classes. Hibernate 5 & JPA 2.2 support java.time.

Where to obtain the java.time classes?

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

figured it out

     * parse them zula times. example: 2010-09-18T21:35:15.000Z
     */
    public static long parseZTime(String strTime) 
    {
        return Instant.parse(strTime).toEpochMilli();
    }
    
    /**
     * parse the offset times. example: 2015-11-10T16:43:29-0500 and 2014-05-14T17:29:23+00:00
     */
    public static long parseOffsetTime(String strTime)
    {
        return OffsetDateTime.parse(strTime).toInstant().toEpochMilli();
    }
  • Thanks for posting your solution (+1). Your `parseOffsetTime()` also accepts `2010-09-18T21:35:15.000Z`, so you don’t necessarily need two methods. – Ole V.V. Jan 09 '21 at 08:22
0

java.time

The date-time API of java.util and their formatting API, SimpleDateFormat are outdated and error-prone. It is recommended to stop using them completely and switch to the modern date-time API.

Using the modern date-time API:

Since your date-time string conforms to ISO-8601 format, you can parse it directly (i.e. without using a DateTimeFormatter) to OffsetDateTime.

import java.time.OffsetDateTime;
import java.util.concurrent.TimeUnit;

public class Main {
    public static void main(String[] args) {
        String strDateTime = "2020-12-16T16:27:57+00:00";
        OffsetDateTime odt = OffsetDateTime.parse(strDateTime);
        System.out.println(odt);
        System.out.println("No of milliseconds from Epoch: " + odt.toInstant().toEpochMilli());
        // Alternatively
        System.out.println("No of milliseconds from Epoch: " + TimeUnit.SECONDS.toMillis(odt.toEpochSecond()));
    }
}

Output:

2020-12-16T16:27:57Z
No of milliseconds from Epoch: 1608136077000
No of milliseconds from Epoch: 1608136077000

What went wrong with your answer?

  1. Use of ZZ:ZZ instead of a single X: Check the documentation to learn more about it. Also, I am not sure whether you have enclosed ZZ:ZZ within quotes, used for formatting code, just to highlight it as a code or you have really done this in actual code. Make sure you never enclose Z within single quotes; otherwise, it will simply mean Z as a character literal instead of timezone. Check this answer to understand it better.
  2. Although it's not mandatory for this parsing, you should never use SimpleDateFormat or DateTimeFormatter without a Locale. Check this answer to understand it better.

Using legacy API:

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.util.Date;
import java.util.Locale;

public class Main {
    public static void main(String[] args) throws ParseException {
        String strDateTime = "2020-12-16T16:27:57+00:00";
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssX", Locale.ENGLISH);
        Date date = sdf.parse(strDateTime);
        System.out.println(date);
        System.out.println("No of milliseconds from Epoch: " + date.getTime());

        // Given below is how you can convert the legacy type, java.util.Date to the
        // modern type java.time.Instant
        Instant instant = date.toInstant();
        System.out.println("No of milliseconds from Epoch: " + instant.toEpochMilli());
    }
}

Output:

Wed Dec 16 16:27:57 GMT 2020
No of milliseconds from Epoch: 1608136077000
No of milliseconds from Epoch: 1608136077000

Note that the java.util.Date object is not a real date-time object like the modern date-time types; rather, it represents the number of milliseconds since the standard base time known as "the epoch", namely January 1, 1970, 00:00:00 GMT (or UTC). When you print an object of java.util.Date, its toString method returns the date-time in the JVM's timezone, calculated from this milliseconds value. If you need to print the date-time in a different timezone, you will need to set the timezone to SimpleDateFormat and obtain the formatted string from it.

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