给定日期对象如何确定其月的最后一天?

时间:2022-10-16 12:05:44

I'm trying to use the following code but it's returning the wrong day of month.

我正在尝试使用以下代码,但它每月都会返回错误的日期。

Calendar cal = Calendar.getInstance();
cal.setTime(sampleDay.getTime());
cal.set(Calendar.MONTH, sampleDay.get(Calendar.MONTH)+1);
cal.set(Calendar.DAY_OF_MONTH, 0);
return cal.getTime();

8 个解决方案

#1


17  

Get the number of days for this month:

获取本月的天数:


Calendar cal = Calendar.getInstance();
cal.setTime(sampleDay.getTime());
int noOfLastDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH);

Set the Calendar to the last day of this month:

将日历设置为本月的最后一天:


Calendar cal = Calendar.getInstance();
cal.setTime(sampleDay.getTime());
cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));

#2


3  

I would create a date object for the first day of the NEXT month, and then just subtract a single day from the date object.

我会为NEXT月的第一天创建一个日期对象,然后从日期对象中减去一天。

#3


3  

tl;dr

YearMonth.from(
    LocalDate.now( ZoneId.of( "America/Montreal" ) )
).atEndOfMonth()

java.time

The Question and other Answers use old outmoded classes. They have been supplanted by the java.time classes built into Java 8 and later. See Oracle Tutorial. Much of the functionality has been back-ported to Java 6 & 7 in ThreeTen-Backport and further adapted to Android in ThreeTenABP.

问题和其他答案使用旧的过时类。它们已被Java 8及更高版本中内置的java.time类取代。请参阅Oracle教程。许多功能已经在ThreeTen-Backport中移植到Java 6和7中,并在ThreeTenABP中进一步适应Android。

LocalDate

The LocalDate class represents a date-only value without time-of-day and without time zone. While these objects store no time zone, note that time zone is crucial in determining the current date. For any given moment the date varies around the globe by time zone.

LocalDate类表示没有时间且没有时区的仅日期值。虽然这些对象不存储时区,但请注意,时区对于确定当前日期至关重要。对于任何特定时刻,日期在全球范围内按时区变化。

ZoneId zoneId = ZoneId.of( "America/Montreal" );
LocalDate today = LocalDate.now( zoneId );  // 2016-06-25

YearMonth

Combine with the YearMonth class to determine last day of any month.

结合YearMonth类来确定任何月份的最后一天。

YearMonth currentYearMonth = YearMonth.from( today );  // 2016-06
LocalDate lastDayOfCurrentYearMonth = currentYearMonth.atEndOfMonth();  // 2016-06-30

By the way, both LocalDate and YearMonth use month numbers as you would expect (1-12) rather than the screwball 0-11 seen in the old date-time classes. One of many poor design decisions that make those old date-time classes so troublesome and confusing.

顺便说一下,LocalDate和YearMonth都使用了你想要的月份数(1-12)而不是旧日期时间类中看到的0-11螺旋球。许多糟糕的设计决策之一,使那些旧的日期时间类如此麻烦和混乱。

TemporalAdjuster

Another valid approach is using a TemporalAdjuster. See the correct Answer by Pierre Henry.

另一种有效的方法是使用TemporalAdjuster。请参阅Pierre Henry的正确答案。

#4


2  

Use calObject.getActualMaximum(calobject.DAY_OF_MONTH)

See Real's Java How-to for more info on this.

有关详细信息,请参阅Real的Java操作方法。

#5


2  

It looks like you set the calendar to the first day of the next month, so you need one more line to subtract one day, to get the last day of the month that sampleDay is in:

看起来您将日历设置为下个月的第一天,因此您还需要一行减去一天,以获取sampleDay所在月份的最后一天:

Calendar cal = Calendar.getInstance();
cal.setTime(sampleDay.getTime());
cal.roll(Calendar.MONTH, true);
cal.set(Calendar.DAY_OF_MONTH, 0);
cal.add(Calendar.DAY_OF_MONTH, -1);

In general, it's much easier to do this kind of thing using Joda Time, eg:

一般来说,使用Joda Time做这种事情要容易得多,例如:

DateTime date = new DateTime(sampleDay.getTime());
return date.plusMonths(1).withDayOfMonth(0).minusDays(1).getMillis();

#6


2  

TemporalAdjuster

Using the (relatively) new Java date API, it is actually very easy :

使用(相对)新的Java日期API,实际上非常简单:

Let date be an instance of LocalDate, for example :

让date成为LocalDate的一个实例,例如:

LocalDate date = LocalDate.of(2018, 1, 22);

or

LocalDate date = LocalDate.now();

or, of course, you could get it as a user input, from a database, etc.

或者,当然,您可以将其作为用户输入,从数据库等获取。

Then apply an implementation of the TemporalAdjuster interface found in the TemporalAdjusters class:

然后应用TemporalAdjusters类中的TemporalAdjuster接口的实现:

LocalDate first = date.with(TemporalAdjusters.firstDayOfMonth());
LocalDate last = date.with(TemporalAdjusters.lastDayOfMonth());

#7


1  

If you use the date4j library:

如果您使用date4j库:

DateTime monthEnd = dt.getEndOfMonth();

#8


-1  

