0% found this document useful (0 votes)
17 views1 page

Date Currentdate

This code sample demonstrates how to manipulate dates in Java by first creating a Date object for the current date, converting it to a Calendar object, adding time to the Calendar through methods like addYears and addMonths, and then converting it back to a Date object to display the results. It adds one year, month, date, hour, minute, and second to the current date, and prints out the original and manipulated dates.

Uploaded by

genriel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views1 page

Date Currentdate

This code sample demonstrates how to manipulate dates in Java by first creating a Date object for the current date, converting it to a Calendar object, adding time to the Calendar through methods like addYears and addMonths, and then converting it back to a Date object to display the results. It adds one year, month, date, hour, minute, and second to the current date, and prints out the original and manipulated dates.

Uploaded by

genriel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 1

Date currentDate = new Date();

System.out.println(dateFormat.format(currentDate));

// convert date to calendar


Calendar c = Calendar.getInstance();
c.setTime(currentDate);

// manipulate date
c.add(Calendar.YEAR, 1);
c.add(Calendar.MONTH, 1);
c.add(Calendar.DATE, 1); //same with c.add(Calendar.DAY_OF_MONTH, 1);
c.add(Calendar.HOUR, 1);
c.add(Calendar.MINUTE, 1);
c.add(Calendar.SECOND, 1);

// convert calendar to date


Date currentDatePlusOne = c.getTime();

System.out.println(dateFormat.format(currentDatePlusOne));

You might also like