2

I'm having some difficulty finding the right way to parse a date.

I receive the date as a String in the following format: '2018-10-18 00:00:00'

I need to convert it to 18/10/2018 and store in a variable startDate

I then need a new variable to hold an endDate variable so roll the date forward by a week.

My code:

public String getStartDate(String startDate){

    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy");

    LocalDate localStartDate = LocalDate.parse(startDate, formatter);

    String startDateFormatted = localStartDate.format(formatter);

    return startDateFormatted;
}

public LocalDate getEndDate(String startDate) {
    LocalDate localEndDate = LocalDate.parse(getStartDate(startDate)).plusDays(7);
    return localEndDate;
}

My error is:

java.time.format.DateTimeParseException: Text '2018-10-18 00:00:00' could 
not be parsed at index 4

Index 4 suggests the '-' char. Not sure the formatter pattern for removing the ISO time format that's in the original String

I'm wading through the Javadocs now but can anyone tell me how I can fix?

Steerpike
  • 1,712
  • 6
  • 38
  • 71
  • 1
    First you need to parse it with the correct formatter and then format it to your desired end format. – jrsall92 Sep 12 '18 at 15:58

2 Answers2

4

Your input format is wrong. Try this:

public String getStartDate(String startDate)
{
    DateTimeFormatter inputFormat = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    DateTimeFormatter outputFormat = DateTimeFormatter.ofPattern("dd/MM/yyyy");

    return LocalDate.parse(startDate, inputFormat).format(outputFormat);
}
dustytrash
  • 1,568
  • 1
  • 10
  • 17
  • that k you @dustytrash, seems obvious now you point it out but I accepted the first answer already. – Steerpike Sep 13 '18 at 07:47
  • @Steerpike But my answer was 2 minutes and 29 seconds before his! Just kidding, that's fine. Glad you got it working! – dustytrash Sep 13 '18 at 13:08
  • Better. You need uppercase `HH` for hour of day from 00 through 23, though. Lowercase `hh` is for hour within AM or PM (01–12) and won’t work without an AM/PM marker, also won’t accept the `00` in the asker’s string. – Ole V.V. Sep 13 '18 at 16:37
  • @OleV.V. askers input string was `2018-10-18 00:00:00` – dustytrash Sep 13 '18 at 16:42
  • Yes, exactly, that’s what I mean. :-) The hours in that string are `00`, which cannot be parsed with lowercase `hh`. – Ole V.V. Sep 13 '18 at 16:42
  • @OleV.V. Oh I see what you mean. I'll edit my answer to HH. But I want to note that I tested with the input string, and it worked as expected, – dustytrash Sep 13 '18 at 17:38
4

You need two formatters. One for the input and one for the output:

public String getStartDate(String startDate) {
    DateTimeFormatter inputFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    DateTimeFormatter outputFormatter = DateTimeFormatter.ofPattern("dd/MM/yyyy");
    LocalDate localStartDate = LocalDate.parse(startDate, inputFormatter);
    String startDateFormatted = localStartDate.format(outputFormatter);
    return startDateFormatted;
}
Michael
  • 2,443
  • 1
  • 21
  • 21