Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java time between 0 and 24

I'm using a SimpleDateFormatter to parse a date/time. The problem is that if the time I insert is bigger than 24 it passes to the next day.

How can I do a date/time validation with strict time (between 0 and 24 -- I don't want a time of 25:00 to be interpreted as 1 day and 1 hour).

Thank you.

like image 410
Sergiu Avatar asked Jul 02 '26 08:07

Sergiu


2 Answers

setLenient(false)

Should do what you want.

DateFormat.setLenient(boolean);

This will throw an Unparseable date: "25:00" exception:

final DateFormat formatter = SimpleDateFormat.getTimeInstance(DateFormat.SHORT);
formatter.setLenient(false);
formatter.parse("25:00");

lenient by default is true.

like image 20
Tomasz Nurkiewicz Avatar answered Jul 04 '26 22:07

Tomasz Nurkiewicz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!