Joda-Time用法

轉自:Ricky_Fung

今天在spring官網demo中無意看到了Joda-Time,於是去Joda-Time官網瞭解了一下,瞬間被它的強大功能和易用性所折服。

Joda-Time簡介

Joda-Time — 面向 Java 應用程序的日期/時間庫的替代選擇,Joda-Time 令時間和日期值變得易於管理、操作和理解。事實上,易於使用是 Joda 的主要設計目標。其他目標包括可擴展性、完整的特性集以及對多種日曆系統的支持。並且 Joda 與 JDK 是百分之百可互操作的,因此您無需替換所有 Java 代碼,只需要替換執行日期/時間計算的那部分代碼。

Joda-Time使用

1.創建一個用時間表示的某個隨意的時刻 — 比如,2015年12月21日0時0分

DateTime dt = new DateTime(2015, 12, 21, 0, 0, 0, 333);// 年,月,日,時,分,秒,毫秒 
  • 1
  • 1

2.格式化時間輸出

DateTime dateTime = new DateTime(2015, 12, 21, 0, 0, 0, 333);
System.out.println(dateTime.toString("yyyy/MM/dd HH:mm:ss EE"));
  • 1
  • 2
  • 1
  • 2

3.解析文本格式時間

DateTimeFormatter format = DateTimeFormat .forPattern("yyyy-MM-dd HH:mm:ss");  
DateTime dateTime = DateTime.parse("2015-12-21 23:22:45", format); 
System.out.println(dateTime.toString("yyyy/MM/dd HH:mm:ss EE"));
  • 1
  • 2
  • 3
  • 1
  • 2
  • 3

4.在某個日期上加上90天並輸出結果

DateTime dateTime = new DateTime(2016, 1, 1, 0, 0, 0, 0);
System.out.println(dateTime.plusDays(90).toString("E MM/dd/yyyy HH:mm:ss.SSS");
  • 1
  • 2
  • 1
  • 2

5.到新年還有多少天

public Days daysToNewYear(LocalDate fromDate) {
  LocalDate newYear = fromDate.plusYears(1).withDayOfYear(1);
  return Days.daysBetween(fromDate, newYear);
}
  • 1
  • 2
  • 3
  • 4
  • 1
  • 2
  • 3
  • 4

6.與JDK日期對象的轉換

DateTime dt = new DateTime();  

//轉換成java.util.Date對象  
Date d1 = new Date(dt.getMillis());  
Date d2 = dt.toDate(); 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 1
  • 2
  • 3
  • 4
  • 5

7.時區

//默認設置爲日本時間  
DateTimeZone.setDefault(DateTimeZone.forID("Asia/Tokyo"));  
DateTime dt1 = new DateTime();  
System.out.println(dt1.toString("yyyy-MM-dd HH:mm:ss"));

//倫敦時間  
DateTime dt2 = new DateTime(DateTimeZone.forID("Europe/London"));
System.out.println(dt2.toString("yyyy-MM-dd HH:mm:ss"));
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

8.計算間隔和區間

DateTime begin = new DateTime("2015-02-01");  
DateTime end = new DateTime("2016-05-01");  

//計算區間毫秒數  
Duration d = new Duration(begin, end);  
long millis = d.getMillis();  

//計算區間天數  
Period p = new Period(begin, end, PeriodType.days());  
int days = p.getDays();  

//計算特定日期是否在該區間內  
Interval interval = new Interval(begin, end);  
boolean contained = interval.contains(new DateTime("2015-03-01")); 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

9.日期比較

DateTime d1 = new DateTime("2015-10-01"); 
DateTime d2 = new DateTime("2016-02-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);  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

下面通過一個簡單的demo來實戰一下,輸入一個日期(生日,格式:yyyy-MM-dd Or yyyy-MM-dd HH:mm:ss),計算出今天是你人生的第多少天/小時/分/秒,代碼如下:

package com.ricky.spring.springdemo;

import org.joda.time.DateTime;
import org.joda.time.Days;
import org.joda.time.Hours;
import org.joda.time.Minutes;
import org.joda.time.Seconds;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import java.util.Scanner;

public class DayOfLife {

    public static void main(String[] args) {

        new DayOfLife().calDayNumber();
    }

    public void calDayNumber(){

        Scanner scanner = new Scanner(System.in);
        try{
            while(true){
                System.out.println("*********請輸入生日(yyyy-MM-dd Or yyyy-MM-dd HH:mm:ss)*********");
                String line = scanner.nextLine();
                if("exit".equalsIgnoreCase(line)){
                    break;
                }
                DateTimeFormatter format = null;
                if(line.length()==10){
                    format = DateTimeFormat .forPattern("yyyy-MM-dd");  
                }else{
                    format = DateTimeFormat .forPattern("yyyy-MM-dd HH:mm:ss");
                }
                DateTime startDateTime = null;;
                try {
                    startDateTime = DateTime.parse(line, format);
                } catch (Exception e) {
                    System.err.println("輸入格式錯誤,請重新輸入生日!");
                    continue;
                }
                calDays(startDateTime, new DateTime());
            }
        }finally{
            scanner.close();
        }

    }

    private void calDays(DateTime startDateTime,DateTime endDateTime){

        Days days = Days.daysBetween(startDateTime, endDateTime);
        System.out.println("今天是你人生的第"+days.getDays()+"天");

        Hours hours = Hours.hoursBetween(startDateTime, endDateTime);
        System.out.println("今天是你人生的第"+hours.getHours()+"小時");

        Minutes minutes = Minutes.minutesBetween(startDateTime, endDateTime);
        System.out.println("今天是你人生的第"+minutes.getMinutes()+"分鐘");

        Seconds seconds = Seconds.secondsBetween(startDateTime, endDateTime);
        System.out.println("今天是你人生的第"+seconds.getSeconds()+"秒");
    }

}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67

參考鏈接: 
http://www.joda.org/joda-time/quickstart.html 
http://www.ibm.com/developerworks/cn/java/j-jodatime.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章