Java中SimpleDataFormat值错误
我正在尝试将日历值设置为其基数(0),以便稍后将其与另一个值进行比较。当我尝试打印结果时,我使用SimpareDateFormat得到的是月份值。有什么想法吗?我做得对吗Java中SimpleDataFormat值错误,java,date-format,simpledateformat,Java,Date Format,Simpledateformat,我正在尝试将日历值设置为其基数(0),以便稍后将其与另一个值进行比较。当我尝试打印结果时,我使用SimpareDateFormat得到的是月份值。有什么想法吗?我做得对吗 SimpleDateFormat DataDateFormat = new SimpleDateFormat("yyyy-MM-dd"); SimpleDateFormat DataDateFormatMonth = new SimpleDateFormat("MM"); Calendar cld = Calendar.ge
SimpleDateFormat DataDateFormat = new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat DataDateFormatMonth = new SimpleDateFormat("MM");
Calendar cld = Calendar.getInstance();
cld.set(1, 1, 1);
System.out.println(DataDateFormat.format(cld.getTime())); // -> 0001-02-01
System.out.println(DataDateFormatMonth.format(cld.getTime())); // -> 02 !!!!
System.out.println(cld.get(Calendar.MONTH)); // -> 1
这就是您想要得到的,因为
new SimpleDateFormat(“MM”)
将格式化输入日期,并只返回MM
值,即月份。月份从日历中的0开始。一月->0,二月->1。。。12月->11日日历API中的月间隔为0到11日
JAN--->0
.......
DEC--->11
Calendar.MONTH将返回一个int值(2),在您的情况下,该值为MM。i、 例如,Calander.MONTH是一个常数
细分:
System.out.println(cld.get(Calendar.MONTH)); // -> 1
Calaender.MONTH--- 2
cld.get(2); will get the value at the given calendar field. (in this case 2, your month field whihc is 1)
例如,将上一次打印语句更改为
Calendar cld = Calendar.getInstance();
cld.set(10, 10, 1100); (format is MM,dd,YYYY)
System.out.println(cld.get(Calendar.YEAR));
输出将是10(它将返回格式中的月份值),作为日历。从集合()中,年份将返回1
参数:
- 年份-用于设置年份时间字段的值
- 月-用于设置月时间字段的值月值为
以0为基础。e、 g.,1月份为0。
- 日期-用于设置日期时间字段的值
这使得将其他语言的源代码翻译成Java特别容易。花了我几分钟来找出文字4
和Calendar.APRIL
之间的差异。谢谢你,甲骨文。顺便说一句,你的链接断了。谢谢。修复了链接