1

I want to parse a date in this format: "Wed Aug 26 2020 11:26:46 GMT+0200" into a date. But I don't know how to do it. I tried this:

SimpleDateFormat parser = new SimpleDateFormat("EEE MMM dd yyyy HH:mm:ss z");
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
Date date = parser.parse(split[0]); //error line
String formattedDate = formatter.format(date);

I am getting this error: Unparseable date: "Wed Aug 26 2020 11:26:46 GMT+0200". Is my date format wrong? And if so could somebody please point me in the right direction?

2
  • 1
    Are you working on a System whose locale language setting isn't English? Because if you don't provide your SimpleDateFormat a locale it will get and use your Systems default locale, which might not be English and therefor unable to parse the English weekday names. Try to use new SimpleDateFormat("EEE MMM dd yyyy HH:mm:ss z", Locale.ROOT); or Locale.ENGLISH and see if the error persists Commented Aug 26, 2020 at 9:46
  • 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 OffsetDateTime and DateTimeFormatter, both from java.time, the modern Java date and time API. Commented Aug 26, 2020 at 16:41

1 Answer 1

2

I suggest you stop using the outdated and error-prone java.util date-time API and SimpleDateFormat. Switch to the modern java.time date-time API and the corresponding formatting API (java.time.format). Learn more about the modern date-time API from Trail: Date Time.

import java.time.OffsetDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;

public class Main {
    public static void main(String[] args) {
        // Given date-time string
        String dateTimeStr = "Wed Aug 26 2020 11:26:46 GMT+0200";

        // Parse the given date-time string to OffsetDateTime
        OffsetDateTime odt = OffsetDateTime.parse(dateTimeStr,
                DateTimeFormatter.ofPattern("E MMM d u H:m:s zX", Locale.ENGLISH));

        // Display OffsetDateTime
        System.out.println(odt);
    }
}

Output:

2020-08-26T11:26:46+02:00

Using the legacy API:

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

public class Main {
    public static void main(String[] args) throws ParseException {
        // Given date-time string
        String dateTimeStr = "Wed Aug 26 2020 11:26:46 GMT+0200";

        // Define the formatter
        SimpleDateFormat parser = new SimpleDateFormat("EEE MMM dd yyyy HH:mm:ss 'GMT'Z", Locale.ENGLISH);

        // Parse the given date-time string to java.util.Date
        Date date = parser.parse(dateTimeStr);
        System.out.println(date);
    }
}

Output:

Wed Aug 26 10:26:46 BST 2020
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.