简体   繁体   English

在日历中设置值(java.util.Calendar)

[英]Setting values in calendar (java.util.Calendar)

I'm learning the Calendar class in Java and I am unable to understand the Set(Calendar.Day OF MONTH) method. 我正在学习Java中的Calendar类,但无法理解Set(Calendar.Day OF MONTH)方法。

Here it goes: 它去了:

import java.util.Calendar;  
import java.util.Date


public class TestCalender
{

 public static void main(String[] args)
    {

        Calendar cal = Calendar.getInstance();
        Date date= cal.getTime();
        System.out.println(date);
        cal.set(Calendar.DAY_OF_MONTH,33);
        //cal.set(Calendar.MONTH,13);------>(1)
        Date newdate = cal.getTime();
        System.out.println(newdate);

Output: 输出:

Fri May 12 17:30:50 CDT 2017  
Fri Jun 02 17:30:50 CDT 2017

When I uncomment the statement(1) the output changes to: 当我取消对statement(1)的注释时,输出更改为:

Fri May 12 17:33:22 CDT 2017  
Mon Mar 05 17:33:22 CST 2018

Here is my question: 这是我的问题:

I understood the change of Month to March but I'm not able to figure out why the date has changed to 5. As per my understanding shouldn't the date be changed to April 02 2018 (when 33 days of March is being computed since March has only 31 days the count moves to the month of April). 我知道将月份更改为三月,但是我无法弄清为什么日期更改为5。据我的理解,日期不应该更改为2018年4月2日(自3月起计算33天) 3月只有31天,计数转移到4月。

I will be extremely grateful if someone could help in clearing this doubt. 如果有人可以帮助消除这一疑问,我将万分感谢。

Thanks in advance. 提前致谢。

Regards Roopa 问候鲁帕

The Calendar class uses months starting from 0, and ending at 11 for December. Calendar类使用从0开始的月份,到12月结束于11。 Therefore, when you set the month to 13, you specified February of the following year, and the "33rd day of February" (which has 28 days) is the 5th of March. 因此,当您将月份设置为13时,您指定了下一年的2月,而“ 2月33日”(有28天)是3月5日。

The java.util.date classes are quirky and hard to use. java.util.date类很古怪且难以使用。 Use java.time instead. 使用java.time代替。

I'm learning the Calendar class 我正在学习日历课程

Don't. 别。

The Calendar class is notoriously troublesome, poorly designed, confusing, and flawed. Calendar类非常麻烦,设计不当,令人困惑和有缺陷。 Now legacy. 现在是遗产。 Supplanted by the java.time classes. 由java.time类取代。 We can sweep that class into the dustbin of Java history. 我们可以将该类扫入Java历史的垃圾箱中。

Among its many issues, Calendar uses crazy month numbering 0-11 for January-December. 在众多问题中, Calendar使用1月至12月的0-11疯狂月份。 This fact was correctly described in the correct Answer by Kyriacou . Kyriacou在正确答案中正确描述了这一事实。 The java.time classes, in contrast, use sane numbering 1-12 for Jan-Dec; 相反,java.time类对Jan-Dec使用理智的编号1-12; see the Month enum. 请参阅Month枚举。

Not quite sure what your goal is in that code snippet, but you seem to be adding 33 days to a date. 不太清楚代码段中的目标是什么,但是您似乎要为日期增加33天。

The LocalDate class represents a date-only value without time-of-day and without time zone. LocalDate类表示没有日期和时区的仅日期值。

One big difference between java.time and the legacy classes is that the modern classes use immutable objects . java.time和旧类之间的一大区别是,现代类使用不可变对象 So adding days to a date results in a new date object with its values based on the original object, while the original object remains untouched. 因此,将日期添加到日期会导致一个新的日期对象,其值基于原始对象,而原始对象保持不变。 This avoids much confusion, and makes them thread-safe . 这避免了很多混乱,并使它们成为线程安全的

LocalDate ld = LocalDate.of( 2017 , Month.MARCH , 23 ) ;
LocalDate later = ld.plusDays( 33 );

About java.time 关于java.time

The java.time framework is built into Java 8 and later. java.time框架内置于Java 8及更高版本中。 These classes supplant the troublesome old legacy date-time classes such as java.util.Date , Calendar , & SimpleDateFormat . 这些类取代了麻烦的旧的旧式日期时间类,例如java.util.DateCalendarSimpleDateFormat

The Joda-Time project, now in maintenance mode , advises migration to the java.time classes. 现在处于维护模式Joda-Time项目建议迁移到java.time类。

To learn more, see the Oracle Tutorial . 要了解更多信息,请参见Oracle教程 And search Stack Overflow for many examples and explanations. 并在Stack Overflow中搜索许多示例和说明。 Specification is JSR 310 . 规格为JSR 310

Where to obtain the java.time classes? 在哪里获取java.time类?

The ThreeTen-Extra project extends java.time with additional classes. ThreeTen-Extra项目使用其他类扩展了java.time。 This project is a proving ground for possible future additions to java.time. 该项目为将来可能在java.time中添加内容提供了一个试验场。 You may find some useful classes here such as Interval , YearWeek , YearQuarter , and more . 您可以在这里找到一些有用的类,比如IntervalYearWeekYearQuarter ,和更多

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM