替代Java類的日期和時間【 Joda Time】

其實在Joda Time學習的首頁,就可以看到介紹,作爲一個相對jdk 的date和calendar的更好的解決方案。joda-time在很多方面都支持的更多,例如:更加完整的時區支持,更易用的api接口。體現在日期的計算,比較和格式轉換。

本文主要分享一些在開發過程中常用的joda-time的API以及幾個示例。一些在筆者在處理oracle的timestamp的方案。

獲取不同格式的時間

`System.out.println("Date格式:現在的時間是:"+new Date());
    System.out.println("joda-time:現在的時間是:"+new DateTime());

    System.out.println("joda-time:只獲取年月日時間:"+(new DateTime()).toString("yyyy-MM-dd"));
    System.out.println("joda-time:今天是"+(new DateTime()).toString("E"));
    System.out.println("joda-time:獲取時間到秒:"+(new DateTime()).toString("yyyy-MM-dd HH:mm:ss"));
    System.out.println("joda-time:獲取時間到毫秒:"+(new DateTime())
            .toString(" yyyy/MM/dd/ E HH:mm:ss.SSS"));
    System.out.println("joda-time:獲取時間帶下午:"+(new DateTime()).toString("MM/dd/yyyy hh:mm:ss.SSSa"));
    ;`

獲取不同時區的時間

`System.out.println("joda-time:獲取所在時區名稱時間:"+(new DateTime()).toString("MM/dd/yyyy HH:mm ZZZZ"));
    System.out.println("joda-time:獲取所在時區時間(依據格林威治時間標準):"+(new DateTime()).toString("MM/dd/yyyy HH:mm Z")+"(時區號)")
    //洛杉磯時間
    DateTimeZone.setDefault(DateTimeZone.forID("America/Los_Angeles"));     
    return  new DateTime(DateTimeZone.forID(pattern));`

與calendar的轉換

joda-time 仍然支持從calendar的方式轉換爲localtime。localtime提供默認的構造方法來轉化。
Calendar calendar=Calendar.getInstance();
LocalTime localTime=new LocalTime(calendar);
System.out.println("由calendar轉化爲localtime的時間:"+localTime.toString());

計算時間

計算二者之間存在毫秒
`Duration duration=new Duration(dateOne,dateTwo);
    return duration.getMillis();
//某天是否存在於區間內
    Interval i = new Interval(new DateTime("2012-02-01"), new DateTime("2012-04-01"));  
    System.out.println( i.contains(new DateTime("2012-03-01")));`
    //得到明天
    DateTime dt=new DateTime();
    System.out.println(dt.plusDays(1));

其他

獲取今天還剩多長時間
`DateTime nowTime = new DateTime();
    DateTime endOfDay = nowTime.millisOfDay().withMaximumValue();

    return endOfDay.getMillis() - nowTime.getMillis();`
獲取一天開始的時間
`DateTime nowTime = new DateTime();
    DateTime startOfDay = nowTime.withTimeAtStartOfDay();`
獲取一天結束的時間
`DateTime nowTime = new DateTime();
    DateTime endOfDay = nowTime.millisOfDay().withMaximumValue();`

將oracle時間戳轉化爲long類型

`public static void timeStmap2Long(String strDate) throws ParseException {
    String tranDate = strDate.replace(".", ":").toString().replace(" ", "");
    System.out.println("字符串長度爲:" + tranDate.trim().length());
    // target 日期pattern
    // 1.獲取月份
    String month = tranDate.substring(3, 4);
    if (tranDate.trim().length() == 26) {
        // 1.獲取月份
        month = tranDate.substring(3, 5);
    }

    System.out.println("月份爲:" + month);
    // 2.獲取天數
    String day = tranDate.substring(0, 2);

    // 返回第二次出現"-"的索引
    int twoindex = tranDate.lastIndexOf("-");
    // 3.獲取年份
    String year = "20" + tranDate.substring(twoindex + 1, twoindex + 3);
    // 4.獲取時間
    // 取得上下午來判斷小時
    String time = tranDate.substring(twoindex + 3, twoindex + 12);

    // 取得上下午
    String h = tranDate.substring(twoindex + 18, twoindex + 20);
    String rightTime = time;
    int intHHtime = 0;
    if ("下午".equals(h)) {
        String HHtime = tranDate.substring(twoindex + 4, twoindex + 5);
        System.out.println(HHtime + "小時");
        intHHtime = Integer.parseInt(HHtime) + 12;
        rightTime = intHHtime + ":"
                + tranDate.substring(twoindex + 6, twoindex + 11);
        System.out.println(rightTime);
    }

    // 5.獲取毫秒
    String msec = tranDate.substring(twoindex + 12, twoindex + 18);
    int msecInt = Integer.parseInt(msec);

    String targetDate = month + "/" + day + "/" + year + " " + rightTime;
    SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
    Date dt = sdf.parse(targetDate);
    long tarLong = dt.getTime() / 1000 + msecInt;
    System.out.println(tarLong);

}`

long類型轉換爲datetime

這個直接由joda-time實現了一個構造函數來支持,簡潔易用。
`  return new DateTime(datetime);`

諸如此類主要是DateTime、和localtime這兩個類在起作用。

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