$show=/label

How To Add Days To Current Date In Java 8? Increment/Decrement Date Examples

SHARE:

A quick example guide, how to add or increment the days to the current date and time in java api and also with new java 8 date time api.

1. Overview

In this quick tutorial, you'll learn how to increment the days to the current date and time in java.

This is quite regularly used in the software developer life.

Let us look into the examples using java.util.calendar api and new java 8 Date Time api.


How To Add Days To Current Date In Java 8? Increment/Decrement Date Examples



2. Example to add days to current time with Calendar.add() method


Java Calendar api has a special add() method that increment the given number for given unit.

Look at the below example that adds the 2 years 2 months 2 days to the current date.

Read the comments in the program for better understanding.
package com.javaprogramto.java8.dates;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class AddDaysExample {
	public static void main(String[] args) {
		// creating date in java
		Date date = new Date();
		
		// printing the date in the readable format.
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		String dateStr = dateFormat.format(date);
		System.out.println("Current date : "+dateStr);
		
		//creating calender instance 
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		
		// adding 2 years, months and date
		calendar.add(Calendar.YEAR, 2);
		calendar.add(Calendar.MONTH, 2);
		calendar.add(Calendar.DATE, 2);
		
		// getting the new date from calendar
		Date addedDate = calendar.getTime();
		
		// printing the new date
		String newDateinStr = dateFormat.format(addedDate);
		System.out.println("New date after adding 2 years : "+newDateinStr);
		
	}
}
Output:
Current date : 2020-11-16
New date after adding 2 years : 2023-01-18

3. Example to add days to current time with new Java 8 Date API


New java 8 api has come up with the new methods such as plus and minus date operations.

All LocalDateTime methods can be used as chained methods but need to store the result into localdatetime variable.

3.1 plusYears() + plusMonths() + plusDays() Example


Next, let us increment the current date by 2 years, 2 months, 2 days, 2 hours, 2 minutes, 2 seconds.

plus methods are declared as read friendly such plusYears(), plusMonths(), plusDays() and easy to remember.

plusYears() - to add years
plusMonths() - to add months
plusDays()  - to add days

plusHours() - to add hours
plusMinutes() - to add minutes
plusSeconds() - to add seconds

package com.javaprogramto.java8.dates;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;

public class Java8AddDaysExample {
	public static void main(String[] args) {
		// date formats
		String format = "yyyy-MM-dd hh:mm:ss";
		SimpleDateFormat dateFormat = new SimpleDateFormat(format);

		// Date format in java 8
		DateTimeFormatter dateFormat8 = DateTimeFormatter.ofPattern(format);

		// creating date in java
		Date date = new Date();

		// printing the date in the readable format.
		String dateStr = dateFormat.format(date);
		System.out.println("Current date : " + dateStr);

		// converting date to LocalDateTime
		LocalDateTime localDateTime = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
		System.out.println("localDateTime : " + dateFormat8.format(localDateTime));

		localDateTime = localDateTime.plusYears(2);
		localDateTime = localDateTime.plusMonths(2);
		localDateTime = localDateTime.plusDays(2);

		localDateTime = localDateTime.plusHours(2).plusMinutes(2).plusSeconds(2);

		// converting LocalDateTime to Date
		Date currentDatePlus = Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());

		// printing the new date
		String newDateinStr = dateFormat.format(currentDatePlus);
		System.out.println("New date after adding 2 years : " + newDateinStr);

	}
}

Output:
Current date : 2020-11-16 03:00:43
localDateTime : 2020-11-16 03:00:43
New date after adding 2 years : 2023-01-18 05:02:45

3.2 minusYears() + minusMonths() + minusDays() Example


Next, let us decrement the current date by 2 years, 2 months, 2 days, 2 hours, 2 minutes, 2 seconds.

minusYears() - to decrement or minus years
minusMonths() - to decrement or minus months
minusDays()  - to decrement or minus days

minusHours() - to decrement or minus hours
minusMinutes() - to decrement or minus minutes
minusSeconds() - to decrement or minus seconds

package com.javaprogramto.java8.dates;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;

/**
 * To remove the date from current date time.
 * 
 * @author javaprogramto.com
 *
 */
public class Java8MinusDaysExample {
	public static void main(String[] args) {
		// date formats
		String format = "yyyy-MM-dd hh:mm:ss";
		SimpleDateFormat dateFormat = new SimpleDateFormat(format);

		// Date format in java 8
		DateTimeFormatter dateFormat8 = DateTimeFormatter.ofPattern(format);

		// creating date in java
		Date date = new Date();

		// printing the date in the readable format.
		String dateStr = dateFormat.format(date);
		System.out.println("Current date : " + dateStr);

		// converting date to LocalDateTime
		LocalDateTime localDateTime = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
		System.out.println("localDateTime : " + dateFormat8.format(localDateTime));

		localDateTime = localDateTime.minusYears(2);
		localDateTime = localDateTime.minusMonths(2);
		localDateTime = localDateTime.minusDays(2);

		localDateTime = localDateTime.minusHours(2).minusMinutes(2).minusSeconds(2);

		// converting LocalDateTime to Date
		Date currentDateminus = Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());

