Java 设置日历实例的时间

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19792066/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 20:14:41  来源:igfitidea点击:

Setting time for calendar instance

java

提问by shmoula

I'm trying to set time to Calendar instance, but I'm experiencing weird behavior. Let's check some example:

我正在尝试为 Calendar 实例设置时间,但我遇到了奇怪的行为。让我们检查一些例子:

Calendar c = Calendar.getInstance();
//c.setTime(date);

c.set(date.getYear(), (date.getMonth() - 1), date.getDay());
int dayOfWeek = c.get(Calendar.DAY_OF_WEEK);

When I set time with setTime(), it sets wrong date. The second (and deprecated) set(year, month, day) works correctly. I've never met with this before - I thought that it uses default timezone in both cases, so it should be the same. Can someone explain it to me please?

当我使用 setTime() 设置时间时,它设置了错误的日期。第二个(和已弃用的)集合(年、月、日)正常工作。我以前从未遇到过这个 - 我认为它在两种情况下都使用默认时区,所以它应该是相同的。有人可以向我解释一下吗?

==================== EDIT: Let's ask that another way:

==================== 编辑:让我们换一种方式问:

Date date = new Date();
Calendar c1 = Calendar.getInstance();
c1.setTime(date);

Calendar c2 = Calendar.getInstance();   
c2.set(date.getYear(), (date.getMonth() - 1), date.getDay());

int day1 = c1.get(Calendar.DAY_OF_WEEK);
int day2 = c2.get(Calendar.DAY_OF_WEEK);

// day1 != day2   ---> I'd like to know - WHY?

So for now are dates as follows:

所以现在的日期如下:

date: Nov 5, 2013 4:27:02 PM
day1: 3
day2: 1
time: 1383665222638
timezone: Europe/Prague

采纳答案by Joni

This line does not do what you think it does:

这一行不会做你认为它做的事情:

c2.set(date.getYear(), (date.getMonth() - 1), date.getDay());

The deprecated methods in the Dateclass work like this:

Date类中已弃用的方法的工作方式如下:

  • getYear()returns year - 1900, so that for example 1986 is returned as 86, and 2013 is returned as 113.
  • getMonth()returns month - 1, so November is returned as 10. Subtracting 1 gives you October.
  • getDay()returns the day of the week; Tuesday is returned as 2.
  • getYear()返回年份 - 1900,例如 1986 返回为 86,2013 返回为 113。
  • getMonth()返回月份 - 1,因此 11 月返回为 10。减去 1 得出 10 月。
  • getDay()返回星期几;星期二返回为 2。

So, given your date, you are setting the calendar's date to Oct 2 year 113. The correct use of the API would be:

因此,根据您的日期,您将日历的日期设置为 113 年 10 月 2 日。 API 的正确用法是:

c2.set(1900+date.getYear(), date.getMonth(), date.getDate());

回答by Ruchira Gayan Ranaweera

There is no issue in this

这没有问题

    Calendar c = Calendar.getInstance();
    c.setTime(new Date());

    c.set(2013, (12 - 1), 05);
    int dayOfWeek = c.get(Calendar.DAY_OF_WEEK);

If you are trying to do some thing like following will deprecated since you are using Date.

如果您尝试执行以下操作,则不推荐使用,因为您使用的是 Date。

  c.set(new Date().getYear(), (new Date().getMonth() - 1), new Date().getDate());
  //don't use `java.util.Date` here since there can be differences 

java.util.Calendarvalues over java.util.Date

java.util.Calendar价值超过 java.util.Date

回答by jbx

Your approach will get a bit hairy especially because of timezones and daylight savings. A lot of the Datemethods were deprecated because they were confusing to work with. The approach I like to use is this:

您的方法会有点麻烦,尤其是因为时区和夏令时。许多Date方法已被弃用,因为它们使用起来令人困惑。我喜欢使用的方法是这样的:

Calendar c = Calendar.getInstance();
c.setTimeInMillis(date.getTime());

//get the same day but previous month
c.add(Calendar.MONTH, -1);

That way I let the Calendardo the calculation.

这样我就让Calendar计算。