0

My Code

String date = "2021-04-05T16:25:45.000+00:00";
SimpleDateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
SimpleDateFormat outputFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss a");
Date parsedDate = null;
try {
    parsedDate = inputFormat.parse(date);
} catch (ParseException e) {
    e.printStackTrace();
}
String formattedDate = outputFormat.format(parsedDate);

Error:

java.lang.NullPointerException: Attempt to invoke virtual method 'long java.util.Date.getTime()' on a null object reference

a_local_nobody
  • 7,947
  • 5
  • 29
  • 51
Faizan
  • 233
  • 3
  • 9
  • As an aside consider throwing away the long outmoded and notoriously troublesome `SimpleDateFormat` and friends. See if you either can use [desugaring](https://developer.android.com/studio/write/java8-support-table) or add [ThreeTenABP](https://github.com/JakeWharton/ThreeTenABP) to your Android project, in order to use java.time, the modern Java date and time API. It is so much nicer to work with. – Ole V.V. Apr 30 '21 at 11:12
  • You have got a parse exception. Check the output from `e.printStackTrace();`. – Ole V.V. Apr 30 '21 at 11:13
  • Does this answer your question in whole or in part? [Generic support for ISO 8601 format in Java 6](https://stackoverflow.com/questions/13040143/generic-support-for-iso-8601-format-in-java-6). And/or this? [String date into Epoch time](https://stackoverflow.com/questions/46142198/string-date-into-epoch-time) – Ole V.V. Apr 30 '21 at 11:17

1 Answers1

0

java.time through desugaring

Consider using java.time, the modern Java date and time API, for your date and time work. Use for example this output formatter:

private static final DateTimeFormatter OUTPUT_FORMATTER
        = DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss a", Locale.forLanguageTag("en-IN"));

With it do:

    String date = "2021-04-05T16:25:45.000+00:00";
    OffsetDateTime dateTime = OffsetDateTime.parse(date);
    String formattedDate = dateTime.format(OUTPUT_FORMATTER);
    
    System.out.println(formattedDate);

Output:

2021-04-05 04:25:45 PM

You may use a different locale for the output formatter as appropriate for your requirements. The choice of locale determines which strings are used for AM and PM.

I am exploiting the fact that the string that you have got is in ISO 8601 format. offsetDateTime parses this format as its default, that is, without any explicit formatter.

What went wrong in your code?

You tried using a format pattern string of yyyy-MM-dd'T'HH:mm:ss.SSS'Z' for a date string of 2021-04-05T16:25:45.000+00:00. The 'Z' in single quotes in the format pattern string means that your date string must end in a literal Z. When instead it ended in +00:00, parsing failed with a ParseException. If you didn’t see the output from e.printStackTrace(); in your code, you have got a serious flaw in your project setup that you should fix before worrying about how to parse the date string. In any case, since parsing failed, parsedDate kept its initial value of null, which caused outputFormat.format(parsedDate) to throw the NullPointerException that you did see.

Question: Doesn’t java.time require Android API level 26?

java.time works nicely on both older and newer Android devices. It just requires at least Java 6.

  • In Java 8 and later and on newer Android devices (from API level 26) the modern API comes built-in.
  • In non-Android Java 6 and 7 get the ThreeTen Backport, the backport of the modern classes (ThreeTen for JSR 310; see the links at the bottom).
  • On older Android either use desugaring or the Android edition of ThreeTen Backport. It’s called ThreeTenABP. In the latter case make sure you import the date and time classes from org.threeten.bp with subpackages.

Links

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161