1

Joda Time : 比JDK的Date,Calendar更好用,并且完美兼容的时间API.

点此访问Joda Time User Guide 用户向导地址

<!--当前最新版本maven地址-->
<dependency>
  <groupId>joda-time</groupId>
  <artifactId>joda-time</artifactId>
  <version>2.8.2</version>
</dependency>

基本对象

  • DateTime (日期+时间)

DateTime dateTime = new DateTime();
System.out.println(dateTime.toString("yyyy-MM-dd HH:mm:ss.SSS"));

输出结果: 2015-09-10 17: 46: 02.983


  • LocalDateTime(本地日期时间)

LocalDateTime localDateTime = new LocalDateTime();
System.out.println(localDateTime);

输出结果: 2015-09-10T18: 02: 42.325


  • LocalDate (本地日期)

LocalDate localDate = new LocalDate();
System.out.println(localDate);

输出结果: 2015-09-10


  • LocalTime(本地时间)

LocalTime localTime = new LocalTime();
System.out.println(localTime);

输出结果: 17: 46: 02.983


  • 与java.util.Date相互转换

java.util.Date date = new Date();

//java.util.Date -> Joda DateTime
DateTime dateTime = new DateTime(date);

//Joda DateTime -> java.util.Date
Date date2 = dateTime.toDate();

System.out.println(date2);

输出结果: Thu Sep 10 18: 08: 10 CST 2015


  • 与java.util.Calendar相互转换

java.util.Calendar calendar = Calendar.getInstance();

//calendar -> dateTime
DateTime dateTime = new DateTime(calendar);

// dateTime -> calendar
Calendar calendar2 = dateTime.toCalendar(Locale.CHINESE);
System.out.println(calendar2);

我有切糕
863 声望45 粉丝

Java架构师,技术经理