How can I change month only in java date object?
I have a java date object.
How can I change my month without changing the year.
eg.
12/1/14
I want to change it to
12/3/14 to 12/10/14
Basically I want to change the month to +/- x month without changing the year.
Can this be done?
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
You can use Calendar setting method to change the specific month you want
Example:
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
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
Other answers use nasty old time classes, which are now being replaced by java.time classes.
About java.time
The java.time framework is built into Java 8 and later. These classes supplant the nasty old legacy time classes such as java.util.Date
, Calendar
and SimpleDateFormat
.
The Joda-Time project , now in maintenance mode , is advised to move to the java.time classes .
To learn more, see the Oracle Tutorial . And search Stack Overflow for many examples and explanations. Specification JSR 310 .
Where can I get the java.time classes?
- Java SE 8 and SE 9 and later
- Built in.
- Part of the standard Java API with a combined implementation.
- Java 9 adds some minor features and fixes.
- Java SE 6 and SE 7
- Most of the functionality of java.time is carried back to Java 6 and 7 in ThreeTen-Backport .
- Android
- The ThreeTenABP project adapts the ThreeTen-Backport (mentioned above) specifically for Android.
- See. How to use ... ThreeTenABP .
The ThreeTen-Extra project extends java.time with additional classes. This project is proof of possible future additions to java.time. Here you can find useful classes, such as Interval
, YearWeek
, YearQuarter
and longer .
Joda time
Update. The Joda-Time project is now in maintenance mode , with the team advising moving to the java.time classes .
The Joda-Time library has LocalDate
to represent dates only without time and time.
You can arbitrarily set the value of the month. But beware that not all months have the same number of days, so the 29, 30, and 31 money issue is a problem.
LocalDate localDate = new LocalDate( 2014 , 12 , 31 ); // December.
LocalDate localDate_Feb = localDate.withMonthOfYear( 2 ); // February.
Dump for console.
System.out.println( "localDate: " + localDate );
System.out.println( "localDate_Feb: " + localDate_Feb );
At startup.
localDate: 2014-12-31 localDate_Feb: 2014-02-28
You can easily change the month with the set method.
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