import java.util.Date; import java.util.GregorianCalendar; public class CalenderTest { public static void main(String[] args) { getCurrentDate(); addDate(); substractDate(); updateDate(); } public static void getCurrentDate() { GregorianCalendar cal = (GregorianCalendar) GregorianCalendar .getInstance(); Date date = cal.getTime(); System.out.println("Current date & time is :" + date); } public static void addDate() { GregorianCalendar cal = (GregorianCalendar) GregorianCalendar .getInstance(); cal.add((GregorianCalendar.MONTH), 2); cal.add((GregorianCalendar.DAY_OF_MONTH), 2); cal.add((GregorianCalendar.DATE), 2); Date date = cal.getTime(); System.out.println("New date & time is :" + date); } public static void substractDate() { GregorianCalendar cal = (GregorianCalendar) GregorianCalendar .getInstance(); cal.add((GregorianCalendar.MONTH), -2); cal.add((GregorianCalendar.DAY_OF_MONTH), -2); Date date = cal.getTime(); System.out.println("New date & time is :" + date); } public static void updateDate() { GregorianCalendar cal = (GregorianCalendar) GregorianCalendar .getInstance(); cal.set((GregorianCalendar.MONTH), 6); cal.set((GregorianCalendar.DAY_OF_MONTH), 10); cal.set((GregorianCalendar.YEAR), 2014); Date date = cal.getTime(); System.out.println("Updated date & time is :" + date); } } OUTPUT: Current date & time is :Thu Oct 06 13:57:00 IST 2016 New date & time is :Sat Dec 10 13:57:00 IST 2016 New date & time is :Thu Aug 04 13:57:00 IST 2016 Updated date & time is :Thu Jul 10 13:57:00 IST 2014
0 comments :
Post a Comment