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

前端 未结 7 1991
天命终不由人
天命终不由人 2020-12-24 04:32

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         


        
7条回答
  •  青春惊慌失措
    2020-12-24 05:22

    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);
    

提交回复
热议问题