Joda-Time开源库

Joda-Time是一个面向 Java™ 平台的易于使用的开源时间日期库。

依赖

1 <dependency>
2     <groupId>joda-time</groupId>
3     <artifactId>joda-time</artifactId>
4     <version>2.9.9</version>
5 </dependency>

用法

1.时间日期类生成

 1 //方法一:取系统点间  
 2 DateTime dt1 = new DateTime();  
 3   
 4 //方法二:通过java.util.Date对象生成  
 5 DateTime dt2 = new DateTime(new Date());  
 6   
 7 //方法三:指定年月日点分秒生成(参数依次是:年,月,日,时,分,秒,毫秒)  
 8 DateTime dt3 = new DateTime(2012, 5, 20, 13, 14, 0, 0);  
 9   
10 //方法四:ISO8601形式生成  
11 DateTime dt4 = new DateTime("2012-05-20");  
12 DateTime dt5 = new DateTime("2012-05-20T13:14:00");  
13   
14 //只需要年月日的时候  
15 LocalDate localDate = new LocalDate(2009, 9, 6);// September 6, 2009  
16   
17 //只需要时分秒毫秒的时候  
18 LocalTime localTime = new LocalTime(13, 30, 26, 0);// 1:30:26PM  

2.获取年月日点分秒

 1 DateTime dt = new DateTime();  
 2 //
 3 int year = dt.getYear();  
 4 //
 5 int month = dt.getMonthOfYear();  
 6 //
 7 int day = dt.getDayOfMonth();  
 8 //星期  
 9 int week = dt.getDayOfWeek();  
10 //
11 int hour = dt.getHourOfDay();  
12 //
13 int min = dt.getMinuteOfHour();  
14 //
15 int sec = dt.getSecondOfMinute();  
16 //毫秒  
17 int msec = dt.getMillisOfSecond(); 

3.星期的处理

 1 DateTime dt = new DateTime();  
 2   
 3 //星期  
 4 switch(dt.getDayOfWeek()) {  
 5 case DateTimeConstants.SUNDAY:  
 6     System.out.println("星期日");  
 7     break;  
 8 case DateTimeConstants.MONDAY:  
 9     System.out.println("星期一");  
10     break;  
11 case DateTimeConstants.TUESDAY:  
12     System.out.println("星期二");  
13     break;  
14 case DateTimeConstants.WEDNESDAY:  
15     System.out.println("星期三");  
16     break;  
17 case DateTimeConstants.THURSDAY:  
18     System.out.println("星期四");  
19     break;  
20 case DateTimeConstants.FRIDAY:  
21     System.out.println("星期五");  
22     break;  
23 case DateTimeConstants.SATURDAY:  
24     System.out.println("星期六");  
25     break;  
26 }  

4.与JDK对象转换

 1 DateTime dt = new DateTime();  
 2   
 3 //转换成java.util.Date对象  
 4 Date d1 = new Date(dt.getMillis());  
 5 Date d2 = dt.toDate();  
 6   
 7 //转换成java.util.Calendar对象  
 8 Calendar c1 = Calendar.getInstance();  
 9 c1.setTimeInMillis(dt.getMillis());  
10 Calendar c2 = dt.toCalendar(Locale.getDefault()); 

5.日期前后推算

 1 DateTime dt = new DateTime();  
 2   
 3 //昨天  
 4 DateTime yesterday = dt.minusDays(1);         
 5 //明天  
 6 DateTime tomorrow = dt.plusDays(1);       
 7 //1个月前  
 8 DateTime before1month = dt.minusMonths(1);        
 9 //3个月后  
10 DateTime after3month = dt.plusMonths(3);          
11 //2年前  
12 DateTime before2year = dt.minusYears(2);          
13 //5年后  
14 DateTime after5year = dt.plusYears(5);  

6.时间比较

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); 

7.时间差

 1 DateTime begin = new DateTime("2012-02-01");  
 2 DateTime end = new DateTime("2012-05-01");  
 3   
 4 //计算区间毫秒数  
 5 Duration d = new Duration(begin, end);  
 6 long time = d.getMillis();  
 7   
 8 //计算区间天数  
 9 Period p = new Period(begin, end, PeriodType.days());  
10 int days = p.getDays();  
11   
12 //计算特定日期是否在该区间内  
13 Interval i = new Interval(begin, end);  
14 boolean contained = i.contains(new DateTime("2012-03-01"));  

8.格式化输出

1 DateTime dateTime = new DateTime();  
2   
3 String s1 = dateTime.toString("yyyy/MM/dd hh:mm:ss.SSSa");  
4 String s2 = dateTime.toString("yyyy-MM-dd HH:mm:ss");  
5 String s3 = dateTime.toString("EEEE dd MMMM, yyyy HH:mm:ssa");  
6 String s4 = dateTime.toString("yyyy/MM/dd HH:mm ZZZZ");  
7 String s5 = dateTime.toString("yyyy/MM/dd HH:mm Z"); 

 

Joda-Time的Util模板

 1 public class DateTimeUtil {
 2     public static final String STANDARD_FORMAT = "yyyy-MM-dd HH:mm:ss";
 3 
 4     public static Date strToDate(String dateTimeStr){
 5         DateTimeFormatter dateTimeFormatter = DateTimeFormat.forPattern(STANDARD_FORMAT);
 6         DateTime dateTime = dateTimeFormatter.parseDateTime(dateTimeStr);
 7         return dateTime.toDate();
 8     }
 9     
10     public static String dateToStr(Date date){
11         if (date == null){
12             return StringUtils.EMPTY;
13         }
14         DateTime dateTime = new DateTime(date);
15         return dateTime.toString(STANDARD_FORMAT);
16     }
17 }

 

参考:https://www.ibm.com/developerworks/cn/java/j-jodatime.html

posted @ 2018-10-28 19:31  platycoden  阅读(240)  评论(0编辑  收藏  举报