0

I am trying to format date were is given in following format 05051988. I want to use LocalDate to format in next 05 may 1988.

I have create private method that i will use later in same class to input formatted date in text. I have already made some code but i at the moment I'm getting 1988-05-05 if I use MM, if I replace it with MMM then it is giving me error message that can not be parsed.

private LocalDate parseDate(){
    LocalDate dateOfBirth = LocalDate.parse(date, DateTimeFormatter.ofPattern("ddMMyyyy"));
    return dateOfBirth;
}

Error:

Exception in thread "main" java.time.format.DateTimeParseException: Text '05051988' could not be parsed at index 2
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
Ronin
  • 39
  • 2
  • 3
  • 10
  • What message did you get? – LHA Aug 07 '14 at 23:12
  • Exception in thread "main" java.time.format.DateTimeParseException: Text '05051988' could not be parsed at index 2 – Ronin Aug 07 '14 at 23:14
  • 1
    If you use MMM. The date input must be "05May1998". Not "05051988" – LHA Aug 07 '14 at 23:19
  • it is given as "05051988", i need to format it to "05 May 1988", but if i leave as MM then i am getting 1988-05-05 but when i change it to MMM then it is throwing me exception – Ronin Aug 07 '14 at 23:24
  • Idk what `date` is in that context but something tells me it'd be better to pass it as a parameter or if you don't want to do that then maybe you should consider not having a `parseDate()` method at all. Nothing to do with the problem, just suggestion – 0x6C38 Aug 07 '14 at 23:24
  • date is part of String were i have like name, last name, date of birth and place of birth, i had to create new array and split it so it shows me separate information about persons, now for date was given to formatted in to different format. – Ronin Aug 07 '14 at 23:31
  • I have no issue parsing a `String` of `05051988` using the code you provided. Consider providing a [runnable example](https://stackoverflow.com/help/mcve) which demonstrates your problem. This will result in less confusion and better responses – MadProgrammer Aug 08 '14 at 00:55
  • The string format which your using (05 may 1988) is incorrect. When ever we are using MMM as a date format, we have to pass the month value as 'May' instead of 'may'. – Rajesh Mar 10 '15 at 08:54

1 Answers1

3

You still need to use "ddMMyyyy" to parse the date. Then use "dd MMM yyyy" to format it when you print it.

Something like this:

String reformat(String dateOfBirth) {
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("ddMMyyyy");
    DateTimeFormatter formatter2 = DateTimeFormatter.ofPattern("dd MMM yyyy");
    LocalDate date = LocalDate.parse(dateOfBirth, fomatter);
    return formatter2.print(date);
}
Ted Bigham
  • 4,237
  • 1
  • 26
  • 31
  • I have try like this DateTimeFormatter formatter = DateTimeFormatter.ofPattern("ddMMyyyy"); DateTimeFormatter formatter2 = DateTimeFormatter.ofPattern("dd MMM yyyy"); LocalDate date = LocalDate.parse(dateOfBirth, fomatter); but then i dont know how to proceed and put it in return xxx; any suggestion? – Ronin Aug 07 '14 at 23:28
  • (a) Please add that code to your question. (b) LocalDate does not carry to format around with it. You'd have to be returning a string to do that. if that's what you want, then use "return formatter2.print(date);" – Ted Bigham Aug 07 '14 at 23:35
  • Sorry. may I ask you why did you put print in return, when i try it is giving me error message "cannot find symbol, symbol: method print(LocaDate), location: variable fomratter2 of type DateTimeFormatter. – Ronin Aug 07 '14 at 23:47
  • Sorry. I thought you were using the Joda library. I didn't realize those classes are available in java 8 now. For java.time.format.DateTimeFormatter you can probably use "return formatter2.format(date);" – Ted Bigham Aug 07 '14 at 23:54
  • [Never use SimpleDateFormat or DateTimeFormatter without a Locale](https://stackoverflow.com/a/65544056/10819573) – Arvind Kumar Avinash May 08 '21 at 16:44