I think this should work nicely:

我认为这应该很好用:

Dim MyDate As Date = #11/14/2012#  'This is just an example date

MyDate = MyDate.AddDays(DateTime.DaysInMonth(MyDate.Year, MyDate.Month) - MyDate.Day)

#1


17  

Get the number of days for this month:

获取本月的天数:


Calendar cal = Calendar.getInstance();
cal.setTime(sampleDay.getTime());
int noOfLastDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH);

Set the Calendar to the last day of this month:

将日历设置为本月的最后一天:


Calendar cal = Calendar.getInstance();
cal.setTime(sampleDay.getTime());
cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));

#2


3  

I would create a date object for the first day of the NEXT month, and then just subtract a single day from the date object.

我会为NEXT月的第一天创建一个日期对象,然后从日期对象中减去一天。

#3


3  

tl;dr

YearMonth.from(
    LocalDate.now( ZoneId.of( "America/Montreal" ) )
).atEndOfMonth()

java.time

The Question and other Answers use old outmoded classes. They have been supplanted by the java.time classes built into Java 8 and later. See Oracle Tutorial. Much of the functionality has been back-ported to Java 6 & 7 in ThreeTen-Backport and further adapted to Android in ThreeTenABP.

问题和其他答案使用旧的过时类。它们已被Java 8及更高版本中内置的java.time类取代。请参阅Oracle教程。许多功能已经在ThreeTen-Backport中移植到Java 6和7中,并在ThreeTenABP中进一步适应Android。

LocalDate

The LocalDate class represents a date-only value without time-of-day and without time zone. While these objects store no time zone, note that time zone is crucial in determining the current date. For any given moment the date varies around the globe by time zone.

LocalDate类表示没有时间且没有时区的仅日期值。虽然这些对象不存储时区,但请注意,时区对于确定当前日期至关重要。对于任何特定时刻,日期在全球范围内按时区变化。

ZoneId zoneId = ZoneId.of( "America/Montreal" );
LocalDate today = LocalDate.now( zoneId );  // 2016-06-25

YearMonth

Combine with the YearMonth class to determine last day of any month.

结合YearMonth类来确定任何月份的最后一天。

YearMonth currentYearMonth = YearMonth.from( today );  // 2016-06
LocalDate lastDayOfCurrentYearMonth = currentYearMonth.atEndOfMonth();  // 2016-06-30

By the way, both LocalDate and YearMonth use month numbers as you would expect (1-12) rather than the screwball 0-11 seen in the old date-time classes. One of many poor design decisions that make those old date-time classes so troublesome and confusing.

顺便说一下,LocalDate和YearMonth都使用了你想要的月份数(1-12)而不是旧日期时间类中看到的0-11螺旋球。许多糟糕的设计决策之一,使那些旧的日期时间类如此麻烦和混乱。

TemporalAdjuster

Another valid approach is using a TemporalAdjuster. See the correct Answer by Pierre Henry.

另一种有效的方法是使用TemporalAdjuster。请参阅Pierre Henry的正确答案。

#4


2  

Use calObject.getActualMaximum(calobject.DAY_OF_MONTH)

See Real's Java How-to for more info on this.

有关详细信息,请参阅Real的Java操作方法。

#5


2  

It looks like you set the calendar to the first day of the next month, so you need one more line to subtract one day, to get the last day of the month that sampleDay is in:

看起来您将日历设置为下个月的第一天,因此您还需要一行减去一天,以获取sampleDay所在月份的最后一天:

Calendar cal = Calendar.getInstance();
cal.setTime(sampleDay.getTime());
cal.roll(Calendar.MONTH, true);
cal.set(Calendar.DAY_OF_MONTH, 0);
cal.add(Calendar.DAY_OF_MONTH, -1);

In general, it's much easier to do this kind of thing using Joda Time, eg:

一般来说,使用Joda Time做这种事情要容易得多,例如:

DateTime date = new DateTime(sampleDay.getTime());
return date.plusMonths(1).withDayOfMonth(0).minusDays(1).getMillis();

#6


2  

TemporalAdjuster

Using the (relatively) new Java date API, it is actually very easy :

使用(相对)新的Java日期API,实际上非常简单:

Let date be an instance of LocalDate, for example :

让date成为LocalDate的一个实例,例如:

LocalDate date = LocalDate.of(2018, 1, 22);

or

LocalDate date = LocalDate.now();

or, of course, you could get it as a user input, from a database, etc.

或者,当然,您可以将其作为用户输入,从数据库等获取。

Then apply an implementation of the TemporalAdjuster interface found in the TemporalAdjusters class:

然后应用TemporalAdjusters类中的TemporalAdjuster接口的实现:

LocalDate first = date.with(TemporalAdjusters.firstDayOfMonth());
LocalDate last = date.with(TemporalAdjusters.lastDayOfMonth());

#7


1  

If you use the date4j library:

如果您使用date4j库:

DateTime monthEnd = dt.getEndOfMonth();

#8


-1  

I think this should work nicely:

我认为这应该很好用:

Dim MyDate As Date = #11/14/2012#  'This is just an example date

MyDate = MyDate.AddDays(DateTime.DaysInMonth(MyDate.Year, MyDate.Month) - MyDate.Day)