The java.time framework is built into Java 8 and later. These classes supplant the old troublesome date-time classes such as java.util.Date, .Calendar, & java.text.SimpleDateFormat. The Joda-Time team also advises migration to java.time.
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations.
Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport and further adapted to Android in ThreeTenABP.
Date-only
If you want the date-only, use the LocalDate class.
ZoneId z = ZoneId.of( "America/Montreal" );
LocalDate today = LocalDate.now( z );
today.toString(): 2017-01-23
Add a month.
LocalDate oneMonthLater = today.plusMonths( 1 );
oneMonthLater.toString(): 2017-02-23
Date-time
Perhaps you want a time-of-day along with the date.
First get the current moment in UTC with a resolution of nanoseconds.
Instant instant = Instant.now();
Adding a month means determining dates. And determining dates means applying a time zone. For any given moment, the date varies around the world with a new day dawning earlier to the east. So adjust that Instant into a time zone.
You might want to adjust the time-of-day to the first moment of the day when making this kind of calculation. That first moment is not always 00:00:00.0 so let java.time determine the time-of-day.
You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.* classes. Hibernate 5 & JPA 2.2 support java.time.
Update: The Joda-Time project is now in maintenance mode. Its team advises migration to the java.time classes. I am leaving this section intact for posterity.
The Joda-Time library offers a method to add months in a smart way.
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String d = "2000-01-30";
Date date= new Date(sdf.parse(d).getTime());
date.setMonth(date.getMonth() + 1);
You can use like this;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String d = "2000-01-30";
Date date= new Date(sdf.parse(d).getTime());
date.setMonth(date.getMonth() + 1);
Calendar calendar = Calendar.getInstance();
Calendar calFebruary = Calendar.getInstance();
calFebruary.set(Calendar.MONTH, Calendar.FEBRUARY);
if (calendar.get(Calendar.DAY_OF_MONTH) == 1) {// if first day of month
calendar.add(Calendar.MONTH, 1);
calendar.set(Calendar.DATE, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
Date nextMonthFirstDay = calendar.getTime();
System.out.println(nextMonthFirstDay);
}
如果该天是该月的最后一天,请运行以下代码。
else if ((calendar.getActualMaximum(Calendar.DAY_OF_MONTH) == calendar.get(Calendar.DAY_OF_MONTH))) {// if last day of month
calendar.add(Calendar.MONTH, 1);
calendar.set(Calendar.DATE, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
Date nextMonthLastDay = calendar.getTime();
System.out.println(nextMonthLastDay);
}
如果这一天是二月,则运行以下代码,
else if (calendar.get(Calendar.MONTH) == Calendar.JANUARY
&& calendar.get(Calendar.DAY_OF_MONTH) > calFebruary.getActualMaximum(Calendar.DAY_OF_MONTH)) {// control of february
calendar.add(Calendar.MONTH, 1);
calendar.set(Calendar.DATE, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
Date nextMonthLastDay = calendar.getTime();
System.out.println(nextMonthLastDay);
}
以下代码用于其他情况。
else { // any day
calendar.add(Calendar.DATE, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
Date theNextDate = calendar.getTime();
System.out.println(theNextDate);
}
In order to find the day after one month, it is necessary to look at what day of the month it is today.
So if the day is first day of month run following code
Calendar calendar = Calendar.getInstance();
Calendar calFebruary = Calendar.getInstance();
calFebruary.set(Calendar.MONTH, Calendar.FEBRUARY);
if (calendar.get(Calendar.DAY_OF_MONTH) == 1) {// if first day of month
calendar.add(Calendar.MONTH, 1);
calendar.set(Calendar.DATE, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
Date nextMonthFirstDay = calendar.getTime();
System.out.println(nextMonthFirstDay);
}
if the day is last day of month, run following codes.
else if ((calendar.getActualMaximum(Calendar.DAY_OF_MONTH) == calendar.get(Calendar.DAY_OF_MONTH))) {// if last day of month
calendar.add(Calendar.MONTH, 1);
calendar.set(Calendar.DATE, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
Date nextMonthLastDay = calendar.getTime();
System.out.println(nextMonthLastDay);
}
if the day is in february run following code
else if (calendar.get(Calendar.MONTH) == Calendar.JANUARY
&& calendar.get(Calendar.DAY_OF_MONTH) > calFebruary.getActualMaximum(Calendar.DAY_OF_MONTH)) {// control of february
calendar.add(Calendar.MONTH, 1);
calendar.set(Calendar.DATE, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
Date nextMonthLastDay = calendar.getTime();
System.out.println(nextMonthLastDay);
}
the following codes are used for other cases.
else { // any day
calendar.add(Calendar.DATE, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
Date theNextDate = calendar.getTime();
System.out.println(theNextDate);
}
Calendar cal = Calendar.getInstance();
cal.setTime(yourDate);
DateSumUtil.sumOneMonth(cal);
就可以了。相关代码:
package you.project.your_package_utils;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Calendar;
import java.util.List;
public class DateSumUtil {
private static Integer[] meses31 = { 2, 4, 7, 9 };
private static List<Integer> meses31List = Arrays.asList(meses31);
private static SimpleDateFormat s = new SimpleDateFormat("dd/MM/yyyy");
private static final int MES = Calendar.MONTH;
private static final int ANO = Calendar.YEAR;
private static final int DIA = Calendar.DAY_OF_MONTH;
/**
* Receives a date and adds one month. <br />
*
* @param c date to receive an added month, as {@code java.util.Calendar}
* @param dia day of month of the original month
*/
public static void addOneMonth(Calendar c, int dia) throws ParseException {
if (cal.get(MES) == 0) { if (dia < 29) cal.add(MES, 1);
else { if (cal.get(ANO) % 4 == 0) { if (dia < 30) cal.add(MES, 1);
else cal.setTime(s.parse("29/02/" + cal.get(ANO)));
} else { if (dia < 29) cal.add(MES, 1);
else cal.setTime(s.parse("28/02/" + cal.get(ANO)));
} } } else if (meses31List.contains(cal.get(MES))) {
if (dia < 31) { cal.add(Calendar.MONTH, 1);
cal.set(DIA, dia);
} else cal.setTime(s.parse("30/" + (cal.get(MES) + 2) + "/" + cal.get(ANO)));
} else { cal.add(MES, 1);
cal.set(DIA, dia); }
}
Constants are in Portuguese because yes, but javadoc is understandable enough.
Just call
Calendar cal = Calendar.getInstance();
cal.setTime(yourDate);
DateSumUtil.sumOneMonth(cal);
and that's that. Related code:
package you.project.your_package_utils;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Calendar;
import java.util.List;
public class DateSumUtil {
private static Integer[] meses31 = { 2, 4, 7, 9 };
private static List<Integer> meses31List = Arrays.asList(meses31);
private static SimpleDateFormat s = new SimpleDateFormat("dd/MM/yyyy");
private static final int MES = Calendar.MONTH;
private static final int ANO = Calendar.YEAR;
private static final int DIA = Calendar.DAY_OF_MONTH;
/**
* Receives a date and adds one month. <br />
*
* @param c date to receive an added month, as {@code java.util.Calendar}
* @param dia day of month of the original month
*/
public static void addOneMonth(Calendar c, int dia) throws ParseException {
if (cal.get(MES) == 0) { if (dia < 29) cal.add(MES, 1);
else { if (cal.get(ANO) % 4 == 0) { if (dia < 30) cal.add(MES, 1);
else cal.setTime(s.parse("29/02/" + cal.get(ANO)));
} else { if (dia < 29) cal.add(MES, 1);
else cal.setTime(s.parse("28/02/" + cal.get(ANO)));
} } } else if (meses31List.contains(cal.get(MES))) {
if (dia < 31) { cal.add(Calendar.MONTH, 1);
cal.set(DIA, dia);
} else cal.setTime(s.parse("30/" + (cal.get(MES) + 2) + "/" + cal.get(ANO)));
} else { cal.add(MES, 1);
cal.set(DIA, dia); }
}
发布评论
评论(18)
Java 8
Java 8
您可以使用 apache 的 commons lang DateUtils 帮助实用程序类。
您可以在 http://commons.apache.org/proper/commons-lang/ 下载 commons lang jar
You can make use of apache's commons lang DateUtils helper utility class.
You can download commons lang jar at http://commons.apache.org/proper/commons-lang/
tl;dr
示例:
最好指定时区。
java.time
java.time 框架内置于 Java 8 及更高版本中。这些类取代了旧的麻烦的日期时间类,例如 java.util.Date、.Calendar 和 & 。
java.text.SimpleDateFormat
。 Joda-Time 团队还建议迁移到 java.time。要了解更多信息,请参阅 Oracle 教程。并在 Stack Overflow 上搜索许多示例和解释。
许多 java.time 功能都向后移植到 Java 6 和 Java 6。 ThreeTen-Backport 中的 7 并在 ThreeTenABP。
仅日期
如果您想要仅日期,请使用
LocalDate
类。添加一个月。
日期时间
也许您想要一个时间和日期。
首先获取 UTC 中的当前时刻,分辨率为 纳秒。
添加月份意味着确定日期。确定日期意味着应用时区。对于任何特定时刻,世界各地的日期都会有所不同,新的一天在东方黎明较早。因此,将
Instant
调整为时区。现在添加您的月份。让 java.time 处理闰月,以及月份长度不同的事实。
在进行此类计算时,您可能需要将时间调整为一天中的第一时刻。第一个时刻并不总是
00:00:00.0
,因此让 java.time 确定一天中的时间。关于 java.time
java.time 框架内置于 Java 8 及更高版本中。这些类取代了麻烦的旧遗留日期时间类,例如
java.util.Date
,日历
, &SimpleDateFormat
。要了解更多信息,请参阅 Oracle 教程。并在 Stack Overflow 上搜索许多示例和解释。规范为 JSR 310。
Joda-Time 项目,现已在 维护模式,建议迁移到 java.time 类。
您可以直接与数据库交换java.time对象。使用符合 JDBC 驱动程序 /jeps/170" rel="nofollow noreferrer">JDBC 4.2 或更高版本。不需要字符串,不需要 java.sql.* 类。 Hibernate 5 和 Hibernate 5 JPA 2.2 支持 java.time。
从哪里获取 java.time 类?
Joda-Time
更新:Joda-Time 项目现已处于维护模式。其团队建议迁移到 java.time 类。我将把这一部分完整地留给后代。
Joda-Time 库提供了一种以智能方式添加月份的方法。
您可能想通过将时间调整为一天中的第一时刻来专注于这一天。
tl;dr
Example:
Better to specify time zone.
java.time
The java.time framework is built into Java 8 and later. These classes supplant the old troublesome date-time classes such as
java.util.Date
,.Calendar
, &java.text.SimpleDateFormat
. The Joda-Time team also advises migration to java.time.To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations.
Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport and further adapted to Android in ThreeTenABP.
Date-only
If you want the date-only, use the
LocalDate
class.Add a month.
Date-time
Perhaps you want a time-of-day along with the date.
First get the current moment in UTC with a resolution of nanoseconds.
Adding a month means determining dates. And determining dates means applying a time zone. For any given moment, the date varies around the world with a new day dawning earlier to the east. So adjust that
Instant
into a time zone.Now add your month. Let java.time handle Leap month, and the fact that months vary in length.
You might want to adjust the time-of-day to the first moment of the day when making this kind of calculation. That first moment is not always
00:00:00.0
so let java.time determine the time-of-day.About java.time
The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as
java.util.Date
,Calendar
, &SimpleDateFormat
.To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.
You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for
java.sql.*
classes. Hibernate 5 & JPA 2.2 support java.time.Where to obtain the java.time classes?
Joda-Time
Update: The Joda-Time project is now in maintenance mode. Its team advises migration to the java.time classes. I am leaving this section intact for posterity.
The Joda-Time library offers a method to add months in a smart way.
You might want to focus on the day by adjust the time-of-day to the first moment of the day.
(改编自杜古)
(adapted from Duggu)
您可以使用 org.apache.commons.lang3.time 包中的 DateUtils 类
you can use DateUtils class in org.apache.commons.lang3.time package
使用 calander 并尝试此代码。
Use calander and try this code.
如果您需要一行(即 Jasper Reports 公式)并且不介意调整是否不是一个月(即“30 天”就足够了):
new Date($F{invoicedate}.getTime ()+30L*24L*60L*60L*1000L)
If you need a one-liner (i.e. for Jasper Reports formula) and don't mind if the adjustment is not exactly one month (i.e "30 days" is enough):
new Date($F{invoicedate}.getTime() + 30L * 24L * 60L * 60L * 1000L)
此方法返回当前日期加上 1 个月。
This method returns the current date plus 1 month.
然后调用方法:
then call method:
对于 Java 8 及更高版本,请使用 LocalDate 类的 plusMonths() 方法。
// 添加一个月到当前本地日期
// 添加一个月到任何本地日期对象
参考: https://www.javaexercise.com/java/java-add-months-to-date
Use the plusMonths() method of the LocalDate class for Java 8 and Higher Versions.
// Add one month to the current local date
// Add one month to any local date object
Reference: https://www.javaexercise.com/java/java-add-months-to-date
你可以这样使用;
You can use like this;
为了找到一个月后的日子,有必要查看今天是该月的哪一天。
因此,如果这一天是该月的第一天,请运行以下代码,
如果该天是该月的最后一天,请运行以下代码。
如果这一天是二月,则运行以下代码,
以下代码用于其他情况。
In order to find the day after one month, it is necessary to look at what day of the month it is today.
So if the day is first day of month run following code
if the day is last day of month, run following codes.
if the day is in february run following code
the following codes are used for other cases.
常量是葡萄牙语,因为是,但 javadoc 是足够容易理解的。
只要打电话
就可以了。相关代码:
Constants are in Portuguese because yes, but javadoc is understandable enough.
Just call
and that's that. Related code:
输出:
Output: