Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reduce one month from current date and stored in date variable using java?

How to reduce one month from current date and want to sore in java.util.Date variable im using this code but it's shows error in 2nd line

 java.util.Date da = new Date();  da.add(Calendar.MONTH, -1); //error 

How to store this date in java.util.Date variable?

like image 699
karthi Avatar asked May 06 '13 06:05

karthi


People also ask

How can I decrement a date by one day in Java?

DateTime yesterday = new DateTime(). minusDays(1);

How do I add 30 days to a specific date in Java?

time. ZonedDateTime zdtMonthLater = zdt. plusMonths( 1 );

How do I add one month to current date in Java?

The plusMonths() method of LocalDate class in Java is used to add the number of specified months in this LocalDate and return a copy of LocalDate. This method adds the months field in the following steps: Add the months to the month-of-year field. Check if the date after adding months is valid or not.


1 Answers

Use Calendar:

Calendar cal = Calendar.getInstance(); cal.add(Calendar.MONTH, -1); Date result = cal.getTime(); 
like image 179
Aleksander Blomskøld Avatar answered Sep 21 '22 19:09

Aleksander Blomskøld