English 中文(简体)
Java日历类不返回May-31〔关闭〕
原标题:Java Calendar Class Not returning May-31 [closed]
  • 时间:2011-06-01 02:18:57
  •  标签:
  • java
Closed. This question needs debugging details. It is not currently accepting answers.

编辑问题以包括所需行为、特定问题或错误,以及再现问题所需的最短代码。这将帮助其他人回答这个问题。

Closed 5 years ago.

My Program Java Calendar类设置为May-31

当我试图在5月份获得最大天数时,它返回了31天。没关系。

int days = c.getActualMaximum(Calendar.DAY_OF_MONTH);
System.out.println("Month:" + days);

但是当我尝试获取Calendar.MONTHCalendar.DAY_OF_MONTH

它总是在6月1日返回

问题回答

与所有其他日历字段不同,月份为零;一月=0,二月=1,等等。这意味着五月是月份号4,而不是5。

当您将月份设置为5,将日期设置为31时,您试图将其设置为June31,而将其转换为July1,6月份只有30天。

getActualMaximum()返回字段的最大值。

  • getActualMaximum(Calender.DAY_OF_MONTH) returns 31, the maximum amount of days in a month
  • getActualMaximum(Calender.DAY_OF_WEEK) will return 7, the maximum amount of days in a week

Calendar.MOUNTHCalendar.DAY_OF_MONTH不会自动设置为日期,因此返回它们的默认值6月1日

尝试

 Calendar c = Calendar.getInstance();

这应该是今天的日期。

tl;dr

使用现代的java.time类来取代麻烦的旧的遗留日期-时间类。

YearMonth.from( 
    LocalDate.of( 2018 , Month.MAY , 31 ) 
).lengthOfMonth()

31

LocalDate.of( 2018 , Month.MAY , 31 ) 
         .getMonthValue()

5.

LocalDate.of( 2018 , Month.MAY , 31 ) 
         .getYear()

2018

java.time

您的问题没有提供足够的详细信息来诊断问题。但这个问题还没有定论,现在有更好的替代方法。

麻烦的Calendar类现在是遗留的,被java.time类所取代。

表示月份的现代方法使用YearMonth类。

YearMonth ym = YearMonth.of( 2018 , Month.MAY ) ;

或者从日期、LocalDate对象中获取YearMonth

LocalDate ld = LocalDate.of( 2018 , Month.MAY , 31 ) ;
YearMonth ym = YearMonth.from( ld ) ;

询问月份长度

int lengthOfMonth = ym.lengthOfMonth() ;

31

LocalDate可以为您提供月份和年份的数字。

int dayOfMonth = ld.getMonthValue() ;
int year = ld.getYear() ;

About java.time

java.time框架内置于java 8及更高版本中。这些类取代了麻烦的旧传统日期-时间类,如java.util.Date日历,&;SimpleDateFormat

Joda Time项目,现在位于维护模式,建议迁移到java.time类。

要了解更多信息,请参阅Oracle教程。并在Stack Overflow中搜索许多示例和解释。规范是JSR 310

从哪里获得java.time类?

您只需要在创建Calendar()实例后添加一行即可。

例如:

Calendar cal = Calendar.getInstance();
cal.clear();

它会起作用的。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签