0

I tried to parse this string :

"2018-08-21T10:12:06.872722+00:00"

but got java.text.ParseException.

I'm using this format :

"yyyy-MM-dd'T'HH:mm:ss.SSSz"

important: I tried to use: XXX, ZZZZZ, ZZZ, z for timezone, but not working

What is wrong? thanks.

covert date to new format:

formatDate("2018-08-21T10:12:06.872722+00:00");

public static String formatDate(String date) {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSz");
    SimpleDateFormat output = new SimpleDateFormat("yyyy-MM-dd");

    Date d;
    try {
        d = sdf.parse(date);
    } catch (ParseException e) {
        e.printStackTrace();
        return "";
    }

    return output.format(d);
}
SWR
  • 123
  • 6
  • 1
    please show your code. – Abhishek Aug 22 '18 at 04:30
  • @VivekMishra, i tried solutions from your topic, it's not work – SWR Aug 22 '18 at 04:36
  • @Abhishek, i updated my question :) – SWR Aug 22 '18 at 04:47
  • @SWR i've given an answer. Hope you find it helpful. – Abhishek Aug 22 '18 at 05:38
  • As an aside consider throwing away the long outmoded and notoriously troublesome `SimpleDateFormat` and friends, and adding [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. Aug 22 '18 at 09:19

4 Answers4

1

You can call this method.

void checkDateParsing()
        {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSSSSZ");
            Date d = null;
            try {
                d = sdf.parse("2018-08-21T10:12:06.872722+00:00");
            } catch (ParseException e) {
                e.printStackTrace();
            }
            Log.e("date",d.toGMTString());
        }
piet.t
  • 11,718
  • 21
  • 43
  • 52
PRATEEK BHARDWAJ
  • 2,364
  • 2
  • 21
  • 35
1

The SimpleDateFormat class you are using is now legacy, and should be supplemented by the java.time classes. Also it isn't thread-safe.

Therefore if you are using java 8 or above then use the below sample code::

public static void main(String[] args) {
        String date = "2018-08-21T10:12:06.872222+00:00";
        LocalDateTime parsedDate = LocalDateTime.parse(date, java.time.format.DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSSSSz"));
        System.out.println(parsedDate.format(java.time.format.DateTimeFormatter.ofPattern("yyyy-MM-dd")));
}

If you are stuck with using SimpleDateFormat class then follow the answer by @Prateek.

Abhishek
  • 2,485
  • 2
  • 18
  • 25
  • 1
    No need to code your own format pattern strings since the involved formats are standard (ISO 8601). Just use `DateTimeFormatter.ISO_OFFSET_DATE_TIME` for parsing and `DateTimeFormatter.ISO_LOCAL_DATE` for formatting. You may even parse into an `OffsetDateTime` without mentioning any formatter at all. – Ole V.V. Aug 22 '18 at 11:11
1

In java 8 or above, you can achieve the same effect using one-line code as:

System.out.println(OffsetDateTime.parse("2018-08-21T10:12:06.872722+00:00",
            DateTimeFormatter.ISO_OFFSET_DATE_TIME).toLocalDate());

Output:

2018-08-21
Hearen
  • 7,420
  • 4
  • 53
  • 63
0
public class test1 {
public static void main(String[] args) {            
    System.out.println(formatDate("2018-08-21-10:12:06.872+0100"));

} 


public static String formatDate(String date) {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd-HH:mm:ss.SSSZ");
    SimpleDateFormat output = new SimpleDateFormat("yyyy-MM-dd");

Date d;
try {
    d = sdf.parse(date);
} catch (ParseException e) {
    e.printStackTrace();
    return "";
}
return output.format(d);

}

Dilan
  • 2,610
  • 7
  • 23
  • 33