开发者

Java time between 0 and 24

开发者 https://www.devze.com 2023-03-23 23:06 出处:网络
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.

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 tim开发者_JAVA百科e of 25:00 to be interpreted as 1 day and 1 hour).

Thank you.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消