-1

I have a string like the following

Fri May 31 2019 05:08:40 GMT-0700 (PDT)

I want to convert this to something like yyyy-MM-dd.

I tried this.

String date1 = "Fri May 31 2019 05:08:40 GMT-0700 (PDT)";
DateTimeFormatter f = DateTimeFormatter.ofPattern( "E MMM dd HH:mm:ss z uuuu" ).withLocale( Locale.US );

ZonedDateTime zdt = ZonedDateTime.parse( date1 , f );
LocalDate ld = zdt.toLocalDate();
DateTimeFormatter fLocalDate = DateTimeFormatter.ofPattern( "yyyy-MM-dd" );
String output = ld.format( fLocalDate ) ;

I am getting the error:

Exception in thread "main" java.time.format.DateTimeParseException: Text 
'Fri May 31 2019 05:08:40 GMT-0700 (PDT)' could not be parsed at index 13
at java.time.format.DateTimeFormatter.parseResolved0(Unknown Source)
at java.time.format.DateTimeFormatter.parse(Unknown Source)
at java.time.ZonedDateTime.parse(Unknown Source)
Ralf
  • 16,086
  • 4
  • 44
  • 68
AnanR
  • 29
  • 1
  • 7
  • 1
    The pattern of your `DateTimeFormatter` doesn't fit the format of your string. – MrPromethee Jun 06 '19 at 12:13
  • Can you tell me the exact pattern which will convert the Date in GMT format to IST – AnanR Jun 06 '19 at 12:19
  • As an aside your way of formatting into `yyyy-MM-dd` is overly complicated. You just need `String output = zdt.format(DateTimeFormatter.ISO_LOCAL_DATE);`. – Ole V.V. Jun 06 '19 at 14:34

3 Answers3

1

The patter of your formater is wrong. Year is missing ("yyyy") and timezone is not matching. To match it you need to use both z and Z you also need to add unmatched text for GMT, like "'GMT'Z (z)".

Try this:

"E MMM dd yyyy HH:mm:ss 'GMT'Z (z)"
Januson
  • 4,533
  • 34
  • 42
1

As you can see here you can use following pattern:

public static void main(String[] args) throws Exception {
    String date1 = "Fri May 31 2019 05:08:40 GMT-0700 (PDT)";
    DateTimeFormatter f = DateTimeFormatter.ofPattern( "EEE MMM dd yyyy HH:mm:ss 'GMT'Z '('z')'" ).withLocale( Locale.US );

    ZonedDateTime zdt = ZonedDateTime.parse( date1 , f );
    LocalDate ld = zdt.toLocalDate();
    DateTimeFormatter fLocalDate = DateTimeFormatter.ofPattern( "yyyy-MM-dd" );
    String output = ld.format( fLocalDate ) ;
    System.out.println(output);
}

Output:

2019-05-31

heiwil
  • 612
  • 4
  • 8
-1

Since you need only yyyy-MM-dd formatted date, try this code:

   String date1 = "Fri May 31 2019 05:08:40 GMT-0700";

   //this format is good enough to read required data from your String
   DateFormat df1 = new SimpleDateFormat("EEE MMM dd yyyy HH:mm:ss");

   //format you require as final output
   DateFormat df2 = new SimpleDateFormat("yyyy-MM-dd");

   //convert String to date ( with required attributes ) and then format to target
   System.out.println(df2.format(df1.parse(date1)));
TechFree
  • 2,600
  • 1
  • 17
  • 18
  • When java.time, the modern Java date and time API is asked about, please don’t bring on the long outdated and notoriously troublesome `SimpleDateFormat` class. – Ole V.V. Jun 06 '19 at 14:30
  • Thanks @Ole. Query was about converting String to the target format. The logic I put would work though. I think I missed something.. – TechFree Jun 06 '19 at 14:33
  • It’s not that your answer isn’t correct. I believe it is. It’s just poor advice (the tag also seems to suggest that the questioner wants to use `ZonedDateTime`, as s/he already tries, and which is a good idea). – Ole V.V. Jun 06 '19 at 14:36