如何使用java从当前日期减少一个月并存储在日期变量中?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16392892/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 06:39:01  来源:igfitidea点击:

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

javadatedatetimejava-time

提问by karthi

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

如何从当前日期减少一个月并希望java.util.Date使用此代码在变量 im 中疼痛,但它在第二行显示错误

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

How to store this date in java.util.Datevariable?

如何将此日期存储在java.util.Date变量中?

采纳答案by Aleksander Blomsk?ld

Use Calendar:

使用日历:

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

回答by Jigar Joshi

Calendar calNow = Calendar.getInstance()

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

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

回答by Ahiel

you can use Calendar

您可以使用 Calendar

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

回答by Fredszaq

Using JodaTime:

使用JodaTime

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

JodaTime provides a convenient API for date manipulation.

JodaTime 为日期操作提供了方便的 API。

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

请注意,类似的 Date API 将在 JDK8 中与 JSR310 一起引入

回答by DVarga

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

从 Java 8 开始,建议的方法是使用Date-Time API而不是Calendar.

If you want a Dateobject to be returned:

如果你想Date返回一个对象:

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

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

如果您不需要确切的Date对象,则可以直接使用包提供的类,甚至可以获取其他时区的日期:

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

回答by Gregory Alan Bolcer

Using new java.time package in Java8 and Java9

在 Java8 和 Java9 中使用新的 java.time 包

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.

顺便说一句,使用 java.time 您还可以获得一周中的哪一天、一个月中的哪一天、直到本月最后一天的所有天数、直到一周中某一天的所有天数,等等。

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

回答by saeedmpt

raduce 1 month of JDF

提供 1 个月的 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));