Date Manipulation Techniques in Java

Manipulating dates is a common requirement in Java programs. Usually you want to add or subtract a fixed number of days, months or years to a given date. In Java, this can be easily achieved using the java.util.Calendar class. Calendar class can also work with java.util.Date.

The following program shows how date manipulation can be done in Java using the Calendar class. Please note that the following examples uses the system time zone,

import java.util.Calendar;
import java.util.Date;

/**
 * This program demonstrates various date manipulation techniques in Java 
 */
public class DateManipulator {
    public static void main(String[] args) {
        DateManipulator dm = new DateManipulator();
        Date curDate = new Date();
        System.out.println("Current date is "+curDate);
        
        Date after5Days = dm.addDays(curDate,5);
        System.out.println("Date after 5 days is "+after5Days);
        
        Date before5Days = dm.addDays(curDate,-5);
        System.out.println("Date before 5 days is "+before5Days);
        
        Date after5Months = dm.addMonths(curDate,5);
        System.out.println("Date after 5 months is "+after5Months);
        
        Date after5Years = dm.addYears(curDate,5);
        System.out.println("Date after 5 years is "+after5Years);
    }
    
	// Add days to a date in Java
    public Date addDays(Date date, int days) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.DATE, days);
        return cal.getTime();
    }
    
	// Add months to a date in Java
    public Date addMonths(Date date, int months) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.MONTH, months);
        return cal.getTime();
    }
    
	// Add years to a date in Java
    public Date addYears(Date date, int years) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.YEAR, years);
        return cal.getTime();
    }
}

 

There are a number of third party libraries which provides these data manipulation methods and much more. For advanced date processing in Java, please use Apache Commons Lang library or Joda-Time.

If you are using Java 8, use the classes in java.time for date manipulation. The following code demonstrates date manipulation in Java 8 using java.time classes,

import java.time.LocalDate;
/**
 * This program demonstrates various date manipulation techniques in Java 8 using java.time.LocalDate
 */
public class DateManipulator8 {
    // This program works only in Java8
    public static void main(String[] args) {
        DateManipulator dm = new DateManipulator();
        LocalDate curDate = LocalDate.now();
        System.out.println("Current date is "+curDate);
       
        System.out.println("Date after 5 days is "+curDate.plusDays(5));
        System.out.println("Date before 5 days is "+curDate.plusDays(5));
        System.out.println("Date after 5 months is "+curDate.plusMonths(5));
        System.out.println("Date after 5 years is "+curDate.plusYears(5));
    }
}