0

I try to use java.util.Date date = Date.from( Instant.parse(minDates)); to parse the date string given in format Wed Jan 17 2001 00:00:00 GMT 0530.

I am not able to figure out, how to do that in JAVA.

The want to convert the given date string in given format

2013-05-22T00:00:00

May be i am not able to figure it out, properly. If someone have way to do that suggest me in Java Only.

user2903536
  • 1,716
  • 18
  • 25
  • 3
    Don't use the `Date` API, rather you should use the `LocalDateTime` API unless there is a good reason not to. – Ousmane D. Jan 08 '18 at 13:01
  • Please notice, `LocalDateTime` is available from JDK 8 https://docs.oracle.com/javase/8/docs/api/java/time/LocalDateTime.html – xxxvodnikxxx Jan 08 '18 at 13:06
  • @xxxvodnikxxx No, **`LocalDateTime` is the wrong class here** as it lacks any concept of time zone or offset-from-UTC. Use `Instant` for a moment always in UTC, use `OffsetDateTime` for a moment in some particular offset-from-UTC, and `ZonedDateTime` for a moment in a particular time zone. – Basil Bourque Jan 08 '18 at 17:09
  • It was just additional info to @Aominè comment, but thanks, good point – xxxvodnikxxx Jan 09 '18 at 07:58

3 Answers3

0

You can do that by using SimpleDateFormat 'parse' API.

You can initialize your SimpleDateFormat with any valid time format such as yyyy-MM-dd HH:mm:ss z and then parse any string which adheres to this format.

reff. to http://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html

One addition tip, use JodaTime as the Date and SDF in Java are getting deprecated: http://www.joda.org/joda-time/

xxxvodnikxxx
  • 1,270
  • 2
  • 18
  • 37
Vishal Raja
  • 303
  • 1
  • 8
0

Here is the solution:

    String dateToParse = "Wed Jan 17 2001 00:00:00 GMT 0530";

    SimpleDateFormat in = new SimpleDateFormat("EEE MMM dd YYYY HH:mm:ss");
    SimpleDateFormat out = new SimpleDateFormat("YYYY-MM-dd'T'HH:mm:ss");

    Date date = in.parse( dateToParse );

    System.out.println( out.format( date ) );

It will work if all dates are in the same timezone (GMT 0530) Else it should be modified to support it, but I suppose you have the same timezone.

Igor
  • 608
  • 6
  • 11
  • FYI, the troublesome old date-time classes such as [`java.util.Date`](https://docs.oracle.com/javase/9/docs/api/java/util/Date.html), [`java.util.Calendar`](https://docs.oracle.com/javase/9/docs/api/java/util/Calendar.html), and `java.text.SimpleDateFormat` are now [legacy](https://en.wikipedia.org/wiki/Legacy_system), supplanted by the [java.time](https://docs.oracle.com/javase/9/docs/api/java/time/package-summary.html) classes built into Java 8 & Java 9. See [Tutorial by Oracle](https://docs.oracle.com/javase/tutorial/datetime/TOC.html). – Basil Bourque Jan 09 '18 at 05:52
0

If you are using Java 8+, You can use java.time.OffsetDateTime (or Instant...) instead of java.util.Date, which is incredibly easy.

OffsetDateTime odt = OffsetDateTime .parse("2013-05-22T00:00:00", DateTimeFormatter.ISO_LOCAL_DATE_TIME);

Note that the second argument is optional in this case but you could have to specify one (with timezone id for example).

There is a solution without external which works with older version of Java and that manages timezones well. It consists of using JAXB's DataTypeConverter.

Date date =  javax.xml.bind.DatatypeConverter.parseDateTime("2013-05-22T00:00:00+01:00").getTime();

Note that DatatypeConverter.parseDateTime returns a Calendar. You just need to call its getTime() method to convert is to a Date.

C.Champagne
  • 5,381
  • 2
  • 23
  • 35