http://www.joda.org/joda-time/
任何企业应用程序都需要处理时间问题。应用程序需要知道当前的时间点和下一个时间点,有时它们还必须计算这两个时间点之间的路径。使用 JDK 完成这项任务将非常痛苦和繁琐。现在来看看 Joda Time,一个面向 Java™ 平台的易于使用的开源时间/日期库。正如您在本文中了解的那样,Joda-Time 轻松化解了处理日期和时间的痛苦和繁琐
pom依赖:
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.9.4</version>
</dependency>
后续补充
1 import org.joda.time.DateTime; 2 3 /** 4 */ 5 public class Example2 { 6 public static final String FORMAT_DATE_WEEK_MS="E MM/dd/yyyy HH:mm:ss.SSS"; 7 public static final String FORMAT_DATE_MS="yyyy/MM/dd HH:mm:ss.SSS"; 8 public static final String FORMAT_DATE_S="yyyy/MM/dd HH:mm:ss"; 9 //按照format格式打印时间 10 public static void displayDate(DateTime dateTime,String format){ 11 String timeStr=dateTime.toString(format); 12 System.out.println(timeStr); 13 } 14 public static void main(String[] args) { 15 //一、创建一个时间点 16 DateTime dateTime1=new DateTime(2016,8,4,19,19,40,12);//年,月,日,时,分,秒,毫秒 17 displayDate(dateTime1,FORMAT_DATE_MS); 18 displayDate(dateTime1,FORMAT_DATE_S); 19 displayDate(dateTime1,FORMAT_DATE_WEEK_MS); 20 //这种填写参数方法至少填写到小时,否则要填写long型的时间戳 21 dateTime1=new DateTime(2016,8,4,19,19); 22 displayDate(dateTime1,FORMAT_DATE_S); 23 } 24 }
import org.joda.time.DateTime; import org.joda.time.Days; import org.joda.time.LocalDate; import org.joda.time.Period; import java.util.Locale; /** */ public class Example1 { //DateTime 时间+日期 public static boolean isAfterPayDay(DateTime datetime) { if (datetime.getMonthOfYear() == 2) { // February is month 2!! return datetime.getDayOfMonth() > 26; } return datetime.getDayOfMonth() > 1; } public static void isAfterPayDayTest(){ DateTime dateTime=new DateTime(); System.out.println(isAfterPayDay(dateTime)); System.out.println(dateTime.toDate()); } //LocalDate 日期 没有时间 public static Days daysToNewYear(LocalDate fromDate) { LocalDate newYear = fromDate.plusYears(1).withDayOfYear(1); System.out.println(newYear.toDate()); newYear = fromDate.plusYears(1); System.out.println(newYear.toDate()); return Days.daysBetween(fromDate, newYear); } public static void daysToNewYearTest() { LocalDate date=new LocalDate(); System.out.println(daysToNewYear(date)); } //Period 进行时间加减 public static boolean isRentalOverdue(DateTime datetimeRented) { Period rentalPeriod = new Period().withDays(2).withHours(12); System.out.println(datetimeRented); System.out.println(datetimeRented.toDateTime()); System.out.println(datetimeRented.plus(rentalPeriod).toDateTime()); return datetimeRented.plus(rentalPeriod).isBeforeNow(); } public static void isRentalOverdueTest(){ DateTime dateTime=new DateTime(); System.out.println(isRentalOverdue(dateTime)); } public static String getBirthMonthText(LocalDate dateOfBirth) { System.out.println(dateOfBirth.monthOfYear().getAsString()); return dateOfBirth.monthOfYear().getAsText(Locale.ENGLISH); } public static void getBirthMonthTextTest(){ LocalDate date=new LocalDate(); System.out.println(getBirthMonthText(date)); } public static void main(String[] args) { System.out.println("test1 ******************"); isAfterPayDayTest(); System.out.println("test2 ******************"); daysToNewYearTest(); System.out.println("test3 ******************"); isRentalOverdueTest(); System.out.println("test4 ******************"); getBirthMonthTextTest(); } }