Joda-Time is a utility library for Date and Time functionality. The following example will show the usage of some function of Joda-Time. Add “joda-time-xxx.jar” into your classpath.
For more references Joda-Time.
DateUtils.java
package org.metadata.core.date; import java.text.SimpleDateFormat; import java.util.Date; import org.joda.time.DateTime; import org.joda.time.DateTimeZone; import org.joda.time.Days; import org.joda.time.Months; import org.joda.time.Years; public class DateUtils { /** * get number of days within given start date and end date */ public static int getPeriodOfDays(Date startDate, Date endDate) { DateTime jdStartDate = new DateTime(startDate); DateTime jdEndDate = new DateTime(endDate); int dyas = Days.daysBetween(jdStartDate, jdEndDate).getDays(); return dyas; } /** * get number of months within given start date and end date */ public static int getPeriodOfMonths(Date startDate, Date endDate) { DateTime jdStartDate = new DateTime(startDate); DateTime jdEndDate = new DateTime(endDate); int months = Months.monthsBetween(jdStartDate, jdEndDate).getMonths(); return months; } /** * get number of years within given start date and end date */ public static int getPeriodOfYears(Date startDate, Date endDate) { DateTime jdStartDate = new DateTime(startDate); DateTime jdEndDate = new DateTime(endDate); int years = Years.yearsBetween(jdStartDate, jdEndDate).getYears(); return years; } /** * Add the number of day into the given date. */ public static Date addDays(Date date, int days) { DateTime dateTime = new DateTime(date, DateTimeZone.getDefault()); return dateTime.plusDays(days).toDate(); } /** * Subtract the number of day into the given date. */ public static Date subtractDays(Date date, int days) { DateTime dateTime = new DateTime(date, DateTimeZone.getDefault()); return dateTime.minusDays(days).toDate(); } /** * Adding the number of month into the given date. */ public static Date addMonths(Date date, int month) { DateTime dateTime = new DateTime(date, DateTimeZone.getDefault()); return dateTime.plusMonths(month).toDate(); } /** * Subtract the number of month into the given date. */ public static Date subtractMonths(Date date, int month) { DateTime dateTime = new DateTime(date, DateTimeZone.getDefault()); return dateTime.minusMonths(month).toDate(); } /** * Adding the number of month into the given date. */ public static Date addYears(Date date, int years) { DateTime dateTime = new DateTime(date, DateTimeZone.getDefault()); return dateTime.plusYears(years).toDate(); } /** * Subtract the number of month into the given date. */ public static Date subtractYears(Date date, int years) { DateTime dateTime = new DateTime(date, DateTimeZone.getDefault()); return dateTime.minusYears(years).toDate(); } public static void main(String[] args) throws Exception { SimpleDateFormat format = new SimpleDateFormat("dd-MM-yyyy"); Date startDate = format.parse("02-12-2013"); Date endDate = format.parse("02-05-2014"); int pDays = DateUtils.getPeriodOfDays(startDate, endDate); System.out.println("Period of Days : " + pDays); int pMonths = DateUtils.getPeriodOfMonths(startDate, endDate); System.out.println("Period of Months : " + pMonths); int pYears = DateUtils.getPeriodOfMonths(startDate, endDate); System.out.println("Period of Years : " + pYears); Date addDate = DateUtils.addDays(startDate, 5); System.out.println("Add Date : " + format.format(addDate)); Date addMonth = DateUtils.addMonths(startDate, 4); System.out.println("Add Month : " + format.format(addMonth)); Date addYear = DateUtils.addYears(startDate, 2); System.out.println("Add Year : " + format.format(addYear)); Date subtractDate = DateUtils.subtractDays(startDate, 5); System.out.println("Subtract Date : " + format.format(subtractDate)); Date subtractMonth = DateUtils.subtractMonths(startDate, 4); System.out.println("Subtract Month : " + format.format(subtractMonth)); Date subtractYear = DateUtils.subtractYears(startDate, 2); System.out.println("Subtract Year : " + format.format(subtractYear)); } } |
Output
Period of Days : 151 Period of Months : 5 Period of Years : 5 Add Date : 07-12-2013 Add Month : 02-04-2014 Add Year : 02-12-2015 Subtract Date : 27-11-2013 Subtract Month : 02-08-2013 Subtract Year : 02-12-2011 |
No comments:
Post a Comment