-1

How can i have my date to show like 26-10-1994?

if i print this out it will show 1994-10-26 for some wierd reason. anyone have some tips on how to fix this? i've tried local_root but that didnt help either.

private String geboortedatum = "26-10-1994";
public LocalDate calculateAge() {
    DateTimeFormatter sdf = DateTimeFormatter.ofPattern("dd-MM-yyyy");
    LocalDate dob = LocalDate.parse(geboortedatum, sdf);
    System.out.println("dob = " + dob);
    return dob;
}
azro
  • 53,056
  • 7
  • 34
  • 70
  • 1
    use the formatter to convert the `LocalDate` to a `String`. – luk2302 Nov 05 '18 at 22:21
  • You *did* "give the right datetimepattern to your `LocalDate.parse`". What you failed to do was to give the right datetimepattern to `LocalDate.format`, instead of (implicitly) calling `LocalDate.toString`. – Andreas Nov 05 '18 at 22:27

1 Answers1

2

The basic toString() of LocalDate prints this out as 1994-10-26, you need to use a formatter to have the pattern you want

public LocalDate calculateAge() {
    DateTimeFormatter sdf = DateTimeFormatter.ofPattern("dd-MM-yyyy");
    LocalDate dob = LocalDate.parse(geboortedatum, sdf);
    String dateFormatted = dob.format(sdf);
    System.out.println("dob = " + dateFormatted);
    return dob;
}
azro
  • 53,056
  • 7
  • 34
  • 70