0

I am getting the above exception while trying to parse. I tried the following date format,

SimpleDateFormat sdf = new SimpleDateFormat("E, dd MMM  yyyy  HH:mm:ss ", Locale.ENGLISH);
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
AJN
  • 261
  • 2
  • 3
  • 15
  • 2
    You seem to have some extra spaces in the format, before and after the `yyyy`. – Sweeper Dec 08 '17 at 07:27
  • Extra space should not be problem. I am getting output - `Fri, 08 Dec 2017 12:59:11` – vinS Dec 08 '17 at 07:30
  • Can you please share the format if it is different from me please. Thank you. @vinS – AJN Dec 08 '17 at 07:31
  • `SimpleDateFormat sdf = new SimpleDateFormat("E, dd MMM yyyy HH:mm:ss ", Locale.ENGLISH); System.out.println(sdf.format(new Date()));` I have run this one. Edit - Putting this in code block in comment here, is truncating extra space. – vinS Dec 08 '17 at 07:32
  • 3
    I recommend you drop the old and troublesome `SimpleDateFormat` and use `java.time`, the modern Java date and time API, instead. – Ole V.V. Dec 08 '17 at 07:33
  • Thanks @vinS. I think space was the problem. Thank you – AJN Dec 08 '17 at 07:34
  • Thank you @Sweeper – AJN Dec 08 '17 at 07:35
  • Yeah, I will try using java.time this time sure @Ole V.V – AJN Dec 08 '17 at 07:35
  • what exception you get? – Amol Raje Dec 08 '17 at 07:52
  • `java.time` version: `LocalDateTime.parse("Thu, 7 Dec 2017 07:40:40 ", DateTimeFormatter.ofPattern("E, d MMM yyyy HH:mm:ss ", Locale.ENGLISH))`. Note: only one `d` and only one space before and after `yyyy` (`SimpleDateFormat` accepts `dd` but also fails to parse when you have extra spaces before and after `yyyy`). – Ole V.V. Dec 08 '17 at 09:12
  • Just curious, why have you got an extra space at the end of your date-time string, after `40`? Did you strip off a time zone?? – Ole V.V. Dec 08 '17 at 09:13

2 Answers2

1

java.time

The SimpleDateFormat class is not only long outdated, it is also notoriously troublesome. I recommend you stop using it and use java.time the modern Java date and time API also known as JSR-310, instead. It is so much nicer to work with.

    System.out.println(LocalDateTime.parse("Thu, 7 Dec 2017 07:40:40 ",
            DateTimeFormatter.ofPattern("E, d MMM yyyy HH:mm:ss ", Locale.ENGLISH)));

This prints the expected date and time:

2017-12-07T07:40:40

What went wrong

In your format pattern string, you’ve got two spaces before and two spaces after yyyy, where it seems that in your date-time string there is only one space in each of those places. While SimpleDateFormat is infamous for parsing strings that it ought to reject, it does object in this case by throwing the ParseException the message of which you quote in the question title.

If you compare my format pattern string to yours, you will notice I use just one d where you use two. SimpleDateFormat parses 7 with dd where the modern classes are stricter: d matches a date-of-month of either 1 or 2 digits. where dd requires two digits. You may of course exploit this for stricter validation if you need it.

Question: Can I use the modern API with my Java version?

If using at least Java 6, you can.

For learning to use java.time, see the Oracle tutorial or find other resoureces on the net.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
0

it seems working fine with space as well...what exception you get?

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
public class SimpleDateFormatExample {
 public static void main(String[] args) {
  SimpleDateFormat sdf = new SimpleDateFormat("E, dd MMM yyyy  HH:mm:ss ",Locale.ENGLISH);
   String strDate= sdf.format(new Date());
   System.out.println(strDate);
 }
}

Ouput:Fri, 08 Dec 2017 07:54:08

Amol Raje
  • 928
  • 3
  • 9
  • 16
  • The OP was (quote) “trying to parse”. Furthermore, “it is working for me” usually isn’t considered a valid answer, rather put it in a comment. – Ole V.V. Dec 08 '17 at 08:53
  • See [Are “Your code works fine for me” answers acceptable?](https://meta.stackoverflow.com/questions/277923/are-your-code-works-fine-for-me-answers-acceptable) – Ole V.V. Dec 08 '17 at 09:14
  • Don't know but it gave the same exception when tried with spaces. I updated the format as SimpleDateFormat sdf = new SimpleDateFormat("E, dd MMM yyyy hh:mm:ss ", Locale.ENGLISH); and it worked – AJN Dec 08 '17 at 09:21
  • @Ole V.V ..i am not saying the code working for me ..i want to say there is no problem with extra space .. – Amol Raje Dec 08 '17 at 10:19
  • @AmolRaje, parsing does *not* work with the extra spaces in the format pattern string. At least not on my computer. – Ole V.V. Dec 08 '17 at 10:32
  • see:https://stackoverflow.com/questions/5564570/why-does-javas-simpledateformat-parse-this – Amol Raje Dec 08 '17 at 10:45
  • see:https://stackoverflow.com/questions/5879546/parsing-dates-with-variable-spaces – Amol Raje Dec 08 '17 at 10:46
  • I don't think it should parse this because of the spaces ..but the `SimpleDateFormat` is returning the date even there is extra space – Amol Raje Dec 08 '17 at 10:48
  • I didn’t test the examples you are linking to. I *did* test the `SimpleDateFormat` from your answer with the date-time string from the question title. I got `Unparseable date: "Thu, 7 Dec 2017 07:40:40 "`. The error offset of the exception is 16. Offset 16 is where the time is in the string after only one space where your format pattern string has two spaces. – Ole V.V. Dec 08 '17 at 11:32