2

I am trying to parse this String date : "2020-06-25T07:48:32Z" to a Date like "2020-06-25" and I did a method like this :

        String newDateFormat = "yyyy-MM-dd";
                try {
                    Date newparseDate = new SimpleDateFormat(newDateFormat).parse(date);
                    System.out.println(newparseDate);
                    return new SimpleDateFormat(dateTimeFormatPattern).parse(date);
                } catch (ParseException px) {
                    px.printStackTrace();
                }
                return null;
            }
    

But I got this format: Thu Jun 25 00:00:00 CEST 2020

mononoke83
  • 342
  • 2
  • 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 `OffsetDateTime`, and `LocalDate`, both from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. Jun 25 '20 at 15:09
  • Could you provide [a complete, reproducible example](https://stackoverflow.com/help/minimal-reproducible-example), please? Without the whole code it’s hard to tell what went wrong for you. – Ole V.V. Jun 25 '20 at 15:10
  • Does this answer your question? [java.text.ParseException: Unparseable date: “20:01:00.000Z”](https://stackoverflow.com/questions/56302402/java-text-parseexception-unparseable-date-200100-000z) – Ole V.V. Jun 25 '20 at 15:12
  • "2020-06-25T07:48:32Z" is a point in time (given in UTC in the string). Do you want the date *in your own time zone* at that point in time? It’s never the same date in all time zones. – Ole V.V. Jun 25 '20 at 15:14

1 Answers1

2

I strongly recommend you use the modern date-time API instead of the broken java.util date-time API.

import java.time.LocalDate;
import java.time.ZonedDateTime;

public class Main {
    public static void main(String[] args) {
        ZonedDateTime zdt = ZonedDateTime.parse("2020-06-25T07:48:32Z");
        System.out.println(zdt);

        // Your required format can be got by simply using LocalDate which drops the
        // time-zone and offset information
        LocalDate ldt = zdt.toLocalDate();
        System.out.println(ldt);
    }
}

Output:

2020-06-25T07:48:32Z
2020-06-25

However, if you still want to use the outdated date-time API, you can do it as follows:

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

public class Main {
    public static void main(String[] args) throws ParseException {
        // Format for the given date-time string
        SimpleDateFormat oldDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");

        // Desired format
        SimpleDateFormat newDateFormat = new SimpleDateFormat("yyyy-MM-dd");

        // The given date-time string
        String dateStr = "2020-06-25T07:48:32Z";

        // Parse to java.util.Date
        Date newParseDate = oldDateFormat.parse(dateStr);

        // Format to the desired format
        String newDateStr = newDateFormat.format(newParseDate);
        System.out.println(newDateStr);
    }
}

Output:

2020-06-25
Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
  • Thank you Arvind ,from the outdated date time, I would like to return that (string) as a Date format, is it possible? – mononoke83 Jun 25 '20 at 09:09
  • @mononoke83 - `newDateFormat.format(newParseDate)` returns a `String` but just to make it clear for you, I've stored it in a `String` explicitly, before the last print statement. I hope, this is what you are looking for. Feel free to comment in case of any doubt/issue. – Arvind Kumar Avinash Jun 25 '20 at 09:13
  • Ok, but I would like return as a Date type, not as a String. Thanks, anyways. – mononoke83 Jun 25 '20 at 09:50
  • 1
    @mononoke83 - A `Date` object is an object which has date-time information (e.g. year, month, day etc.) and if you print a `Date` object, it will always invoke its [toString](http://hg.openjdk.java.net/jdk7u/jdk7u6/jdk/file/8c2c5d63a17e/src/share/classes/java/util/Date.java) method. That is why you have `SimpleDateFormat` class so that you can print it in your desired formats. – Arvind Kumar Avinash Jun 25 '20 at 10:08