Java DateTimeFormatter validateDate(String input, DateTimeFormatter formatter)

Here you can find the source of validateDate(String input, DateTimeFormatter formatter)

Description

validate Date

License

Open Source License

Declaration

public static LocalDate validateDate(String input, DateTimeFormatter formatter) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

public class Main {
    public static boolean validateDate(LocalDate date) {
        return !(date.getDayOfWeek().equals(DayOfWeek.SATURDAY) || date.getDayOfWeek().equals(DayOfWeek.SUNDAY));
    }//from w  w  w.j  av a2  s.c o  m

    public static LocalDate validateDate(String input) {
        return validateDate(input, DateTimeFormatter.ISO_LOCAL_DATE);
    }

    public static LocalDate validateDate(String input, DateTimeFormatter formatter) {
        if (!input.isEmpty()) {
            try {
                LocalDate date = LocalDate.parse(input, formatter);
                if (validateDate(date)) {
                    return date;
                }
            } catch (Exception e) {
                return null;
            }
        }
        return null;
    }
}

Related

  1. parse(String format, String datetime)
  2. parseAndFormat(String date, DateTimeFormatter fromFormatter, DateTimeFormatter toFormatter)
  3. parseDateTime(String dateTimeStr, String format)
  4. parseZoneDateTime(String value, DateTimeFormatter formatter)
  5. setDateFormat(String pattern)