-1

I have a date object that returns the below string value in doing date.toString()

String date = "Wed Jun 27 12:33:00 CDT 2018";

And I want to format it in exactly this style:

"June-27-2018 5:33:00 PM GMT".

I tried using SimpleDateFormat

protected SimpleDateFormat dateFormat = new SimpleDateFormat( "MMMM-dd-yyyy h:mm:ss a z", Locale.US);

But I keep getting a parse exception. Is there any way to format this the way I need it to? The timezone needs to be converted too.

SonhnLab
  • 321
  • 1
  • 11
  • It would sound like you are trying the `parse` method when you should use the `format` method for formatting? I recommend you avoid the `SimpleDateFormat` and `Date` classes. They are not only long outdated, `SimpleDateFormat` 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. Jun 27 '18 at 20:48
  • 1
    Welcome to Stack Overflow! Your question is very well suited for [a Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve). So please? – Ole V.V. Jun 27 '18 at 20:51
  • 1
    Please search Stack Overflow thoroughly before posting. This issue of formatting pattern for parsing date-time strings has been covered [many many many times](https://duckduckgo.com/?q=site%3Astackoverflow.com+java+parse+date&t=osx&ia=web) already. – Basil Bourque Jun 28 '18 at 00:27

3 Answers3

1

First, you shouldn’t have a Date object. The Date class is long outdated (no pun intended). Today you should prefer to use java.time, the modern and much nicer date and time API. However, I am assuming that you are getting a Date from some legacy API that you cannot change. The first thing you should do is convert it to an Instant. Instant is the corresponding class in java.time. Then you should do any further operations from there.

    DateTimeFormatter formatter
            = DateTimeFormatter.ofPattern("MMMM-dd-yyyy h:mm:ss a z", Locale.US);
    ZoneId desireedZone = ZoneId.of("Etc/GMT");
    Date yourOldfashionedDate = // …;
    ZonedDateTime dateTimeInGmt = yourOldfashionedDate.toInstant().atZone(desireedZone);
    String formattedDateTime = dateTimeInGmt.format(formatter);
    System.out.println(formattedDateTime);

This snippet prints the desired:

June-27-2018 5:33:00 PM GMT

Converting directly from the Date object is safer and easier than converting from its string representation. The biggest problem with the latter is that the string contains CDT as time zone, which is ambiguous. It may stand for Australian Central Daylight Time, North American Central Daylight Time, Cuba Daylight Time or Chatham Daylight Time. You cannot be sure which one Java is giving you. Never rely on three and four letter time zone abbreviations if there is any way you can avoid it.

Link: Oracle tutorial: Date Time explaining how to use java.time.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
0

Your date string cannot parse to the format you have given, so change the format to EEE MMM dd HH:mm:ss zzz yyyy

    String myDate = "Wed Jun 27 12:33:00 CDT 2018";
    SimpleDateFormat dateFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.US);
    SimpleDateFormat dateFormat_2 = new SimpleDateFormat("MMMM-dd-yyyy h:mm:ss a z", Locale.US);
    dateFormat_2.setTimeZone(TimeZone.getTimeZone("GMT"));

    Date d = dateFormat.parse(myDate);
    dateFormat_2.format(d);
    System.out.println(dateFormat_2.format(d));

Output :

June-27-2018 12:33:00 PM GMT
Madushan Perera
  • 2,568
  • 2
  • 17
  • 36
0

You will achieve your desired output if you pass date or object to format function.

SimpleDateFormat dateFormat = new SimpleDateFormat( "MMMM-dd-yyyy h:mm:ss a z", Locale.US);

String ans=dateFormat.format(param);

In above code param must be date or object so first convert string to date and then apply format function to get your desired output.

See below Sample code

 SimpleDateFormat dateFormat = new SimpleDateFormat( "MMMM-dd-yyyy h:mm:ss a z", Locale.US);
 dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
 String ans=dateFormat.format(new Date());

Sample output:

June-27-2018 6:22:35 PM GMT
Manoj Kumar Dhakad
  • 1,862
  • 1
  • 12
  • 26