Java Add Time To Date Example
Chapter:
Date and Time
Last Updated:
02-09-2016 08:42:56 UTC
Program:
/* ............... START ............... */
import java.util.*;
public class JavaAddTimeToDate {
public static void main(String[] args) throws Exception {
Date date = new Date();
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
System.out.println("today is " + date.toString());
calendar.add(Calendar.MONTH, 1);
System.out.println("date after a month will be " + calendar.getTime().toString());
calendar.add(Calendar.HOUR, 70);
System.out.println("date after 7 hrs will be " + calendar.getTime().toString());
calendar.add(Calendar.YEAR, 3);
System.out.println("date after 3 years will be " + calendar.getTime().toString());
}
}
/* ............... END ............... */
Output
today is Fri Sep 02 12:35:28 IST 2016
date after a month will be Sun Oct 02 12:35:28 IST 2016
date after 7 hrs will be Wed Oct 05 10:35:28 IST 2016
date after 3 years will be Sat Oct 05 10:35:28 IST 2019
Tags
Add Time To Date, Java, Date And Time