SimpleDateFormat返回奇怪的日期与无效的日期字符串 [英] SimpleDateFormat returns strange date with invalid date string

查看:192
本文介绍了SimpleDateFormat返回奇怪的日期与无效的日期字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试解析时间拼写字符串,现在当我解析字符串2014-01-07 95:25:37时,它返回一个日期,而不是抛出ParseException。谁知道如何让它抛出ParseException。谢谢乔治。

I try to parse time formating string ,now when I parse string "2014-01-07 95:25:37" , it return a date ,not throw ParseException . who know how to let it throw ParseException . Thanks George .

SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

System.out.print(  ft.parse("2014-01-07 95:25:37"));

输出:

Fri Jan 10 23:25:37 EST 2014


推荐答案

问题的原因是时间:95:25:37。 Java尝试将这个额外的时间添加到您的日子,因此您将获得额外的3天。

The cause of the problem is the time : 95:25:37 . Java tries to add this extra time to your days ,hence you are get additional 3 days.

为了防止java这样做,您可以使用以下内容:

To prevent java from doing so, you can use the following :

 ft.setLenient(false);

这篇关于SimpleDateFormat返回奇怪的日期与无效的日期字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