使用严格格式将字符串转换为Date(convert string to Date using strict format)

编程入门 行业动态 更新时间:2024-10-12 08:26:53
使用严格格式将字符串转换为Date(convert string to Date using strict format)

我需要将一个String转换为具有确切格式的日期(dd / MM / yyyy)我写了这个方法:

public static Date toDate(String input, String format) throws DateConverterException { if (StringUtils.isEmpty(input) || StringUtils.isEmpty(format)) { throw new DateConverterException (String.format("Input %s or format %s is empty or", input, format)); } SimpleDateFormat sdf = new SimpleDateFormat(format); sdf.setLenient(false); Date date = null; try { date = sdf.parse(input); } catch (ParseException e) { throw new DateConverterException (e); } return date; }

因此,如果输入日期为“06/10 /”,则抛出异常=良好但如果输入日期为“06/10/1”,则构建日期,尽管格式不受尊重

任何帮助将不胜感激,非常感谢

I need convert a String to Date with exact format ( dd/MM/yyyy) I wrote this method :

public static Date toDate(String input, String format) throws DateConverterException { if (StringUtils.isEmpty(input) || StringUtils.isEmpty(format)) { throw new DateConverterException (String.format("Input %s or format %s is empty or", input, format)); } SimpleDateFormat sdf = new SimpleDateFormat(format); sdf.setLenient(false); Date date = null; try { date = sdf.parse(input); } catch (ParseException e) { throw new DateConverterException (e); } return date; }

So if input date is "06/10/" , exception is thrown = good but if input date is "06/10/1" date is constructed despite the format is not respected

Any help would be appreciated Thank you very much

最满意答案

是的,我很抱歉,我把0作为一个例子但确实抛出异常。现在用06/10/1或06/10/2并且它将构建Date。

从文档 :

年份 :如果格式化程序的日历是公历,则应用以下规则。

....

对于解析,如果模式字母的数量大于2,则无论数字位数如何,都按字面解释年份。 所以使用“MM / dd / yyyy”模式,“01/11/12”解析到公元12年1月11日

对于使用缩写年份模式(“y”或“yy”)进行解析,SimpleDateFormat必须解释相对于某个世纪的缩写年份。 它通过将日期调整为创建SimpleDateFormat实例之前的80年和20年之后来实现此目的。 例如,使用1997年1月1日创建的“MM / dd / yy”模式和SimpleDateFormat实例,字符串“01/11/12”将被解释为2012年1月11日,而字符串“05/04 / 64“将被解释为1964年5月4日。在解析期间,只有由Character.isDigit(char)定义的由两个数字组成的字符串才会被解析为默认世纪。 字面上解释任何其他数字字符串,例如一位数字符串,三位或更多位数字符串,或不是所有数字的两位数字符串(例如,“ - 1”)。 所以使用相同的模式解析“01/02/3”或“01/02/003”,如公元1月2日。 同样,“01/02 / -3”被解析为公元前1月2日。

(我的重点)

例如, 1是有效年份。 如果您想在一定范围内(例如1800-2100)接受年份,则需要在解析之后检查日期。

Yeah my bad sorry , i took 0 as an example but exception is thrown indeed .try now with 06/10/1 or 06/10/2 and it will construct Date.

From the documentation:

Year: If the formatter's Calendar is the Gregorian calendar, the following rules are applied.

....

For parsing, if the number of pattern letters is more than 2, the year is interpreted literally, regardless of the number of digits. So using the pattern "MM/dd/yyyy", "01/11/12" parses to Jan 11, 12 A.D.

For parsing with the abbreviated year pattern ("y" or "yy"), SimpleDateFormat must interpret the abbreviated year relative to some century. It does this by adjusting dates to be within 80 years before and 20 years after the time the SimpleDateFormat instance is created. For example, using a pattern of "MM/dd/yy" and a SimpleDateFormat instance created on Jan 1, 1997, the string "01/11/12" would be interpreted as Jan 11, 2012 while the string "05/04/64" would be interpreted as May 4, 1964. During parsing, only strings consisting of exactly two digits, as defined by Character.isDigit(char), will be parsed into the default century. Any other numeric string, such as a one digit string, a three or more digit string, or a two digit string that isn't all digits (for example, "-1"), is interpreted literally. So "01/02/3" or "01/02/003" are parsed, using the same pattern, as Jan 2, 3 AD. Likewise, "01/02/-3" is parsed as Jan 2, 4 BC.

(my emphasis)

E.g., 1 is a valid year. If you want to accept years only within a certain range (say, 1800-2100), you'll need to check the year on the date after parsing it.

更多推荐

本文发布于:2023-04-29 04:05:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1334460.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   转换为   格式   Date   format

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!