Pages

Footer Pages

Spring Boot

Java String API

Java Conversions

Kotlin Programs

Kotlin Conversions

Java Threads Tutorial

Java 8 Tutorial

Tuesday, November 17, 2020

How To Increment/Decrement Date Using Joda DateTime

1. Overview

In this quick tutorial, you will learn how to add days to the current date and time using joda date time api.

And also how to remove or set back to old date using joda date time api.

First, you need to add the joda date time dependency to the project using the below maven dependency.
<dependency>
    <groupId>joda-time</groupId>
    <artifactId>joda-time</artifactId>
    <version>2.10.8</version>
</dependency>
Joda Time api has one class org.joda.time.DateTime class with several methods to properly deal with the date and time operations.

DateTime class is implementation of unmodifiable DateTime and this is immutable.

How To Increment/Decrement Date Using Joda DateTime



2. Increment Date Using Joda Date Time API


Next, let us look into the DateTime api methods which can add some days to the current date.

DateTime api class several method to add or increment the date and time with the following methods.

All methods starts with plusXXX() are to increment the particular part of date time value.

All plus methods:

plusYears(int n) - to increase n years
plusMonths(int n) - to increase n months
plusWeeks(int n) - to increase n weeks
plusDays(int n) - to increase n days
plusHours(int n) - to increase n hours
plusMinutes(int n) - to increase n minutes
plusSeconds(int n) - to increase n seconds
plusMillis(int n) - to increase n milli seconds


joda datetime plusDays + plusXXX methods

import org.joda.time.DateTime;

public class JodaDateIncrement {

	public static void main(String[] args) {

		String dateFormat = "yyyy-MM-dd";

		// creating joda date time object
		DateTime dateTime = new DateTime();

		// printing the current date in format "yyyy-MM-dd"
		System.out.println("Joda current date time : " + getDate(dateTime, dateFormat));

		// incrementing two days to the current date
		dateTime = dateTime.plusDays(2);

		// printing the modified datetime object
		System.out.println("Joda date after adding two days : " + getDate(dateTime, dateFormat));

	}

	// getting the date in the given format
	private static String getDate(DateTime dateTime, String dateFormat) {
		return dateTime.toString(dateFormat);
	}

}
Output:
Joda current date time : 2020-11-17
Joda date after adding two days : 2020-11-19

In the above example, first created current date time with DateTime class and add two days using plusDays() method. Finally, used the toString("yyyy-MM-dd") method with a format to view the date in the readable format.

We have used only plusDays() method but you can play around with the remaining plusXXX series methods.

3. Decrement Date Using Joda Date Time API


As above similar to the plus methods, Joda DateTime also has methods for minus date operations.

In the below program, we called minusDays(2), minusMonths(2) and minusYears(2) methods. So, it sets 2 months, 2 days and 2 years back to the current date.
package com.javaprogramto.java8.dates.conversion;

import org.joda.time.DateTime;
/**
 * A simple example to set the current date to old date using joda api.  
 * 
 * @author javaprogramto.com
 *
 */
public class JodaDateDecrement {

	public static void main(String[] args) {

		String dateFormat = "yyyy-MM-dd";

		// creating joda date time object
		DateTime dateTime = new DateTime();

		// printing the current date in format "yyyy-MM-dd"
		System.out.println("Joda current date time : " + getDate(dateTime, dateFormat));

		// decreasing two years, two months and two days to the current date
		dateTime = dateTime.minusDays(2).minusMonths(2).minusYears(2);

		// printing the modified datetime object
		System.out.println("Joda date after setting old date: " + getDate(dateTime, dateFormat));

	}

	// getting the date in the given format
	private static String getDate(DateTime dateTime, String dateFormat) {
		return dateTime.toString(dateFormat);
	}

}
Output:
Joda current date time : 2020-11-17
Joda date after setting old date: 2018-09-15
As you can see in the output that original current date is moved back to 2 years, 2 months and 2 days.

Note: But, you need to store the result back to the same object "datetime". If you do not apply this step then result would not take into effect. This rules applies to the all plus and minus series methods.

All minus methods:

minusYears(int n) - to minus n years
minusMonths(int n) - to minus n years
minusWeeks(int n) - to minus n years
minusDays(int n) - to minus n years
minusHours(int n) - to minus n years
minusMinutes(int n) - to minus n years
minusSeconds(int n) - to minus n years
minusMillis(int n) - to minus n years



joda datetime minusDays + minusXXX methods

4. Conclusion


In this quick article, you've seen how to work with date increment and decrement operations with Joda DateTime api methods.

Examples are shown with plusXXX() and minusXXX() Series methods.

GitHub:



No comments:

Post a Comment

Please do not add any spam links in the comments section.