DateTime result = dt.plus(0, 1, 0, 0, 0, 0, DayOverflow.Spillover);
log("date difference : " + result.toString());
// result date difference : 2011-05-01 00:00:00
}
/** What day is 3 months and 5 days from today */
private void whenIs3Months5DaysFromToday() {
DateTime today = DateTime.today(TimeZone.getDefault());
DateTime result = today.plus(0, 3, 5, 0, 0, 0,
DateTime.DayOverflow.FirstDay);
log("3 months and 5 days from today is : "
+ result.format("YYYY-MM-DD"));
// result 3 months and 5 days from today is : 2012-07-17
}
/**
* Current number of hours difference between Paris, France and Perth,
* Australia.
*/
private void hoursDifferenceBetweenParisAndPerth() {
// this assumes the time diff is a whole number of hours; other styles
// are possible
DateTime paris = DateTime.now(TimeZone.getTimeZone("Europe/Paris"));
DateTime perth = DateTime.now(TimeZone.getTimeZone("Australia/Perth"));
int result = perth.getHour() - paris.getHour();
if (result < 0) {
result = result + 24;
}
log("Numbers of hours difference between Paris and Perth : " + result);
// result Numbers of hours difference between Paris and Perth : 6
}
/** How many weeks is it since Sep 6, 2010 */
private void weeksSinceStart() {
DateTime today = DateTime.today(TimeZone.getDefault());
DateTime startOfProject = DateTime.forDateOnly(2010, 9, 6);
int result = today.getWeekIndex() - startOfProject.getWeekIndex();
log("The number of weeks since Sep 6, 2010 : " + result);
// result The number of weeks since Sep 6, 2010 : 83
}
/** How much time till midnight */
private void timeTillMidnight() {
DateTime now = DateTime.now(TimeZone.getDefault());
DateTime midnight = now.plusDays(1).getStartOfDay();
long result = now.numSecondsFrom(midnight);
log("This many seconds till midnight : " + result);
// result This many seconds till midnight : 83046
}
/** Format using ISO style. */
private void imitateISOFormat() {
DateTime now = DateTime.now(TimeZone.getDefault());
log("Output using an ISO format: " + now.format("YYYY-MM-DDThh:mm:ss"));
// result Output using an ISO format: 2012-04-12T00:55:54
}
private void firstDayOfThisWeek() {
DateTime today = DateTime.today(TimeZone.getDefault());
DateTime firstDayThisWeek = today; // start value
int todaysWeekday = today.getWeekDay();
int SUNDAY = 1;
if (todaysWeekday > SUNDAY) {
int numDaysFromSunday = todaysWeekday - SUNDAY;
firstDayThisWeek = today.minusDays(numDaysFromSunday);
}
log("The first day of this week is : " + firstDayThisWeek);
// result The first day of this week is : 2012-04-08
}
/** For how many years has the JDK date-time API been suctorial */
private void jdkDatesSuctorial() {
DateTime today = DateTime.today(TimeZone.getDefault());
DateTime jdkFirstPublished = DateTime.forDateOnly(1996, 1, 23);
int result = today.getYear() - jdkFirstPublished.getYear();
log("The number of years the JDK date-time API has been suctorial : "
+ result);
// result The number of years the JDK da