/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package test;
import java.util.Calendar;
public class DateTimeUtility {
public static void main(String[] args) {
int addDays = 4;
int subtractDays = 10;
int addHours=3;
int subtractHours=26;
Calendar cal = Calendar.getInstance();
//System.out.println(cal);
//Current date
System.out.println("Today's date : " + (cal.get(Calendar.MONTH) + 1) +
"/" + cal.get(Calendar.DATE) + "/" + cal.get(Calendar.YEAR));
//Current Time
System.out.println("Current Time : " + (cal.get(Calendar.HOUR) ) +
":" + cal.get(Calendar.MINUTE) + ":" + cal.get(Calendar.SECOND));
//Adding days
cal.add(Calendar.DATE, addDays);
System.out.println("Date (after): " + (cal.get(Calendar.MONTH) + 1) +
"/" + cal.get(Calendar.DATE) + "/" + cal.get(Calendar.YEAR));
cal = Calendar.getInstance();
//subtracting days
cal.add(Calendar.DATE, -subtractDays);
System.out.println("Date (before): " + (cal.get(Calendar.MONTH) + 1) +
"/" + cal.get(Calendar.DATE) + "/" + cal.get(Calendar.YEAR));
//Adding Hours
cal.add(Calendar.HOUR, addHours);
System.out.println("New Time after adding " + addHours +" Hours "+
cal.get(Calendar.HOUR) + ":" + cal.get(Calendar.MINUTE) +
":" + cal.get(Calendar.SECOND));
cal=Calendar.getInstance();
//Subtracting Hours
cal.add(Calendar.HOUR, -subtractHours);
System.out.println("Time before " + subtractHours+" Hours "+
(cal.get(Calendar.HOUR) ) + ":" + cal.get(Calendar.MINUTE) +
":" + cal.get(Calendar.SECOND));
}
}
OUTPUT :
Today's date : 2/22/2014
Current Time : 6:22:6
Date (after): 2/26/2014
Date (before): 2/12/2014
New Time after adding 3 Hours 9:22:6
Time before 26 Hours 4:22:6
No comments:
Post a Comment