简体   繁体   English

每月的一天-Java日历

[英]Day of month - java calendar

I want get, how much month have got days. 我想要得到多少个月的时间。 I have got this code: 我有以下代码:

    Calendar cal = Calendar.getInstance();
    cal.set(2013, 1, 1);
    System.out.println(cal.getActualMaximum(cal.DAY_OF_MONTH));

Januar of year 2013 (and other else yaers :)) have got 31 days, but, I'm still getting 28, do you know, where I have got mistake? 2013年的Januar(以及其他Yaers :))已经有31天了,但是,我仍然有28天,你知道我哪里出错了吗? Thank you. 谢谢。

The first month of the year in the Gregorian and Julian calendars is JANUARY which is 0; 阳历和朱利安历中的第一月是一月,即0。 the last depends on the number of months in a year. 最后一个取决于一年中的月份数。 You are getting the max days for February. 您将获得2月的最高天数。

Try cal.set(2013, 0, 1); 试试cal.set(2013, 0, 1);

@sunrize920 is spot on. @ sunrize920已发现。 To avoid making mistakes, I find it easier to use the Calender deifned values as much as possible. 为避免出错,我发现尽可能使用Calender指定的值更容易。 So in your desired case that would be: 因此,在您想要的情况下,将是:

cal.set(2013, Calendar.JANUARY, 1);

Much easier, and saves remembering whether Calendar counts months from 0 or 1 ... 轻松得多,并且省去了对Calendar从0到1数月的记忆...

If you want to get number of days for January of 2013 如果您想获得2013年1月的天数

try 尝试

cal.set(2013,0,1);

tl;dr tl; dr

YearMonth.from(                               // Represent the month as a whole with `YearMonth` class.
    LocalDate.of( 2013 , Month.JANUARY , 1 )  // A date-only value, without time-of-day and without time zone.
)
.lengthOfMonth()                              // As for the month’s length. Returns an `int`.

31 31

java.time java.time

The modern approach uses the java.time classes that supplanted the troublesome old date-time classes such as Date & Calendar . 现代方法使用java.time类来代替麻烦的旧日期时间类,例如DateCalendar

LocalDate

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

A time zone is crucial in determining a date. 时区对于确定日期至关重要。 For any given moment, the date varies around the globe by zone. 在任何给定时刻,日期都会在全球范围内变化。 For example, a few minutes after midnight in Paris France is a new day while still “yesterday” in Montréal Québec . 例如, 法国巴黎午夜过后几分钟是新的一天,而在魁北克蒙特利尔仍然是“昨天”。

If no time zone is specified, the JVM implicitly applies its current default time zone. 如果未指定时区,则JVM隐式应用其当前的默认时区。 That default may change at any moment, so your results may vary. 该默认值可能随时更改,因此您的结果可能会有所不同。 Better to specify your desired/expected time zone explicitly as an argument. 最好将您的期望/期望时区明确指定为参数。

Specify a proper time zone name in the format of continent/region , such as America/Montreal , Africa/Casablanca , or Pacific/Auckland . continent/region的格式指定正确的时区名称 ,例如America/MontrealAfrica/CasablancaPacific/Auckland Never use the 3-4 letter abbreviation such as EST or IST as they are not true time zones, not standardized, and not even unique(!). 切勿使用ESTIST等3-4个字母的缩写,因为它们不是真实的时区,不是标准化的,甚至不是唯一的(!)。

ZoneId z = ZoneId.of( "America/Montreal" ) ;  
LocalDate today = LocalDate.now( z ) ;

If you want to use the JVM's current default time zone, ask for it and pass as an argument. 如果要使用JVM的当前默认时区,请提出要求并作为参数传递。 If omitted, the JVM's current default is applied implicitly. 如果省略,则隐式应用JVM的当前默认值。 Better to be explicit, as the default may be changed at any moment during runtime by any code in any thread of any app within the JVM. 最好明确一点,因为缺省值可以在运行时随时由JVM中任何应用程序的任何线程中的任何代码更改。

ZoneId z = ZoneId.systemDefault() ;  // Get JVM’s current default time zone.

Or specify a date. 或指定一个日期。 You may set the month by a number, with sane numbering 1-12 for January-December. 您可以用数字设置月份,一月至十二月的理智编号为1-12。

LocalDate ld = LocalDate.of( 1986 , 2 , 23 ) ;  // Years use sane direct numbering (1986 means year 1986). Months use sane numbering, 1-12 for January-December.

Or, better, use the Month enum objects pre-defined, one for each month of the year. 或者,最好使用预定义的Month枚举对象,一年中的每个月使用一个。 Tip: Use these Month objects throughout your codebase rather than a mere integer number to make your code more self-documenting, ensure valid values, and provide type-safety . 提示:在整个代码库中使用这些Month对象,而不是仅使用整数,可以使您的代码更具自文档性,确保有效值并提供类型安全

LocalDate ld = LocalDate.of( 1986 , Month.FEBRUARY , 23 ) ;

YearMonth

For that LocalDate , get its entire month, represented by the YearMonth class. 对于该LocalDate ,获取其整个月,由YearMonth类表示。

YearMonth ym = YearMonth.from( ld ) ;

Interrogate that YearMonth object for its length, the number of days in the month. 询问YearMonth对象的长度,即YearMonth的天数。

int lengthOfMonth = ym.lengthOfMonth() ;

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

You may exchange java.time objects directly with your database. 您可以直接与数据库交换java.time对象。 Use a JDBC driver compliant with JDBC 4.2 or later. 使用与JDBC 4.2或更高版本兼容的JDBC驱动程序 No need for strings, no need for java.sql.* classes. 不需要字符串,不需要java.sql.*类。

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