0

I have a problem here, the format of the date was given (dd-MM-YYYY) and it's a not a good format (I know) but that was the raw data, and I can't parse the string using SimpleDateFormat class, it gives an Error

java.text.ParseException: Unparseable date: "27-08-2019"

And I also use the DateTimeFormatter but again it can't be parse, this is the error.

java.time.format.DateTimeParseException: Text '19-08-2019' could not be parsed at index 0

So my working option is to format first the String so that the Date Formatter can parse the string date, but if there are other option to minimize manipulation of data in String format will be nice. Thanks.

EDIT

Sorry for not posting the code. This the code using the SimpleDateFormat

String string = "08-27-2019";
DateFormat format = new SimpleDateFormat("yyyy/MM/dd", Locale.ENGLISH);
Date date = format.parse(string);
System.out.println(date);

And here using the DateTimeFormatter and LocalDate.

String string = "08-27-2019";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd", Locale.ENGLISH);
LocalDate date = LocalDate.parse(string, formatter);
System.out.println(date);
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
Francis G
  • 1,040
  • 1
  • 7
  • 21
  • 1
    what does `YYYY` mean? – Scary Wombat Aug 27 '19 at 05:31
  • 1
    You should include your Java code, but if you poke around this site, you fill already find dozens (hundreds?) of other questions which can probably already answer your own question. – Tim Biegeleisen Aug 27 '19 at 05:33
  • 1
    Also use JSR310 – Scary Wombat Aug 27 '19 at 05:33
  • I've been searching and still searching, In this thread https://stackoverflow.com/questions/4216745/java-string-to-date-conversion/4216767#4216767, I got the Idea using `DateTimeFormatter` and I got it working when I change the raw data first before parsing – Francis G Aug 27 '19 at 05:37
  • here you may be get your answer. 1.https://stackoverflow.com/questions/30858866/why-java-date-is-not-parsable 2.https://stackoverflow.com/questions/2009207/java-unparseable-date-exception – Jaber Bin Ahamed Aug 27 '19 at 05:39
  • 1
    @Francisaskquestion - see the Java8 update on the answer that you linked to. – Scary Wombat Aug 27 '19 at 05:42
  • thanks @jaber but in the raw data there are no hours and minutes just Date, So i might not work. I could manipulate the String but that will be my last option – Francis G Aug 27 '19 at 05:44
  • I'm currently experimenting on that – Francis G Aug 27 '19 at 05:45
  • Without seeing the code you're trying to use to parse, we can't really help you improve it. Please post a [mcve]. – Jon Skeet Aug 27 '19 at 05:46
  • Sorry to add the code late – Francis G Aug 27 '19 at 05:50
  • 5
    Your format string is "yyyy/MM/dd" - that isn't the same as `dd-MM-yyyy` at all; it uses different separators, and has the year first instead of the day. The format string you pass to `SimpleDateFormat` has to match the data you're trying to parse. – Jon Skeet Aug 27 '19 at 05:52
  • @JonSkeet thanks for pointing that out, I don't know separators matters in parsing. So if I parse it first with the format `dd-MM-yyyy` then changing it format again since it is now a `Date` type. Thanks – Francis G Aug 27 '19 at 05:57
  • `System.out.println(date);` - what are you hoping this to do? – Scary Wombat Aug 27 '19 at 06:01
  • Sorry about that, just printing the raw date. – Francis G Aug 27 '19 at 06:02
  • What do you mean by "the raw date"? If you want to convert a `Date` to a specific format, use `SimpleDateFormat` again. But I'd *strongly* encourage you to use java.time instead, if at all possible. – Jon Skeet Aug 27 '19 at 06:29
  • @JonSkeet do you mean "the raw date" I was pointing on my comment? if so that was the parsed date. – Francis G Aug 27 '19 at 06:48
  • @Francisaskquestion: Well you've parsed it to a `Date`. Printing out a `java.util.Date` might not do what you expect... which is why I questioned the use of "the raw date" in your comment. If you expect it to print just 2019-08-27 for example, then that would work for `LocalDate` (I believe) but not `Date`. – Jon Skeet Aug 27 '19 at 07:36
  • Thanks for pointing that out, should I include the code to get the exact format as string/date? – Francis G Aug 27 '19 at 09:46

1 Answers1

1

Thanks for all the people help me, I got it working after pointing out my mistakes. I just parse it with the format given then format the parse Date to the format I want to have.

        String string = "27-08-2019";
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy", Locale.ENGLISH);
        LocalDate date = LocalDate.parse(string, formatter);
        System.out.println(DateTimeFormatter.ofPattern("yyyy-MM-dd", Locale.ENGLISH).format(date));

If there are more pointers to my solution I'm gladly accept any correction. Thanks

Francis G
  • 1,040
  • 1
  • 7
  • 21
  • 1
    Thanks for posting your working solution. It is possible to change the 3rd line to just `LocalDate date = LocalDate.parse(string, formatter);`. The last line can be simplified to `System.out.println(date);` or `System.out.println(date.toString());`. – Ole V.V. Aug 27 '19 at 16:34
  • Ow sorry about that, I didn't review the source and just copy paste it, hahaha the first `DateTimeFormatter` instance was never used, Thanks – Francis G Aug 28 '19 at 00:33