Home > other >  how do I convert LocalDate to String form so it prints out the date inputed
how do I convert LocalDate to String form so it prints out the date inputed

Time:11-18

   public void displayDVD(DVD dvd) {
       if (dvd != null) {
           io.print(dvd.getTitle());
           io.print(dvd.getReleaseDate());
           io.print(dvd.getMpaaRating());
           io.print(dvd.getDirectorsName());
           io.print(dvd.getStudio());
           io.print(dvd.getUserRating());

I need to display the getReleaseDate, but it's telling me I can't because its a LocalDate and can't be a string. Probably a rookie look, but really can't find a way around it. Thanks in advance!

CodePudding user response:

Start with the Date Time Trail, in particular Parsing and Formatting

Something like...

LocalDate ld = LocalDate.now();
System.out.println(ld.format(DateTimeFormatter.ISO_LOCAL_DATE));

prints out...

2021-11-18 

CodePudding user response:

Try this.

public void displayDVD(DVD dvd) {
    if (dvd != null) {
        io.print(dvd.getTitle());
        io.print(dvd.getReleaseDate().toString());
        io.print(dvd.getMpaaRating());
        io.print(dvd.getDirectorsName());
        io.print(dvd.getStudio());
        io.print(dvd.getUserRating());

CodePudding user response:

You can overloading io.print or convert dvd.getReleaseDate() to a string. like this

private void print(LocalDate localDate) {
    System.out.println(localDate.toString());
    // use toString() or format
    System.out.println(format(localDate));
}

private String format(LocalDate localDate) {
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
    return formatter.format(localDate);
}

public void displayDVD(DVD dvd) {
    if (dvd != null) {
        io.print(dvd.getTitle());
        // format
        io.print(format(dvd.getReleaseDate()));
        io.print(dvd.getMpaaRating());
        io.print(dvd.getDirectorsName());
        io.print(dvd.getStudio());
        io.print(dvd.getUserRating());
    }
}
  • Related