1

The full date received from the request is of this format

Wed Mar 11 2020 05:29:01 GMT+0100 (West Africa Standard Time)

Now I substringed it to this - Wed Mar 11 2020

date.substring(0,15))

To enable me to save the date, I am parsing it as below

SimpleDateFormat format = new SimpleDateFormat("YYYY-MM-dd", Locale.ENGLISH);
Date parsedDate = null;
try {
    parsedDate = format.parse(date);
} catch (ParseException e) {
    e.printStackTrace();
}

When the code is ran, I get below error

java.text.ParseException: Unparseable date: "Wed Mar 11 2020"

I have also tried parsing with

SimpleDateFormat format = new SimpleDateFormat("EE MMM dd HH:mm:ss z yyyy",
                Locale.ENGLISH);

based on SO answers and I am still getting date parsing error.

How can I parse this date - date.substring(0,15))

Nick
  • 138,499
  • 22
  • 57
  • 95
Blaze
  • 2,269
  • 11
  • 40
  • 82
  • 4
    `"Wed Mar 11 2020"` does not match `YYYY-MM-dd` – Scary Wombat Mar 12 '20 at 05:50
  • with this - "dd-MM-YYYY", Locale.ENGLISH do the trick, justed tested that and it does not – Blaze Mar 12 '20 at 05:51
  • Hope this helps. https://stackoverflow.com/a/36310118/5201238 – Sam Mar 12 '20 at 05:53
  • will that suite this - Wed Mar 11 2020 @Sam – Blaze Mar 12 '20 at 05:55
  • try doing this 1) Parse the date with this format ("EEE MMM dd yyyy HH:mm:ss") 2) Format the data with this format ("YYYY-MM-dd") – Pulkit Mar 12 '20 at 06:03
  • 1
    I recommend you don’t use `SimpleDateFormat` and `Date`. Those classes are poorly designed and long outdated, the former in particular notoriously troublesome. Instead use `ZonedDateTime` or `OffsetDateTime` and in any case `DateTimeFormatter`, all from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). Also I see no reason for your substring operation (and it’s fragile unless you are very sure that all month and day abbreviations will always be exactly 3 chars and day of month always 2 digits). Why not just parse the entire original string? – Ole V.V. Mar 12 '20 at 09:24

4 Answers4

1

The format you need to match your date is EEE MMM dd yyyy e.g.

String date = "Wed Mar 11 2020 05:29:01 GMT+0100 (West Africa Standard Time)";
date = date.substring(0,15);
SimpleDateFormat format = new SimpleDateFormat("EEE MMM dd yyyy");
Date parsedDate = null;
try {
    parsedDate = format.parse(date);
} catch (Exception e) {
    e.printStackTrace();
}     
System.out.println(parsedDate);
SimpleDateFormat outformat = new SimpleDateFormat("yyyy-MM-dd");
System.out.println(outformat.format(parsedDate));

Output:

Wed Mar 11 00:00:00 CET 2020
2020-03-11
Nick
  • 138,499
  • 22
  • 57
  • 95
  • if I want to get the date back to a string in this format 2020-03-08, can u modify your solution a bit to that – Blaze Mar 12 '20 at 06:23
  • @Blaze see my edit, you can use an output formatter in the same way – Nick Mar 12 '20 at 06:29
1

Three points:

  1. Do use java.time, the modern Java date and time API, for your date and time work.
  2. Rather than taking a substring of the string you receive, I’d prefer to parse the entire string.
  3. Your format pattern string must match the string you are trying to parse (and vice versa). Exactly.

In code:

DateTimeFormatter formatter = 
        DateTimeFormatter.ofPattern(
            "EEE MMM d uuuu HH:mm:ss 'GMT'xx (zzzz)",  // Pattern to match your input strings.
            Locale.UK                                  // Locale determines human language used to parse name of month and such.
        )
;
String dateString = "Wed Mar 11 2020 05:29:01 GMT+0100 (West Africa Standard Time)";
ZonedDateTime zonedDateTime = ZonedDateTime.parse(dateString, formatter);

Generate a string.

System.out.println( zonedDateTime.toString() );

Output from this snippet is:

2020-03-11T03:29:01+01:00[Africa/Lagos]

Use java.time. The modern API is sol much nicer to work with. The Date class that you used is poorly designed, and SimpleDateFormat notoriously troublesome. Don’t use any of those.

Parse the entire string. Taking a substring of length 15 will cause some readers of your code to wonder, some ask “WTF?”, some to use their precious time for counting to make sure that 15 is the correct length. Also taking a substring of length 15 is fragile unless you’re sure that the abbreviations for day of week and for month always have length three and day of month is always written with two digits (May 02, not May 2). Furthermore it’s easier to parse more than you need and throw information away later, than to parse just what you think you need and later discover that you needed one more bit.

Specifying the format. Since your string begins with a day of week abbreviation, you need a format pattern string that begins with the format pattern letter for day of week. In this case EEE (or E or EE) for the abbreviation (EEEE would have meant the day written in full, like Wednesday). So YYYY-MM-dd is all wrong. EE MMM dd HH:mm:ss z yyyy comes closer and can parse day of week, month and day of month. Then comes a space and a year in the input, but your format pattern string has yyyy for year at the end instead, so this is where parsing breaks for you. If writing the correct format pattern string teases (as it does for many), a trick is to try something and first use the formatter for formatting a date and time. If the result differs from the string we would like to parse, it usually gives us a hint about what’s wrong.

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

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
0

Try this.

    SimpleDateFormat sdf3 = new SimpleDateFormat("EEE MMM dd yyyy");
    Date d1 = null;
    try {
        d1 = sdf3.parse("Wed Mar 30 2016");
    } catch (Exception e) {
        e.printStackTrace();
    }

    System.out.println(sdf3.format(d1));

}

Output:

Wed Mar 30 2016
Sam
  • 513
  • 1
  • 11
  • 27
  • 1
    These terrible date-time classes were supplanted years ago by the modern *java.time* classes, as of the unanimous adoption of JSR 310. – Basil Bourque Mar 13 '20 at 04:00
0

First you need to control your computer language. According to your computer's language. You must write day's name and month's name in your computer's language

Actually you had better convert this 'Wed' according to your language.

My computer's language is turkish. I used below code how to use day's name on date on java.

public static void main(String[] args) { 
    String sDate1 = "Per Mar-11-2020";  
    String sDate2 = "Perşembe Mar-11-2020";  
    Date date1;
    try {
        date1 = new SimpleDateFormat("EEE MMM-dd-yyyy").parse(sDate1);
        System.out.println(sDate1+"\t\t"+date1);
        date1 = new SimpleDateFormat("EEEEE MMM-dd-yyyy").parse(sDate2);
        System.out.println(sDate2+"\t\t"+date1);
    } catch (ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }       
}

output :

Per Mar-11-2020     Wed Mar 11 00:00:00 AST 2020
Perşembe Mar-11-2020        Wed Mar 11 00:00:00 AST 2020

Also you need to be carefull such as :

if your String is Per Mar-11-2020, you need to write EEE MMM-dd-yyyy.

Or

if your String is Per Mar 11 2020, you need to write EEE MMM dd yyyy.

Or

if your String is Per Mar/11/2020, you need to write EEE MMM/dd/yyyy.