Java – How to add days to current date
1. Calendar.add
Example to add 1 year, 1 month, 1 day, 1 hour, 1 minute and 1 second to the current date.
package com.mkyong.time;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class DateExample {
private static final DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
public static void main(String[] args) {
Date currentDate = new Date();
System.out.println(dateFormat.format(currentDate));
// convert date to calendar
Calendar c = Calendar.getInstance();
c.setTime(currentDate);
// manipulate date
c.add(Calendar.YEAR, 1);
c.add(Calendar.MONTH, 1);
c.add(Calendar.DATE, 1); //same with c.add(Calendar.DAY_OF_MONTH, 1);
c.add(Calendar.HOUR, 1);
c.add(Calendar.MINUTE, 1);
c.add(Calendar.SECOND, 1);
// convert calendar to date
Date currentDatePlusOne = c.getTime();
System.out.println(dateFormat.format(currentDatePlusOne));
}
}
Output
2016/11/10 17:11:48
2017/12/11 18:12:49
2. Java 8 Plus Minus
In Java 8, you can use the plus and minus methods to manipulate LocalDate, LocalDateTime and ZoneDateTime, see the following examples
package com.mkyong.time;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;
public class LocalDateTimeExample {
private static final String DATE_FORMAT = "yyyy/MM/dd HH:mm:ss";
private static final DateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT);
private static final DateTimeFormatter dateFormat8 = DateTimeFormatter.ofPattern(DATE_FORMAT);
public static void main(String[] args) {
// Get current date
Date currentDate = new Date();
System.out.println("date : " + dateFormat.format(currentDate));
// convert date to localdatetime
LocalDateTime localDateTime = currentDate.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
System.out.println("localDateTime : " + dateFormat8.format(localDateTime));
// plus one
localDateTime = localDateTime.plusYears(1).plusMonths(1).plusDays(1);
localDateTime = localDateTime.plusHours(1).plusMinutes(2).minusMinutes(1).plusSeconds(1);
// convert LocalDateTime to date
Date currentDatePlusOneDay = Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
System.out.println("\nOutput : " + dateFormat.format(currentDatePlusOneDay));
}
}
Output
date : 2016/11/10 17:40:11
localDateTime : 2016/11/10 17:40:11
Output : 2017/12/11 18:41:12
http://www.mkyong.com/java/java-how-to-add-days-to-current-date/
Java – How to add days to current date的更多相关文章
- Java – How to get current date time
Java – How to get current date time 1. Code SnippetsFor java.util.Date, just create a new Date() Dat ...
- Java中的日期(Calendar、Date)
(1)获取当前日期: java.util.Calendar calendar = java.util.Calendar.getInstance(); 或 = new java.util.Gregor ...
- Java中如何将String转成Date
Java中如何将String转成Date 最近在开发Json数据反序列化为Java对象的时候发现spring mvc 和 Jackson 对Date类型对支持不是特别好,虽然在Java对象序列化为Js ...
- Attempt to invoke interface method 'boolean java.util.List.add(java.lang.Object)' on a null object reference
尝试在一个空的对象引用上引用boolean java.util.List.add()这个方法: 错误例子: private ArrayList<String> classList; cla ...
- Java进阶(二十四)Java List集合add与set方法原理简介
Java List集合add与set方法原理简介 add方法 add方法用于向集合列表中添加对象. 语法1 用于在列表的尾部插入指定元素.如果List集合对象由于调用add方法而发生更改,则返回 tr ...
- java Queue中 add/offer,element/peek,remove/poll区别
转自https://blog.csdn.net/u012050154/article/details/60572567 java Queue中 add/offer,element/peek,remov ...
- 论Java的ArrayList.add(e)和C++的vector.push_back(e)的区别
Java的ArrayList和C++的vector很类似,都是很基本的线性数据结构.但是他们的表现却不同. 在工作中碰到一个问题就是,搞不清楚到底传进去的是一个新对象,还是当前对象的引用! 经过实战分 ...
- Attempt to invoke interface method 'boolean java.util.List.add(java.lang.Object)' on a null
1.Android Studio报错 Attempt to invoke interface method 'boolean java.util.List.add(java.lang.Object)' ...
- Java中日期格式(String、Date、Calendar)的相互转换
package day20190318; import java.text.ParseException; import java.text.SimpleDateFormat; import java ...
随机推荐
- vsphere性能
vNUMA介绍 http://virtualbarker.com/ vSphere VMware Performance With every release of vSphere the overh ...
- Struts2添加了<s:debug>后页面无效果的解决方案
一.环境 Struts2版本 struts2.5 二.问题 在jsp页面中添加了<s:debug>后页面上无任何展示. 三.解决 在struts.xml中的struts节点下添加如下常量即 ...
- miniOrm
PetaPoco.Core https://www.nuget.org/packages/PetaPoco.Core/5.1.228
- SSH协议
SSH是一种协议,实现计算机之间的加密登录,即使被截获,截获的也只是加密后的密文,不会泄密. 如果每次登录另外一台计算机,都需要输入密码,就显得太麻烦,所以SSH协议实现了无密码登录,即公钥登录.所谓 ...
- ES6学习笔记四:Proxy与Reflect
一:Proxy 代理. ES6把代理模式做成了一个类,直接传入被代理对象.代理函数,即可创建一个代理对象,然后我们使用代理对象进行方法调用,即可调用被包装过的方法: 1)创建 var proxy = ...
- Definitaion of 'utsname' must be imported from module 'Darwin.POSIX.sys.utsname' before it is required
https://stackoverflow.com/questions/34430354/objective-c-gettimeofday-must-be-imported
- selenium 定制启动 chrome 的选项
序 使用 selenium 时,我们可能需要对 chrome 做一些特殊的设置,以完成我们期望的浏览器行为,比如阻止图片加载,阻止JavaScript执行 等动作.这些需要 selenium的 Chr ...
- 洛谷P1387 最大正方形
题目描述 题目链接:https://www.luogu.org/problemnew/show/P1387 在一个n*m的只包含0和1的矩阵里找出一个不包含0的最大正方形,输出边长. 输入输出格式 输 ...
- Ubuntu16.04 Arduino UNO R3开发板
使用的是国内改过的版本, usb芯片换成了ch341, 晶振不是原版的16MHz而是12MHz, 杜邦线孔布局和原版一致. 设备连接 在Ubuntu16.04下不需要驱动, 能直接认出ch341设备 ...
- net.sf.json.JSONException: 'object' is an array. Use JSONArray instead
list集合转换JSON出错误 意思是:对象"是一个数组. 使用jsonarray取代. 解决方法: 将JSONObject替换为JSONArray 代码: JsonConfig jsonC ...