如何控制SimpleDateFormat解析为19xx或20xx? [英] How to control SimpleDateFormat parse to 19xx or 20xx?

查看:55
本文介绍了如何控制SimpleDateFormat解析为19xx或20xx?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以将以下日期字符串解析为 1916年7月23日,而不是 2016年7月23日? / p>

Is there a way to parse the following date string as July 23 1916 rather than July 23 2016?

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


推荐答案

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

The Java Doc (http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html) says:


要使用缩写年份模式( y或 yy)进行解析,SimpleDateFormat必须将相对于某个世纪的缩写年份。它通过将日期调整为创建SimpleDateFormat实例之前的80年和之后20年之内的日期。例如,使用模式 MM / dd / yy和1997年1月1日创建的SimpleDateFormat实例,字符串 01/11/12将解释为2012年1月11日,而字符串 05/04 / 64将被解释为1964年5月4日。

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)可用于确定年份。

这篇关于如何控制SimpleDateFormat解析为19xx或20xx?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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