[英]Addition and Subtraction of Dates in Java
我们如何在 java 中添加或减去日期? 例如java.sql.Date
格式如下: yyyy-MM-dd ,我如何从中添加 5 个月? 我在一些教程中看到他们正在使用Calendar
,我们可以在上面设置日期吗? 请帮忙。
示例: 2012-01-01
添加 5 个月后将变为2012-06-01
。
PS:本人是.Net Programmer,正在慢慢学习Java环境。
首先,您必须将String
日期转换为java.util.Date
,而不是必须使用java.util.Calendar
来操作日期。 也可以对毫秒进行数学运算,但是我不建议这样做。
public static void main( final String[] args ) throws ParseException {
final String sdate = "2012-01-01";
final SimpleDateFormat df = new SimpleDateFormat( "yyyy-MM-dd" );
final Date date = df.parse( sdate ); // conversion from String
final java.util.Calendar cal = GregorianCalendar.getInstance();
cal.setTime( date );
cal.add( GregorianCalendar.MONTH, 5 ); // date manipulation
System.out.println( "result: " + df.format( cal.getTime() ) ); // conversion to String
}
清除用于日期数学的内置Date类。 看一下JodaTime,它对于此类事情具有更好的API。
使用Calendar
Calendar cal = Calendar.getInstance();
cal.add(Calendar.MONTH, 5);
要将日期转换为日历,请使用:
Date date = your_date_here;
Calendar cal = Calendar.getInstance();
cal.setTime(date);
然后使用日历算术函数进行加/减:
cal.add(Calendar.MONTH, 5);
或者,将日期转换为时间以毫秒为单位。 进行数学运算,然后将毫秒转换回日期。
使用Google软件包GWT中的CalenderUtils
。
import com.google.gwt.user.datepicker.client.CalendarUtil;
...
//now
Date d = new Date();
// Now + 2 months
CalendarUtil.addMonthsToDate(d, 2);
另一个选择是来自第三方Apache Commons库集合的DateUtils类。 例:
Date d = DateUtils.parseDate("2012-01-01", "yyyy-MM-dd");
Date d2 = DateUtils.addMonths(d, 5);
System.out.println("Old date + 5 months = " + d2);
有多种方法。 其中之一可能与joda.time 有关。 这不会通过使用Calendar来回答问题,而是在某人需要时使用另一种方法。 :D
import java.sql.Date;
import org.joda.time.DateTime;
//
DateTime datetime = new DateTime("2012-01-01");
Date dt = new Date(datetime.plusMonths(5).toDate().getTime());
System.out.println(dt);
// This gives output as 2012-06-01
PS:快乐编码 Java
The accepted answer uses java.util
date-time API and SimpleDateFormat
which was the correct thing to do in 2012. In Mar 2014, the java.util
date-time API and their formatting API, SimpleDateFormat
were supplanted by the modern date-time API . 从那时起,强烈建议停止使用旧版日期时间 API。
java.time
的解决方案,现代日期时间 API: 您的日期字符串不需要DateTimeFormatter
: java.time
API 基于ISO 8601 ,因此您不需要指定DateTimeFormatter
来解析已经采用 ISO 8601 格式的日期时间字符串,例如您的日期字符串, 2012-01-01
可以直接解析为仅包含日期单位的LocalDate
实例。
将日期字符串解析为LocalDate
后,您可以对其添加或减去不同的日期单位,例如年、月、日等。
演示:
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
class Main {
public static void main(String args[]) {
LocalDate date = LocalDate.parse("2012-01-01");
System.out.println(date);
LocalDate afterFiveMonths = date.plusMonths(5);
LocalDate beforeFiveMonths = date.minusMonths(5);
System.out.println(afterFiveMonths);
System.out.println(beforeFiveMonths);
// Alternatively,
afterFiveMonths = date.plus(5, ChronoUnit.MONTHS);
beforeFiveMonths = date.minus(5, ChronoUnit.MONTHS);
System.out.println(afterFiveMonths);
System.out.println(beforeFiveMonths);
}
}
Output :
2012-01-01
2012-06-01
2011-08-01
2012-06-01
2011-08-01
从Trail:Date Time中了解有关现代日期时间 API 的更多信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.