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

JavaDateDatetimeJava Time

Java Problem Overview


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?

Java Solutions


Solution 1 - Java

Use Calendar:

Calendar cal = Calendar.getInstance();
cal.add(Calendar.MONTH, -1);
Date result = cal.getTime();

Solution 2 - Java

Starting from Java 8, the suggested way is to use the Date-Time API rather than Calendar.

If you want a Date object to be returned:

Date date = Date.from(ZonedDateTime.now().minusMonths(1).toInstant());

If you don't need exactly a Date object, you can use the classes directly, provided by the package, even to get dates in other time-zones:

ZonedDateTime dateInUTC = ZonedDateTime.now(ZoneId.of("Pacific/Auckland")).minusMonths(1);

Solution 3 - Java

Calendar calNow = Calendar.getInstance()

// adding -1 month
calNow.add(Calendar.MONTH, -1);

// fetching updated time
Date dateBeforeAMonth = calNow.getTime();

Solution 4 - Java

you can use Calendar

    java.util.Date da = new Date();
    Calendar cal = Calendar.getInstance();
    cal.setTime(da);
    cal.add(Calendar.MONTH, -1);
    da = cal.getTime();

Solution 5 - Java

Using new java.time package in Java8 and Java9

import java.time.LocalDate;

LocalDate mydate = LocalDate.now(); // Or whatever you want
mydate = mydate.minusMonths(1);

The advantage to using this method is that you avoid all the issues about varying month lengths and have more flexibility in adjusting dates and ranges. The Local part also is Timezone smart so it's easy to convert between them.

As an aside, using java.time you can also get the day of the week, day of the month, all days up to the last of the month, all days up to a certain day of the week, etc.

mydate.plusMonths(1);
mydate.with(TemporalAdjusters.next(DayOfWeek.SUNDAY)).getDayOfMonth();
mydate.with(TemporalAdjusters.lastDayOfMonth());

Solution 6 - Java

Using JodaTime :

Date date = new DateTime().minusMonths(1).toDate();

JodaTime provides a convenient API for date manipulation.

Note that similar Date API will be introduced in JDK8 with the JSR310.

Solution 7 - Java

raduce 1 month of JDF

Date dateTo = new SimpleDateFormat("yyyy/MM/dd").parse(jdfMeTo.getJulianDate());
        Calendar cal = Calendar.getInstance();
        cal.setTime(dateTo);
        cal.add(Calendar.MONTH, -1);
        Date dateOf = cal.getTime();
        Log.i("dateOf", dateOf.getTime() + "");
        jdfMeOf.setJulianDate(cal.get(Calendar.DAY_OF_YEAR), cal.get(Calendar.DAY_OF_MONTH), 
        cal.get(Calendar.DAY_OF_WEEK_IN_MONTH));

Solution 8 - Java

You can also use the DateUtils from apache common. The library also supports adding Hour, Minute, etc.

Date date = DateUtils.addMonths(new Date(), -1)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionkarthiView Question on Stackoverflow
Solution 1 - JavaAleksander BlomskøldView Answer on Stackoverflow
Solution 2 - JavaDVargaView Answer on Stackoverflow
Solution 3 - JavajmjView Answer on Stackoverflow
Solution 4 - JavaAhielView Answer on Stackoverflow
Solution 5 - JavaGregory Alan BolcerView Answer on Stackoverflow
Solution 6 - JavaFredszaqView Answer on Stackoverflow
Solution 7 - JavasaeedmptView Answer on Stackoverflow
Solution 8 - JavaSabuj HassanView Answer on Stackoverflow