为什么Java日历设置(int year,int month,int date)不返回正确的日期? [英] Why Java Calendar set(int year, int month, int date) not returning correct date?

查看:398
本文介绍了为什么Java日历设置(int year,int month,int date)不返回正确的日期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据doc,日历集()为:

According to doc, calendar set() is:

http://docs.oracle.com/javase/1.5.0/docs/api/java/util/Calendar。 html#set%28int,%20int,%20int%29

set(int year, int month, int date) 
Sets the values for the calendar fields YEAR, MONTH, and DAY_OF_MONTH.

代码

Calendar c1 = GregorianCalendar.getInstance();
c1.set(2000, 1, 30);  //January 30th 2000
Date sDate = c1.getTime();

System.out.println(sDate);

输出

Wed Mar 01 19:32:21 JST 2000

为什么不是1月30日

推荐答案

2月30日更改为3月1日。
您应该为月设置0。最好是使用日历中定义的常数:

1 for month is February. The 30th of February is changed to 1st of March. You should set 0 for month. The best is to use the constant defined in Calendar:

c1.set(2000, Calendar.JANUARY, 30);

这篇关于为什么Java日历设置(int year,int month,int date)不返回正确的日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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