Why don#39;t I get the year right using SimpleDateFormat in java?(为什么我不能在Java中使用SimpleDateFormat来正确地计算年份呢?)
问题描述
在尝试解析MySql
格式的数据时,遇到了SimpleDateFormat
。我可以获得正确的日期和月份,但我得到了一年中奇怪的结果:
date = 2009-06-22;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date d = sdf.parse(date);
System.println(date);
System.println(d.getDate());
System.println(d.getMonth());
System.println(d.getYear());
输出:
2009-06-22
22 OK
5 Hum... Ok, months go from 0 to 11
109 o_O WTF ?
我尝试将格式更改为YYYY-MM-dd
(收到错误)和yy-MM-dd
(未执行任何操作)。我在Android上编程,不知道这是否重要。
目前,我使用拆分绕过它,但它很脏,无法使用I18N功能。
推荐答案
年份相对于1900年。这是Date
类的一个"特性"。尝试使用Calender
。
这篇关于为什么我不能在Java中使用SimpleDateFormat来正确地计算年份呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!