0

In this code,

        String str="Sun Feb 07 00:27:16 CET 2021";
    SimpleDateFormat sdf=new SimpleDateFormat("E MMM dd HH:mm:ss z yyyy");
    try {
        java.util.Date date=sdf.parse(str);
        System.out.print(date.getTime());
    } catch (ParseException ex) {
        Logger.getLogger(NewClass.class.getName()).log(Level.SEVERE, null, ex);
    }

It shows GRAVE: null java.text.ParseException: Unparseable date: "Sun Feb 07 00:27:16 CET 2021" How to solve it plz!

4
  • 1
    Judging by your username I'd say your system's language may not be English, so you should pass a locale to the SimpleDateFormat constructor. Commented Feb 4, 2021 at 9:22
  • 2
    For those wondering: I think the OP’s system uses a French locale setting, and “GRAVE” translates to a “SEVERE” log level. Commented Feb 4, 2021 at 9:24
  • Yes, it utilizes french loc settings Commented Feb 4, 2021 at 9:27
  • 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 and DateTimeFormatter, both from java.time, the modern Java date and time API. Commented Feb 13, 2021 at 4:37

1 Answer 1

1

There are two problems with your code:

  1. Not using the correct format: you have used E instead of EEE
  2. Not using Locale: make it a habit to use the applicable Locale with date-time parsing/formatting API. Your date-time string is in English and therefore you should use an English-specific locale e.g. Locale.ENGLISH, Locale.US etc.

Correct code:

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

public class Main {
    public static void main(String[] args) throws ParseException {
        String str = "Sun Feb 07 00:27:16 CET 2021";
        SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy", Locale.ENGLISH);
        java.util.Date date = sdf.parse(str);
        System.out.print(date.getTime());
    }
}

Output:

1612654036000

The date-time API of java.util and their formatting API, SimpleDateFormat are outdated and error-prone. It is recommended to stop using them completely and switch to the modern date-time API.

Using the modern date-time API:

import java.text.ParseException;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;

public class Main {
    public static void main(String[] args) throws ParseException {
        String str = "Sun Feb 07 00:27:16 CET 2021";
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("EEE MMM dd HH:mm:ss z uuuu", Locale.ENGLISH);
        ZonedDateTime zdt = ZonedDateTime.parse(str, dtf);
        System.out.println(zdt);
        System.out.println(zdt.toInstant().toEpochMilli());
    }
}

Output:

2021-02-07T00:27:16+01:00[Europe/Paris]
1612654036000

Learn more about the modern date-time API from Trail: Date Time.

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.