Joda-Time java时间工具

发布时间 2023-08-28 15:34:08作者: Smile☆

引入依赖

<!--日期时间工具-->
      <dependency>
        <groupId>joda-time</groupId>
        <artifactId>joda-time</artifactId>
        <version>2.10.1</version>
      </dependency>

使用方式:

  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(); 
  //
  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(); 
 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; 
 } 
取特殊日期
 
DateTime dt = new DateTime();    

//月末日期   
DateTime lastday = dt.dayOfMonth().withMaximumValue();   
//90天后那周的周一 
DateTimefirstday=dt.plusDays(90).dayOfWeek().withMinimumValue(); 
//时区
 
  //默认设置为日本时间 
 DateTimeZone.setDefault(DateTimeZone.forID("Asia/Tokyo")); 
 DateTime dt1 = new DateTime(); 

 
//计算区间
 
  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")); 
 
 
//日期比较
 
  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); 
 
 
//格式化输出
 
  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"); 

原文出处:http://quanzhan.applemei.com/webStack/TWpjMU5BPT0=

时区
 
1.  //默认设置为日本时间 
2.  DateTimeZone.setDefault(DateTimeZone.forID("Asia/Tokyo")); 
3.  DateTime dt1 = new DateTime(); 
4.   
5.  //伦敦时间 
6.  DateTime dt2 = new DateTime(DateTimeZone.forID("Europe/London")); 
 
8、计算区间
 
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()); 
10int days = p.getDays(); 
11.  
12//计算特定日期是否在该区间内 
13. Interval i = new Interval(begin, end); 
14boolean contained = i.contains(new DateTime("2012-03-01")); 
 
 
9、日期比较
 
1.  DateTime d1 = new DateTime("2012-02-01"); 
2.  DateTime d2 = new DateTime("2012-05-01"); 
3.   
4.  //和系统时间比 
5.  boolean b1 = d1.isAfterNow(); 
6.  boolean b2 = d1.isBeforeNow(); 
7.  boolean b3 = d1.isEqualNow(); 
8.   
9.  //和其他日期比 
10boolean f1 = d1.isAfter(d2); 
11boolean f2 = d1.isBefore(d2); 
12boolean f3 = d1.isEqual(d2); 
 
 
10、格式化输出
 
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");