java java获取下个月的最后一天
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15807118/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
java get last day in next month
提问by faszynski
I have variable nowDate type of Date and I want set variable nextDate contains last day in next month.
我有变量 nowDate 类型的日期,我想设置变量 nextDate 包含下个月的最后一天。
For example: nowDate = 2013-04-16
例如:nowDate = 2013-04-16
So nextDate will contains 2013-05-31
所以 nextDate 将包含 2013-05-31
How can I do that?
我怎样才能做到这一点?
回答by jgreen
Similar to Xavi but one less line of code :-)
类似于 Xavi,但少了一行代码 :-)
Calendar cal = Calendar.getInstance();
cal.add(Calendar.MONTH, 1);
cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
回答by Arun P Johny
Try
尝试
private static Date getNextDate(Date nowDate) {
Calendar c = Calendar.getInstance();
c.setTime(nowDate);
c.add(Calendar.MONTH, 1);
c.set(Calendar.DATE, c.getMaximum(Calendar.DATE));
Date nextDate = c.getTime();
return nextDate;
}
Usage:
用法:
Date nowDate = new Date();
Date nextDate = getNextDate(nowDate);
回答by Xavi López
You could try setting a Calendar
to day one two months ahead, and then substract one day:
您可以尝试Calendar
提前两个月设置第一天,然后减去一天:
Calendar c = Calendar.getInstance();
c.add(Calendar.MONTH, 2);
c.set(Calendar.DAY_OF_MONTH, 1);
c.add(Calendar.DATE, -1);
Date nextDate = c.getTime();
As others have already pointed out, you could also just add one month, and use Calendar.getActualMaximum()
to set the last day of the following month.
正如其他人已经指出的,您也可以添加一个月,并用于Calendar.getActualMaximum()
设置下个月的最后一天。
Calendar c = Calendar.getInstance();
c.add(Calendar.MONTH, 1);
c.set(Calendar.DAY_OF_MONTH, c.getActualMaximum(Calendar.DAY_OF_MONTH));
Date nextDate = c.getTime();
回答by Basil Bourque
tl;dr
tl;博士
Very easy.
很简单。
YearMonth.now() // Get entire month for the date current in the JVM's current default time zone. Better to pass explicitly your desired/expected time zone.
.plusMonths( 1 ) // Move to the following month.
.atEndOfMonth() // Returns a `LocalDate` object, for a date-only value without time-of-day and without time zone.
.toString() // Generate a String in standard ISO 8601 format.
2018-02-28
2018-02-28
java.time
时间
The modern approach uses the java.timeclasses that supplanted the troublesome old legacy date-time classes.
现代方法使用java.time类取代了麻烦的旧的遗留日期时间类。
Get the date.
获取日期。
LocalDate today = LocalDate.now();
Better to pass explicitly the desired/expected time zone.
最好明确传递所需/预期的时区。
LocalDate today = LocalDate.now( ZoneId.of( "America/Montreal" ) ) ;
Pull the YearMonth
from that, to represent the month as a whole.
YearMonth
从那个中拉出来,代表整个月。
YearMonth ym = YearMonth.from( today ) ;
Or skip the LocalDate
entirely.
或者LocalDate
完全跳过。
YearMonth ym = YearMonth.now( ZoneId.of( "America/Montreal" ) ) ;
Move to next month.
移至下个月。
YearMonth ymNext = ym.plusMonths( 1 ) ;
Get the date at end of the month.
获取月底的日期。
LocalDate ld = ymNext.atEndOfMonth() ;
Generate a String in standard ISO 8601 format: YYYY-MM-DD.
生成标准 ISO 8601 格式的字符串:YYYY-MM-DD。
String output = ld.toString() ;
For other formats, search Stack Overflow for DateTimeFormatter
class.
对于其他格式,请搜索 Stack Overflow 以获取DateTimeFormatter
类。
About java.time
关于java.time
The java.timeframework is built into Java 8 and later. These classes supplant the troublesome old legacydate-time classes such as java.util.Date
, Calendar
, & SimpleDateFormat
.
该java.time框架是建立在Java 8和更高版本。这些类取代了麻烦的旧的遗留日期时间类,例如java.util.Date
, Calendar
, & SimpleDateFormat
。
The Joda-Timeproject, now in maintenance mode, advises migration to the java.timeclasses.
现在处于维护模式的Joda-Time项目建议迁移到java.time类。
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
要了解更多信息,请参阅Oracle 教程。并在 Stack Overflow 上搜索许多示例和解释。规范是JSR 310。
You may exchange java.timeobjects directly with your database. Use a JDBC drivercompliant with JDBC 4.2or later. No need for strings, no need for java.sql.*
classes.
您可以直接与数据库交换java.time对象。使用符合JDBC 4.2或更高版本的JDBC 驱动程序。不需要字符串,不需要类。java.sql.*
Where to obtain the java.time classes?
从哪里获得 java.time 类?
- Java SE 8, Java SE 9, Java SE 10, and later
- Built-in.
- Part of the standard Java API with a bundled implementation.
- Java 9 adds some minor features and fixes.
- Java SE 6and Java SE 7
- Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
- Android
- Later versions of Android bundle implementations of the java.time classes.
- For earlier Android (<26), the ThreeTenABPproject adapts ThreeTen-Backport(mentioned above). See How to use ThreeTenABP….
- Java SE 8、Java SE 9、Java SE 10及更高版本
- 内置。
- 具有捆绑实现的标准 Java API 的一部分。
- Java 9 添加了一些小功能和修复。
- Java SE 6和Java SE 7
- 多的java.time功能后移植到Java 6和7在ThreeTen-反向移植。
- 安卓
- 更高版本的 Android 捆绑实现 java.time 类。
- 对于早期的 Android(<26),ThreeTenABP项目采用了ThreeTen-Backport(上面提到过)。请参阅如何使用ThreeTenABP ...。
The ThreeTen-Extraproject extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval
, YearWeek
, YearQuarter
, and more.
该ThreeTen-额外项目与其他类扩展java.time。该项目是未来可能添加到 java.time 的试验场。你可能在这里找到一些有用的类,比如Interval
,YearWeek
,YearQuarter
,和更多。
回答by Joe2013
One way is to increment 2 months to your current month and set the date as 1st. After that decrement the date by 1 and it will give you the last day of month. This will automatically take care of leap year, 30 days, 31 days, 29 days and 28 days months. Program below
一种方法是将 2 个月增加到您当前的月份并将日期设置为 1 日。之后将日期减 1,它会给你一个月的最后一天。这将自动处理闰年、30 天、31 天、29 天和 28 天的月份。下面的程序
public class LastDayofNextMonth {
public static void main(String args[])
{
Calendar cal = Calendar.getInstance();
cal.set(2013,Calendar.APRIL, 14) ;
cal.set(Calendar.MONTH, cal.get(Calendar.MONTH)+2);
cal.set(Calendar.DATE,1);
cal.add(Calendar.DATE,-1);
System.out.println(cal.getTime().toString());
}
}
回答by hd1
回答by Ritesh Kumar Gupta
Use Calendar:
使用日历:
First Set the Calendar to the next month.
Use: cal.add(Calendar.MONTH, 1);
首先将日历设置为下个月。利用:cal.add(Calendar.MONTH, 1);
Then Use getActualMaximumto calculate the last day of the month that you set in previous step.
然后使用getActualMaximum计算您在上一步中设置的月份的最后一天。
import java.util.Calendar;
import java.util.Date;
public class Main{
public static void main(String ar[]){
Calendar cal = Calendar.getInstance();
cal.set(Calendar.DATE, cal.getActualMaximum(Calendar.DATE));
cal.add(Calendar.MONTH, 1);
Date lastDateOfNextMonth = cal.getTime();
System.out.println(lastDateOfNextMonth );
}
}
回答by Eng. Samer T
we have so many good answers, however all above answers have code like:
我们有很多很好的答案,但是以上所有答案都有如下代码:
cal.add(Calendar.MONTH, 1);
this code produce a little confusion because Months start at 0, so 1 is for February.
这段代码会产生一些混乱,因为 Months 从 0 开始,所以 1 代表二月。
(Edit: The integer in the add() function is not the month number (1=february) but the number of months to add )
(编辑:add() 函数中的整数不是月份数(1=二月)而是要添加的月份数)
回答by JRR
You can do the following to get last day of month
您可以执行以下操作以获取每月的最后一天
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.getActualMaximum(Calendar.DAY_OF_MONTH);