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.
This is also similar to the how to add minutes to the current date in java 8?
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:
No comments:
Post a Comment
Please do not add any spam links in the comments section.