		// printing the new date
		String newDateinStr = dateFormat.format(currentDateminus);
		System.out.println("New date after going back 2 years : " + newDateinStr);

	}
}
Output:
Current date : 2020-11-16 03:04:00
localDateTime : 2020-11-16 03:04:00
New date after going back 2 years : 2018-09-14 01:01:58

4. Conclusion


In this quick article, you've learnt how to increment or decrement the date in old java and new java 8 Date time api.

GitHub


Read next:




COMMENTS

BLOGGER

About Us

Author: Venkatesh - I love to learn and share the technical stuff.
Name

accumulo,1,ActiveMQ,2,Adsense,1,API,37,ArrayList,16,Arrays,16,Bean Creation,3,Bean Scopes,1,BiConsumer,1,Blogger Tips,1,Books,1,C Programming,1,Collection,5,Collections,23,Collector,1,Command Line,1,Compile Errors,1,Configurations,7,Constants,1,Control Statements,8,Conversions,6,Core Java,84,Corona India,1,Create,2,CSS,1,Date,3,Date Time API,12,Dictionary,1,Difference,1,Download,1,Eclipse,2,Efficiently,1,Error,1,Errors,1,Exception,1,Exceptions,3,Fast,1,Files,10,Float,1,Font,1,Form,1,Freshers,1,Function,3,Functional Interface,2,Garbage Collector,1,Generics,4,Git,4,Grant,1,Grep,1,HashMap,1,HomeBrew,2,HTML,2,HttpClient,2,Immutable,1,Installation,1,Interview Questions,5,Iterate,2,Jackson API,3,Java,30,Java 10,1,Java 11,5,Java 12,5,Java 13,2,Java 14,2,Java 8,77,Java 8 Difference,2,Java 8 Stream Conversions,2,java 8 Stream Examples,3,Java 9,1,Java Conversions,11,Java Design Patterns,1,Java Files,1,Java Program,3,Java Programs,93,java.lang,5,java.util. function,1,jQuery,1,Kotlin,10,Kotlin Conversions,3,Kotlin Programs,6,Lambda,1,lang,29,Leap Year,1,live updates,1,Logging,1,Mac OS,2,Math,1,Matrix,5,Maven,1,Method References,1,Mockito,1,MongoDB,3,New Features,1,Operations,1,Optional,6,Oracle,5,Oracle 18C,1,Partition,1,Patterns,1,Programs,1,Property,1,Python,2,Quarkus,1,Read,1,Real Time,1,Recursion,2,Remove,2,Rest API,1,Schedules,1,Serialization,1,Servlet,2,Sort,1,Sorting Techniques,8,Spring,2,Spring Boot,23,Spring Email,1,Spring MVC,1,Streams,23,String,58,String Programs,12,String Revese,1,Swing,1,System,1,Tags,1,Threads,10,Tomcat,1,Tomcat 8,1,Troubleshoot,16,Unix,2,Updates,3,util,5,While Loop,1,
ltr
item
JavaProgramTo.com: How To Add Days To Current Date In Java 8? Increment/Decrement Date Examples
How To Add Days To Current Date In Java 8? Increment/Decrement Date Examples
A quick example guide, how to add or increment the days to the current date and time in java api and also with new java 8 date time api.
https://1.bp.blogspot.com/-omaL0Py7ohA/X7JKK1q7cVI/AAAAAAAADDY/1M1GaUIJhKsoNL76cdoJ9ikNgakpL05QQCLcBGAsYHQ/w640-h470/How%2BTo%2BAdd%2BDays%2BTo%2BCurrent%2BDate%2BIn%2BJava%2B8%253F%2BIncrement%253ADecrement%2BDate%2BExamples.png
https://1.bp.blogspot.com/-omaL0Py7ohA/X7JKK1q7cVI/AAAAAAAADDY/1M1GaUIJhKsoNL76cdoJ9ikNgakpL05QQCLcBGAsYHQ/s72-w640-c-h470/How%2BTo%2BAdd%2BDays%2BTo%2BCurrent%2BDate%2BIn%2BJava%2B8%253F%2BIncrement%253ADecrement%2BDate%2BExamples.png
JavaProgramTo.com
https://www.javaprogramto.com/2020/11/how-to-add-days-to-current-date-in-java.html
https://www.javaprogramto.com/
https://www.javaprogramto.com/
https://www.javaprogramto.com/2020/11/how-to-add-days-to-current-date-in-java.html
true
3124782013468838591
UTF-8
Loaded All Posts Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS PREMIUM CONTENT IS LOCKED STEP 1: Share to a social network STEP 2: Click the link on your social network Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy Table of Content