-1
DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
String dateStr = "2017-03-09T18:30:00.000Z";
Date date = formatter.parse(dateStr);
System.out.println("here");
System.out.println(date);  
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
Deobrat Singh
  • 15
  • 1
  • 4
  • Sure. Note the sparkles in your pattern! – Gyro Gearless May 31 '17 at 06:52
  • Thanks i got the problem is was missing 'Z'. Z should be in single quote as T – Deobrat Singh May 31 '17 at 07:16
  • 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 `Instant` from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). See the answer. – Ole V.V. May 12 '21 at 02:29

1 Answers1

1

Thanks i got the problem is was missing 'Z'. Z should be in single quote as T

No! No! No!

Using 'Z' in the pattern will make Z treated as a character literal instead of as its intended meaning. The Z in the date-time string, 2017-03-09T18:30:00.000Z 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).

Moreover, the modern date-time API* is based on ISO 8601 and does not require using a DateTimeFormatter object explicitly as long as the date-time string conforms to the ISO 8601 standards.

Demo:

import java.time.Instant;

public class Main {
    public static void main(String[] args) {
        Instant instant = Instant.parse("2017-03-09T18:30:00.000Z");
        System.out.println(instant);
    }
}

Output:

2017-03-09T18:30:00Z

For any reason, if you need to convert this object of Instant to an object of java.util.Date, you can do so as follows:

Date date = Date.from(instant);

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

Using the legacy API:

The legacy date-time API (java.util date-time types and their formatting type, SimpleDateFormat) is outdated and error-prone. It is recommended to stop using it completely and switch to java.time, the modern date-time API. Just for the sake of completeness, I have written below a solution using the legacy date-time API.

Use X to handle the Z in your date-time string. Check the documentation page to learn more about the symbols.

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

public class Main {
    public static void main(String[] args) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("y-M-d'T'H:m:s.SSSX", Locale.ENGLISH);
        Date date = sdf.parse("2017-03-09T18:30:00.000Z");
        System.out.println(date);// Note: will display date-time in your local timezone
    }
}

Output in my timezone:

Thu Mar 09 18:30:00 GMT 2017

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 e.g.

Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX", Locale.ENGLISH);
sdf.setTimeZone(TimeZone.getTimeZone("America/New_York"));
System.out.println(sdf.format(date));

* 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