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

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

问题描述

根据文档,日历 set() 是:

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 日???

推荐答案

1 月是二月.2 月 30 日改为 3 月 1 日.您应该为月份设置 0.最好是使用 Calendar 中定义的常量:

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 Calendar set(int year, int month, int date) 没有返回正确的日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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