Joda-Time提供了一組Java類包用於處理包括ISO8601標準在內的date和time。能夠利用它把JDK Date和Calendar類徹底替換掉,並且仍然可以提供很好的集成。
http://joda-time.sourceforge.net/
版本:joda-time-2.1.jar
一、時間類得做成html
//方法一:取系統點間 DateTime dt1 = new DateTime(); //方法二:經過java.util.Date對象生成 DateTime dt2 = new DateTime(new Date()); //方法三:指定年月日點分秒生成(參數依次是:年,月,日,時,分,秒,毫秒) DateTime dt3 = new DateTime(2012, 5, 20, 13, 14, 0, 0); //方法四:ISO8601形式生成 DateTime dt4 = new DateTime("2012-05-20"); DateTime dt5 = new DateTime("2012-05-20T13:14:00"); //只須要年月日的時候 LocalDate localDate = new LocalDate(2009, 9, 6);// September 6, 2009 //只須要時分秒毫秒的時候 LocalTime localTime = new LocalTime(13, 30, 26, 0);// 1:30:26PM
二、獲取年月日點分秒java
DateTime dt = new DateTime(); //年 int year = dt.getYear(); //月 int month = dt.getMonthOfYear(); //日 int day = dt.getDayOfMonth(); //星期 int week = dt.getDayOfWeek(); //點 int hour = dt.getHourOfDay(); //分 int min = dt.getMinuteOfHour(); //秒 int sec = dt.getSecondOfMinute(); //毫秒 int msec = dt.getMillisOfSecond();
三、星期的特殊處理.net
DateTime dt = new DateTime(); //星期 switch(dt.getDayOfWeek()) { case DateTimeConstants.SUNDAY: System.out.println("星期日"); break; case DateTimeConstants.MONDAY: System.out.println("星期一"); break; case DateTimeConstants.TUESDAY: System.out.println("星期二"); break; case DateTimeConstants.WEDNESDAY: System.out.println("星期三"); break; case DateTimeConstants.THURSDAY: System.out.println("星期四"); break; case DateTimeConstants.FRIDAY: System.out.println("星期五"); break; case DateTimeConstants.SATURDAY: System.out.println("星期六"); break; }
四、與JDK日期對象的轉換code
DateTime dt = new DateTime(); //轉換成java.util.Date對象 Date d1 = new Date(dt.getMillis()); Date d2 = dt.toDate(); //轉換成java.util.Calendar對象 Calendar c1 = Calendar.getInstance(); c1.setTimeInMillis(dt.getMillis()); Calendar c2 = dt.toCalendar(Locale.getDefault());
五、日期先後推算htm
DateTime dt = new DateTime(); //昨天 DateTime yesterday = dt.minusDays(1); //明天 DateTime tomorrow = dt.plusDays(1); //1個月前 DateTime before1month = dt.minusMonths(1); //3個月後 DateTime after3month = dt.plusMonths(3); //2年前 DateTime before2year = dt.minusYears(2); //5年後 DateTime after5year = dt.plusYears(5);
六、取特殊日期對象
DateTime dt = new DateTime(); //月末日期 DateTime lastday = dt.dayOfMonth().withMaximumValue(); //90天后那周的週一 DateTime firstday = dt.plusDays(90).dayOfWeek().withMinimumValue();
七、時區get
//默認設置爲日本時間 DateTimeZone.setDefault(DateTimeZone.forID("Asia/Tokyo")); DateTime dt1 = new DateTime(); //倫敦時間 DateTime dt2 = new DateTime(DateTimeZone.forID("Europe/London"));
八、計算區間it
DateTime begin = new DateTime("2012-02-01"); DateTime end = new DateTime("2012-05-01"); //計算區間毫秒數 Duration d = new Duration(begin, end); long time = d.getMillis(); //計算區間天數 Period p = new Period(begin, end, PeriodType.days()); int days = p.getDays(); //計算特定日期是否在該區間內 Interval i = new Interval(begin, end); boolean contained = i.contains(new DateTime("2012-03-01"));
九、日期比較io
DateTime d1 = new DateTime("2012-02-01"); DateTime d2 = new DateTime("2012-05-01"); //和系統時間比 boolean b1 = d1.isAfterNow(); boolean b2 = d1.isBeforeNow(); boolean b3 = d1.isEqualNow(); //和其餘日期比 boolean f1 = d1.isAfter(d2); boolean f2 = d1.isBefore(d2); boolean f3 = d1.isEqual(d2);
十、格式化輸出ast
DateTime dateTime = new DateTime(); String s1 = dateTime.toString("yyyy/MM/dd hh:mm:ss.SSSa"); String s2 = dateTime.toString("yyyy-MM-dd HH:mm:ss"); String s3 = dateTime.toString("EEEE dd MMMM, yyyy HH:mm:ssa"); String s4 = dateTime.toString("yyyy/MM/dd HH:mm ZZZZ"); String s5 = dateTime.toString("yyyy/MM/dd HH:mm Z");
Jodd的JDateTime也提供了很不錯的時間API。
參考資料:http://www.ibm.com/developerworks/cn/java/j-jodatime.html