如何仅在 java date 对象中更改月份?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25347073/
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-11-02 07:51:54  来源:igfitidea点击:

how can I change month only in java date object?

javadatetimecalendar

提问by Elad Benda2

I have java date object.

我有 java 日期对象。

How can I change its month without changing the year.

如何在不更改年份的情况下更改其月份。

e.g.

例如

12/1/14

14 年 1 月 12 日

I want to change it to

我想把它改成

12/3/14 and to 12/10/14

12/3/14 至 12/10/14

I basically want to change the month by +/- x month without changing the year.

我基本上想在不更改年份的情况下将月份更改为 +/- x 个月。

Can it be done?

可以做到吗?

回答by TJ-

Calendar cal = Calendar.getInstance();
cal.setTime(date); // your date (java.util.Date)
cal.add(Calendar.MONTH, x); // You can -/+ x months here to go back in history or move forward.
return cal.getTime(); // New date

ref : Java Calendar

参考:Java 日历

回答by Rod_Algonquin

You can use the set methodof the Calendarclass to change the specific month you want

您可以使用set方法的的日历类改变你想要的特定月份

sample:

样本:

Calendar c = Calendar.getInstance();
System.out.println(c.getTime());
c.set(Calendar.MONTH, Calendar.JANUARY); //will change the month to JANUARY
System.out.println(c.getTime());

result:

结果:

Sun Aug 17 03:17:35 EDT 2014
Fri Jan 17 03:17:35 EST 2014

回答by Basil Bourque

java.time

时间

LocalDate localDate = LocalDate.of ( 2017, Month.JANUARY, 23 );
LocalDate localDateFeb = localDate.withMonth ( 2 );

…or…

…或者…

LocalDate localDateFeb = localDate.withMonth ( Month.FEBRUARY.getValue ( ) );

localDate.toString(): 2017-01-23

localDateFeb.toString(): 2017-02-23

localDate.toString(): 2017-01-23

localDateFeb.toString(): 2017-02-23

The other Answers use the troublesome old date-time classes, now supplanted by the java.time classes.

其他答案使用麻烦的旧日期时间类,现在由 java.time 类取代。



About java.time

关于 java.time

The java.timeframework is built into Java 8 and later. These classes supplant the troublesome old legacydate-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

java.time框架是建立在Java 8和更高版本。这些类取代了麻烦的旧的遗留日期时间类,例如java.util.Date, Calendar, & SimpleDateFormat

The Joda-Timeproject, now in maintenance mode, advises migration to the java.timeclasses.

现在处于维护模式Joda-Time项目建议迁移到java.time类。

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

要了解更多信息,请参阅Oracle 教程。并在 Stack Overflow 上搜索许多示例和解释。规范是JSR 310

Where to obtain the java.time classes?

从哪里获得 java.time 类?

The ThreeTen-Extraproject extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.

ThreeTen-额外项目与其他类扩展java.time。该项目是未来可能添加到 java.time 的试验场。你可能在这里找到一些有用的类,比如IntervalYearWeekYearQuarter,和更多



Joda-Time

乔达时间

Update: The Joda-Timeproject is now in maintenance mode, with the team advising migration to the java.timeclasses.

更新:Joda-Time项目现在处于维护模式,团队建议迁移到java.time类。

The Joda-Timelibrary has a LocalDateclass to represent a date-only without a time-of-day nor time zone.

乔达时库有一个LocalDate类没有时间的天,也不时区来表示,仅日期。

You can arbitrarily set the month value. But beware of the issue that not all months have the same number of days, so having day-of-month values of 29, 30, and 31 are a problem.

您可以任意设置月份值。但请注意并非所有月份的天数都相同的问题,因此月份的天数为 29、30 和 31 是一个问题。

LocalDate localDate = new LocalDate( 2014 , 12 , 31 ); // December.
LocalDate localDate_Feb = localDate.withMonthOfYear( 2 ); // February.

Dump to console.

转储到控制台。

System.out.println( "localDate: " + localDate );
System.out.println( "localDate_Feb: " + localDate_Feb ); 

When run.

跑的时候。

localDate: 2014-12-31
localDate_Feb: 2014-02-28

回答by user3689624

You can easily change the month using the set method.

您可以使用 set 方法轻松更改月份。

Calendar cal = Calendar.getInstance();    
System.out.println(cal.getTime()); //prints the current date    
cal.set(Calendar.MONTH, Calendar.July); //changes the month to july    
System.out.println(cal.getTime()); // prints the edited date