JAVA EXAMPLE PROGRAMS

JAVA EXAMPLE PROGRAMS

Publish Your Article Here

How to add/substract days to Date in Java 8?


This page shows examples on how to add/substract days, hours, weeks, months or years to date object using Java8 new date and time APIs. Java 8 introduced plus and minus related methods to make it easier.

AddDaysToDateEx
package com.java2novice.datetime;

import java.time.LocalDateTime;

public class AddDaysToDateEx {

	public static void main(String[] args) {

		LocalDateTime ldTime = LocalDateTime.now();
		System.out.println("Current date time: "+ldTime);

		// add 10 days to current time
		LocalDateTime advLdTime = ldTime.plusDays(10);
		System.out.println("Date time after 10 days: "+advLdTime);

		// add 5 hours to crrent time
		LocalDateTime advLdTime1 = ldTime.plusHours(5);
		System.out.println("Date time after adding 5 hours: "+advLdTime1);

		// date one month before
		LocalDateTime prevLdTime = ldTime.minusMonths(1);
		System.out.println("Date one month before: "+prevLdTime);

		// date two week before
		LocalDateTime prevLdTime1 = ldTime.minusWeeks(2);
		System.out.println("Date two weeks before: "+prevLdTime1);
	}
}

Output:
Current date time: 2015-12-16T09:38:15.975
Date time after 10 days: 2015-12-26T09:38:15.975
Date time after adding 5 hours: 2015-12-16T14:38:15.975
Date one month before: 2015-11-16T09:38:15.975
Date two weeks before: 2015-12-02T09:38:15.975
<< Previous Program 

Java-8 Date and Time API Examples

  1. Java 8 LocalDateTime example.
  2. Java 8 ZonedDateTime example.
  3. Java 8 LocalDate example.
  4. Java 8 LocalTime example.
  5. Create date time with custom values in Java 8.
  6. Java 8 Instant (Timestamp) example.
  7. Java 8 Period example.
  8. Java 8 Duration example.
  9. Java 8 Date parsing and formatting example.
  10. How to compare two dates in Java 8?
  11. How to get current date and time Java 8?
  12. How to calculate execution / elapsed time Java 8?
  13. How to convert string to Date Java 8?
  14. How to add/substract days to Date in Java 8?
Knowledge Centre
Transient and Volatile Modifiers
Transient: The transient modifier applies to variables only and it is not stored as part of its object's Persistent state. Transient variables are not serialized.

Volatile: Volatile modifier applies to variables only and it tells the compiler that the variable modified by volatile can be changed unexpectedly by other parts of the program.
Famous Quotations
We know what we are, but know not what we may be.
-- William Shakespeare

About Author

I'm Nataraja Gootooru, programmer by profession and passionate about technologies. All examples given here are as simple as possible to help beginners. The source code is compiled and tested in my dev environment.

If you come across any mistakes or bugs, please email me to [email protected].

Most Visited Pages

Other Interesting Sites

Reference: Java™ Platform Standard Ed. 7 - API Specification | Java™ Platform Standard Ed. 8 - API Specification | Java is registered trademark of Oracle.
Privacy Policy | Copyright © 2022 by Nataraja Gootooru. All Rights Reserved.