-3

I have a list of dates. Example:

Tue Oct 21 17:05:37 EDT 2014
Tue Oct 22 18:05:37 IST 2014
Tue Oct 23 19:05:37 EST 2014

Since all dates are in dfferent timezone, i want to convert all dates to GMT zone and then convert all dates to yyyy-MM-dd'T'HH:mm:ss format. ie, like this 2014-10-21T17:05:37 .

How can i do this?

Thanks

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Dinoop Nair
  • 2,663
  • 6
  • 31
  • 51
  • Your input strings are faulty. The 21st, 22nd, and 23rd cannot all be Tuesday. Please put more effort into writing your Question. – Basil Bourque Feb 15 '20 at 05:18

2 Answers2

2

You could do the following:

Define the dates you have in String variables like this:

String date1 = "Tue Oct 21 17:05:37 EDT 2014";
String date2 = "Wed Oct 22 18:05:37 IST 2014";
String date3 = "Thu Oct 23 19:05:37 EST 2014";

Create the representation of the current format that your dates are in:

SimpleDateFormat currentSDF = new SimpleDateFormat("EEE MMM d HH:mm:ss zzz yyyy");
SimpleDateFormat wantedSDF = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");

And finally parse and format the dates:

System.out.println(wantedSDF.format(currentSDF.parse(date1)));
System.out.println(wantedSDF.format(currentSDF.parse(date2)));
System.out.println(wantedSDF.format(currentSDF.parse(date3)));

You may check all the availiable symbols for SimpleDateFormat

  • 1
    FYI, the terribly flawed date-time classes such as [`java.util.Date`](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/Date.html), [`java.util.Calendar`](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/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/tutorial/datetime/TOC.html) classes built into Java 8 and later. Suggesting their use in 2020 is poor advice. – Basil Bourque Feb 15 '20 at 05:11
  • Well it was 2015 when I replied and not everybody was using Java 8 at that time.. – Christoforos Vasilatos Feb 16 '20 at 07:01
  • Thus my use of the word “now”. My comment is not a criticism, it is a note to the future readers. – Basil Bourque Feb 16 '20 at 07:48
2

java.time

Use modern java.time classes rather than the troublesome legacy classes seen in the other Answer. Specifically, the ZonedDateTime class.

By the way, those input string formats are terrible. Especially the use of EDT, IST, and such. Those are not true time zones. These are real time zone names. Those pseudo-zones are not standardized, and are not even unique! So the ZonedDateTime class will take a guess as to what you might mean, but the results may be other than you intended. For example, is IST Irish Standard Time or India Standard Time?

Instead, use only the standard ISO 8601 formats when exchanging date-time values as text.

I fixed your faulty input strings (Tuesday = 21, 22, and 23?).

List < String > inputs = List.of(
        "Tue Oct 21 17:05:37 EDT 2014" ,
        "Wed Oct 22 18:05:37 IST 2014" ,
        "Thu Oct 23 19:05:37 EST 2014"
);

Define a formatting pattern to match your inputs.

DateTimeFormatter f = DateTimeFormatter.ofPattern( "EEE MMM dd HH:mm:ss z uuuu" ).withLocale( Locale.US );

Loop the inputs, attempting to parse each. Again, no guarantee as to the right time zone being guessed for your pseudo-zones.

for ( String input : inputs )
{
    ZonedDateTime zdt = ZonedDateTime.parse( input , f );
    System.out.println( "zdt.toString() = " + zdt );
}

When run.

zdt.toString() = 2014-10-21T17:05:37-04:00[America/New_York]

zdt.toString() = 2014-10-22T18:05:37Z[Atlantic/Reykjavik]

zdt.toString() = 2014-10-23T19:05:37-04:00[America/New_York]

enter image description here


About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.* classes.

Where to obtain the java.time classes?

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154