Im trying to check the format of the Date.
Implementation: Get Date format yyyy-MM-dd
then if i input beyond month 12 and day 30 or 31 depend on the month it must show error.
The pattern will work if I manually setup a date in String, but the date must come from Date to String but the Date will auto adjust.
Try the code here: https://www.programiz.com/java-programming/online-compiler/
import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.time.LocalDate; import java.time.ZoneId; import java.time.format.DateTimeFormatter; import java.time.format.DateTimeParseException; import java.time.temporal.TemporalAccessor; import java.util.Calendar; import java.util.Date; import java.time.Instant; import java.util.GregorianCalendar; import java.time.LocalDateTime; import java.util.regex.Matcher; import java.util.regex.Pattern; class HelloWorld { public static void main(String[] args) { Date date = new GregorianCalendar(2015, 11, 33).getTime(); SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd"); formatter.setLenient(false); String strDate = formatter.format(date); Pattern dateFrmtPtrn = Pattern.compile( "((19|20)\d\d)" + "-" + "(0?[1-9]|1[012])" + "-" +"(0?[1-9]|[12][0-9]|3[01])" ); Matcher mtch = dateFrmtPtrn.matcher(strDate); if(mtch.matches( )){ System.out.println(strDate); }else{ System.out.println("Error"); } } } //Must Error //Output: 2016-01-02
The manual with String only. This will work.
import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.time.LocalDate; import java.time.ZoneId; import java.time.format.DateTimeFormatter; import java.time.format.DateTimeParseException; import java.time.temporal.TemporalAccessor; import java.util.Calendar; import java.util.Date; import java.time.Instant; import java.util.GregorianCalendar; import java.time.LocalDateTime; import java.util.regex.Matcher; import java.util.regex.Pattern; class HelloWorld { public static void main(String[] args) { // String datey = "2023-12-32"; String datey = "2023-13-32"; Pattern dateFrmtPtrn = Pattern.compile( "((19|20)\d\d)" + "-" + "(0?[1-9]|1[012])" + "-" +"(0?[1-9]|[12][0-9]|3[01])" ); Matcher mtch = dateFrmtPtrn.matcher(datey); if(mtch.matches( )){ System.out.println(datey); }else{ System.out.println("Error"); } } } //working //Output: Error
Advertisement
Answer
If you switched to a java.time.LocalDate
, you could use the Exception
s that get thrown when you try to initialize a LocalDate
with invalid values:
Let’s say you just want to create a LocalDate
with given int
values for year, month of year and day of month. You could try
to initialize one and print the error message of the DateTimeException
thrown.
e.g. the following code will print an error:
try { // try to create December 32. 2022 LocalDate invLocDat = LocalDate.of(2022, 12, 32); System.out.println("Created LocalDate: " + invLocDat); } catch (DateTimeException dte) { // print what the exception says System.err.println(dte.getMessage()); }
The message printed will be
Invalid value for DayOfMonth (valid values 1 - 28/31): 32
You could find out an invalid month of year, too, e.g. LocalDate.of(2022, 13, 31)
would cause the above code to print
Invalid value for MonthOfYear (valid values 1 - 12): 13
And a valid input would simply print the toString()
method of the successfully created LocalDate
. Use a DateTimeFormatter
if you want different output.