gpt4 book ai didi

java - 如何控制 SimpleDateFormat 解析为 19xx 或 20xx?

转载 作者:IT老高 更新时间:2023-10-28 20:31:38 26 4
gpt4 key购买 nike

有没有办法将以下日期字符串解析为 July 23 1916 而不是 July 23 2016

System.out.println(new SimpleDateFormat("yy/MM/dd", Locale.US).parse("16/07/23"));

最佳答案

Java 文档 (http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html) 说:

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.

方法SimpleDateFormat.set2DigitYearStart(Date)可以用来固定年份。

关于java - 如何控制 SimpleDateFormat 解析为 19xx 或 20xx?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24903259/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com