Joda-Time使用

今天在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

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

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

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

5.到新年还有多少天

public Days daysToNewYear(LocalDate fromDate) {
  LocalDate newYear = fromDate.plusYears(1).withDayOfYear(1);
  return Days.daysBetween(fromDate, newYear);
}
 
 
  • 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

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

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

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

下面通过一个简单的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

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值