1

I recently started using util.Date in Java, only to learn you cannot add/subtract days, and so I've now started to use LocalDate.

I have a web app that allows a user to enter a date in 'dd/MM/yyyy' format and it needs to be converted to 'yyyy-MM-dd'. The application also needs to throw an error if the entered date does not exist.

Below is a test application I'm using. It works, but wrongly allows dates like '31/02/2018'. I've tried adding '.withResolverStyle(ResolverStyle.STRICT)', but I get different errors.

package javaapplication1;

import java.text.ParseException;
import java.time.format.DateTimeFormatter;
import java.time.LocalDate;
import java.time.format.ResolverStyle;

public class JavaApplication1 {

    public static void main(String[] args) {

        LocalDate today = LocalDate.now();

        LocalDate date;

        try {
            String strDate = "31/2/09"; // Input from user
            System.out.println("Form: " + strDate);

            date = setDate(strDate, "d/M/yy");

            System.out.println("Data: " + convertDateToString(date, "yyyy-MM-dd")); // Convert format for insertting into database

            // If date is older than 1 year, output message
            if (date.isBefore(today.minusYears(1))) {
                System.out.println("Date is over a year old");
            }

            // If date is older than 30 days, output message
            if (date.isBefore(today.minusDays(30))) {
                System.out.println("Date is over 30 days old");
            }
        }
        catch (ParseException e) {
            System.out.println("Invalid date!");
            e.printStackTrace();
        }
    }

    private static LocalDate setDate(String strDate, String dateFormat) throws ParseException {

        DateTimeFormatter dtf = DateTimeFormatter.ofPattern(dateFormat).withResolverStyle(ResolverStyle.STRICT);

        //sdf.setLenient(false);

        LocalDate date = LocalDate.parse(strDate, dtf);

        return date;
    }

    private static String convertDateToString(LocalDate date, String dateFormat) {

        //DateTimeFormatter dtf = DateTimeFormatter.ofPattern(dateFormat);

        String strDate = date.toString();

        return strDate;
    }
}
smally
  • 453
  • 1
  • 4
  • 14
  • So the user should enter the date like `31/2/2018` and you want to throw an error when it's entered as `'31/02/2018`? – Kaan Burak Sener Apr 03 '18 at 21:48
  • Sorry no. An error should be thrown because there is no day 31 in February. 28/2/18 and 28/02/2018 should both be accepted without an error – smally Apr 03 '18 at 21:51
  • 1
    check this: https://stackoverflow.com/questions/226910/how-to-sanity-check-a-date-in-java – Kaan Burak Sener Apr 03 '18 at 21:52
  • Thanks. The answer for me was buried in there, but I found that I needed to throw DateTimeParseExeption instead, and use uu instead of yy for the year in my date format for some reason – smally Apr 03 '18 at 22:09
  • @smally Please post your solution as an Answer and accept it to close this Question. – Basil Bourque Apr 04 '18 at 06:27

1 Answers1

0

Problems were fixed using the DateTimeParseExeption instead.

And using 'uuuu' instead of 'yyyy' to represent the year is required for ResolverStyle.STRICT to work.

smally
  • 453
  • 1
  • 4
  • 14