Java date add day


 

Java date add day

In this tutorial, you will learn how to add days to date.

In this tutorial, you will learn how to add days to date.

Java date add day

In this tutorial, you will learn how to add days to date.

Sometimes, there is a need to manipulate the date and time (like adding days to date or subtract days from date) for java applications. Here, we are going to add few days to current date and return the date of that day. For this, we have created a calendar instance and get a date to represent the current date. Then using the method add() of Calendar class, we have added 4 days to the calendar and using the Date class, we have got the date of that day.

Example

import java.util.*;

public class AddDaysToDate{

  public static void main(String[] args) {
    Calendar calendar = Calendar.getInstance();
    Date today = calendar.getTime();
    System.out.println("Today's Date: " + today);
    calendar.add(Calendar.DAY_OF_YEAR, 4);
    Date addDays = calendar.getTime();
    System.out.println("Date after 4 days: " + addDays);
  }
}

Output:

Today's Date: Tue Oct 09 12:32:36 IST 2012
Date after 4 days: Sat Oct 13 12:32:36 IST 2012

Ads