My Program Java Calendar类设置为May-31
当我试图在5月份获得最大天数时,它返回了31天。没关系。
int days = c.getActualMaximum(Calendar.DAY_OF_MONTH);
System.out.println("Month:" + days);
但是当我尝试获取Calendar.MONTH
和Calendar.DAY_OF_MONTH
时
它总是在6月1日返回
My Program Java Calendar类设置为May-31
当我试图在5月份获得最大天数时,它返回了31天。没关系。
int days = c.getActualMaximum(Calendar.DAY_OF_MONTH);
System.out.println("Month:" + days);
但是当我尝试获取Calendar.MONTH
和Calendar.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 monthgetActualMaximum(Calender.DAY_OF_WEEK)
will return 7, the maximum amount of days in a weekCalendar.MOUNTH
和Calendar.DAY_OF_MONTH
不会自动设置为日期,因此返回它们的默认值6月1日
尝试
Calendar c = Calendar.getInstance();
这应该是今天的日期。
使用现代的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
您的问题没有提供足够的详细信息来诊断问题。但这个问题还没有定论,现在有更好的替代方法。
麻烦的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() ;
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();
它会起作用的。
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 ...
Check this, List<String> list = new ArrayList<String>(); for (int i = 0; i < 10000; i++) { String value = (""+UUID.randomUUID().getLeastSignificantBits()).substring(3, ...
I am in the middle of solving a problem where I think it s best suited for a decorator and a state pattern. The high level setting is something like a sandwich maker and dispenser, where I have a set ...
I have been trying to execute a MS SQL Server stored procedure via JDBC today and have been unsuccessful thus far. The stored procedure has 1 input and 1 output parameter. With every combination I ...
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 ...
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 ...
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....
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 ...