0

How can I convert "E MMM dd HH:mm:ss z yyyy" (String type) to Date object in Java? I have tried parsing, but it gives a string output.

DateFormat dfNy1 = new SimpleDateFormat("E MMM dd HH:mm:ss z yyyy", Locale.ROOT);
dfNy1.setTimeZone(TimeZone.getTimeZone("EST"));

final DateFormat df = new SimpleDateFormat("E MMM dd HH:mm:ss z yyyy", Locale.ROOT);
        df.setTimeZone(TimeZone.getTimeZone("UTC"));

System.out.println(df.format(dfNy1.parse("Thu Dec 13 00:00:00 EST 2018")));

Output: Thu Dec 13 05:00:00 UTC 2018

Iam getting the output in String format, But I need the same as Date object.

ernest_k
  • 44,416
  • 5
  • 53
  • 99
Shubha
  • 1
  • 1
    I recommend you avoid the `SimpleDateFormat` class. It is not only long outdated, it is also notoriously troublesome. Today we have so much better in [`java.time`, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. Nov 26 '18 at 07:05
  • 1
    Parsing is correct. Only don’t format because formatting will give you a string back (that’s what formatting means). `ZonedDateTime.parse("Thu Dec 13 00:00:00 EST 2018", DateTimeFormatter.ofPattern("E MMM dd HH:mm:ss z yyyy", Locale.ROOT))` yields a `ZonedDateTime` of `2018-12-13T00:00-05:00[America/New_York]`. – Ole V.V. Nov 26 '18 at 07:06
  • Yes: `dfNy1.parse("Thu Dec 13 00:00:00 EST 2018")`gives you the `Date` you want. Why are you formatting it? – Maurice Perry Nov 26 '18 at 07:12
  • Are you really after a `Date` with a specific time zone and format? Such a beast doesn’t exist. A `Date` is a point in time, it cannot have neither time zone nor format. – Ole V.V. Nov 26 '18 at 07:14
  • Possible duplicate of [Java string to date conversion](https://stackoverflow.com/questions/4216745/java-string-to-date-conversion). Or of [want current date and time in “dd/MM/yyyy HH:mm:ss.SS” format](https://stackoverflow.com/questions/8745297/want-current-date-and-time-in-dd-mm-yyyy-hhmmss-ss-format) and [Calendar returns date in wrong time zone](https://stackoverflow.com/questions/26678030/calendar-returns-date-in-wrong-time-zone). – Ole V.V. Nov 26 '18 at 07:21
  • Thanks for the suggestions Ole and Maurice.. dfNy1.parse("Thu Dec 13 00:00:00 EST 2018") this gives me the output in date type.. but the timezone changes to that of default one.. – Shubha Nov 26 '18 at 08:26
  • No it doesn’t, @Shubha, sorry. I repeat, a `Date` hasn’t got and cannot have a time zone. What confuses you is that `Date.toString` grabs your JVM’s default time zone and uses it for generating the string that is printed when you print the date. This tells nothing about what is inside the `Date` object. Instead you need to use `ZonedDateTime` from java.time. As the name says, contrary to a `Date` this *does* hold a time zone. – Ole V.V. Nov 26 '18 at 12:35

3 Answers3

1

Format method of SimpleDateFormatter class has return type of StringBuffer so If you want to format a date it will always return string.

You can read java docs for the same. https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html

When you format a date from one pattern to another date object from that string will still remain the same. So why do you want to convert your object into date?

Pooja Aggarwal
  • 1,163
  • 6
  • 14
0

Please don't format date again , it will return in string format.

DateFormat dfNy1 = new SimpleDateFormat("E MMM dd HH:mm:ss z yyyy", Locale.ROOT);
dfNy1.setTimeZone(TimeZone.getTimeZone("EST"));

try {
        Date d = dfNy1.parse("Thu Dec 13 00:00:00 EST 2018");
        System.out.println(s);`enter code here`
} catch (ParseException e1) {
 e1.printStackTrace();
}
samji
  • 31
  • 6
0

As you already found out, you can already successfully parse your String value into a Date object:

DateFormat df = new SimpleDateFormat("E MMM dd HH:mm:ss z yyyy", Locale.ROOT);
Date date = df.parse("Thu Dec 13 00:00:00 EST 2018");

But as a Date object is an 'instant in time', it does not hold any time-zone information; it is nothing more than a container for milliseconds, which means after parsing the time-zone information gets lost. See Java API doc for Date:

The class Date represents a specific instantin time, with millisecond precision

Fortunately, since Java 8 there is a new time-API (see package java.time) that has many features that are missing in the old Date or Calendar API.

To parse your String value into a full-featured date/time object including time zone, use a java.time.format.DateTimeFormatter now:

DateTimeFormatter format = DateTimeFormatter.ofPattern("E MMM dd HH:mm:ss z yyyy", Locale.ROOT);
ZonedDateTime time = ZonedDateTime.parse("Thu Dec 13 00:00:00 EST 2018", dtf);

Now you can use this java.time.ZonedDateTime directly, or convert it to any other time zone, preserving the "instant in time":

ZonedDateTime gmtTime = time.withZoneSameInstant(ZoneId.of("GMT");

If you are just interested in the local time (skipping time zone information), convert it to a LocalDateTime:

LocalDateTime localTime = zdt.toLocalDateTime();
LocalDateTime localGmtTime = gmtTime.toLocalDateTime();

For more information about how to use the new API, have a look at this Tutorial about the Java time API.

isnot2bad
  • 24,105
  • 2
  • 29
  • 50