0
public static String Date() throws IOException
{
    URL url = new URL(target);
    URLConnection conn = url.openConnection();

    Map headers = conn.getHeaderFields();
    Set<String> keys = headers.keySet();
    String gmtTime = conn.getHeaderField("Date");
    gmtTime = gmtTime.replace("GMT", "").trim();
    SimpleDateFormat format = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss");
    java.util.Date date = null;
    try {
        System.out.println(gmtTime);
        date = format.parse(gmtTime); 
    } catch (ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return gmtTime;
}

Tue, 26 May 2020 07:08:22
java.text.ParseException: Unparseable date: "Tue, 26 May 2020 07:08:22"
at java.base/java.text.DateFormat.parse(DateFormat.java:395)
at ex.Time.Date(Time.java:34)
at ex.ServerTime_CMW.draw(ServerTime_CMW.java:35)
at processing.core.PApplet.handleDraw(PApplet.java:2482)
at processing.awt.PSurfaceAWT$12.callDraw(PSurfaceAWT.java:1547)
at processing.core.PSurfaceNone$AnimationThread.run(PSurfaceNone.java:313)

I don't know why ParseException occurs... My date string is "Tue, 26 May 2020 07:08:22" And the format what i think is "EEE, dd MMM yyyy HH:mm:ss"

홍윤표
  • 31
  • 4
  • 5
    Wild guess: in your locale Tuesday is not "Tue"? – Federico klez Culloca May 26 '20 at 07:29
  • https://repl.it/repls/HungryExcitingBoolean Also I'd suggest to use java.time DateTimeFormatter, LocalDateTime, there easier to use – azro May 26 '20 at 07:30
  • SimpleDateFormat format = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss", Locale.ENGLISH); Works Done!! Thanks!!!! – 홍윤표 May 26 '20 at 07:31
  • At this point I'd like to know how "May" is written in your language, since it seems to work. – Federico klez Culloca May 26 '20 at 07:33
  • Welcome to Stack Overflow. Please search for already existing questions and answers on the site before posting a new question. Note, your problem has been asked many times already and was already answered by [Java - Unparseable date](https://stackoverflow.com/questions/6154772/java-unparseable-date). Have a look at [How do I ask a good question?](https://stackoverflow.com/help/how-to-ask) – Ivo Mori May 26 '20 at 07:46
  • 1
    Also have a look at this answer frrom [Java - Unparseable date](https://stackoverflow.com/questions/6154772/java-unparseable-date) to see how to do this without using the obsolete, outdated Java APIs: https://stackoverflow.com/a/44017483/5698098 – Ivo Mori May 26 '20 at 07:53
  • 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.RFC_1123_DATE_TIME`, both from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. May 26 '20 at 17:44

1 Answers1

1

dd MMM yyyy won't work in all Locales. In the US, yes. In Canada, no ... that is not a legal parse format in Canada. If you were using the new time library that replaces the antiquated java.util.Date classes, then, you could escape your Locale by passing Locale.Root as a second argument to the format call.

EDIT: In Canada, I had to format dd MMM yyyy from a bank pdf, and I had to leave my default Locale to do it, and used this. Java 8 time, of course though. The polymorph is so my other date calls work, without having to pass a Locale. I can pass the other method Locale.ROOT to obtain the elusive 'dd MMM yyyy' format.

    public static LocalDate parseDate( final String format, final String s ) {
        return parseDate( format, s, java.util.Locale.getDefault() );
    }

    public static LocalDate parseDate( final String format, final String s, final Locale loc ) {

        final DateTimeFormatter df = DateTimeFormatter.ofPattern( format, loc );
        LocalDate ld;   // Check if this was a legal LocalDate.
        try {
            ld = LocalDate.parse(s, df);
        } catch (java.time.format.DateTimeParseException pe) {
            //System.out.println( pe.getMessage() );
            ld = null;  // This will signal an error
        }
        return ld;
    }
Lotsa
  • 412
  • 4
  • 11
  • 2
    Just a hint. Before writing up a new answer, and investing time, it's worth checking whether the question was already asked and answered. See for example [Java - Unparseable date](https://stackoverflow.com/questions/6154772/java-unparseable-date). – Ivo Mori May 26 '20 at 07:55
  • I guess I better figure out how to link other questions pretty quick then. Since this was the answer to my question about this just a few days ago. I could have just linked my own question. And by the way, no search, when you didn't know the answer, or what the problem was, located any info about this ... anywhere. That's why I asked it a few days ago too. I always search first. – Lotsa May 26 '20 at 08:01