Java 为什么格式化日期给出上个世纪
我有4位数字的日期。我遇到了2050年以后的投入。在我的申请中,“0150”是指2050年1月1日。但当我格式化时,它会返回以下输出Java 为什么格式化日期给出上个世纪,java,date,date-format,simpledateformat,Java,Date,Date Format,Simpledateformat,我有4位数字的日期。我遇到了2050年以后的投入。在我的申请中,“0150”是指2050年1月1日。但当我格式化时,它会返回以下输出 Sun Jan 01 00:00:00 PKT 1950 public static Date getFormatDate( String newformat, String dateValue ) throws Exception { try { SimpleDateFormat simpleDateForm
Sun Jan 01 00:00:00 PKT 1950
public static Date getFormatDate( String newformat, String dateValue ) throws Exception {
try {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat( newformat );
java.util.Date newdate = simpleDateFormat.parse( dateValue );
return newdate;
}
catch( Exception e ) {
System.out.println( "Exception in converting date format --->" + e );
throw e;
}
}
public static void main( String[] args ) throws Exception {
System.out.println(getFormatDate( "MMyy", "0150" ));
}
我们将非常感谢您对解决此问题的任何帮助。来自SimpleDataFormat javadoc: 对于使用缩写年份模式(“y”或“yy”)进行解析, SimpleDateFormat必须解释相对于某些年份的缩写年份 世纪。为此,它将日期调整为80年以内 在SimpleDataFormat实例创建20年后 就你而言,1950年是离今天不到80年的时间了 为了获得正确的日期(2050),您必须调用。根据,
SimpleDataFormat
解析两位数的年份字段,因此结果是“在SimpleDataFormat实例创建之前80年内和之后20年内”
如果您想修改行为以更好地满足您的需求,您可以使用方法
set2DigitYearStart
来设置100年期的开始,您的日期将映射到100年期中。现在才2013年,人们已经在尝试再次修剪世纪了?