Java examples on org.joda.time
- How do I get number of days between two dates in Joda?
- How do I use Joda-Time's DateMidnight class?
- How do I get number of months between two dates in Joda?
- How do I format date information in Joda?
- How do I get the last day of a month in Joda?
- How do I get date / time fields of date in Joda?
- How do I create DateTime object in Joda Time?
- How do I format date in Joda using ISODateTimeFormat class?
- How do I do a date calculations?
- How do I convert DateTime into Calendar or Date object?
- How do I use the Interval class of Joda Time?
- How do I get the first day of the next month in Joda?
- How to add hours, minutes, seconds into DateTime in Joda Time?
- How do I use Joda Time Instant Class?
How do I get the first day of the next month in Joda?
This example will give you the first day of the next month from the current system date. In the example we use the MutableDateTime class. We add 1 month from the current date using the addMonths() method. Next we set the day of the month to the first day using the setDayOfMonth() method. To make sure it is the first day we set the time to midnight by calling the setMillisOfDay() method.
package org.kodejava.example.joda;
import org.joda.time.MutableDateTime;
public class FirstDayOfNextMonth {
public static void main(String[] args) {
//
// Creates an instance of MutableDateTime for the current
// system date time.
//
MutableDateTime dateTime = new MutableDateTime();
System.out.println("dateTime = " + dateTime);
//
// Find the first day of the next month can be done by:
// 1. Add 1 month to the date
// 2. Set the day of the month to 1
// 3. Set the millis of day to 0.
//
dateTime.addMonths(1);
dateTime.setDayOfMonth(1);
dateTime.setMillisOfDay(0);
System.out.println("First day of next month = " + dateTime);
}
}
Here is an example result of the program:
dateTime = 2012-02-03T23:34:21.801+08:00 First day of next month = 2012-03-01T00:00:00.000+08:00