1.maven引入
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.9.9</version>
</dependency>
2.小例子
public class JodaTimeDemo {
public static void main(String[] args) {
DateTime dateTime1 = new DateTime(1523428434000L);
System.out.println(dateTime1.toString("yyyy-MM-dd hh:mm:ss"));
System.out.println(dateTime1.plusDays(1).toString("yyyy-MM-dd hh:mm:ss"));
DateTime dateTime2 = new DateTime(
2000, //year
1, // month
1, // day
13, // hour (midnight is zero)
0, // minute
0, // second
0 // milliseconds
);
System.out.println(dateTime2.toString("yyyy-MM-dd hh:mm:ss"));
LocalDate localDate = new LocalDate(2009, 9, 6);
System.out.println(localDate.toString("yyyy-MM-dd"));
LocalTime localTime = new LocalTime(13, 30, 26, 0);
System.out.println(localTime.toString("hh:mm:ss"));
DateTime dateTime3 = new DateTime(
2000, //year
1, // month
1, // day
1, // hour (midnight is zero)
0, // minute
0, // second
0 // milliseconds
);
DateTime dateTime4 = new DateTime(
2000, //year
1, // month
1, // day
13, // hour (midnight is zero)
0, // minute
0, // second
0 // milliseconds
);
System.out.println((dateTime4.getMillis() - dateTime3.getMillis()) / 1000 / 3600);
}
}
3.总结
JodaTime确实比Calendar好用,开发过程中如果使用时间比较多,能够节省很多开发时间。而且JodaTime时间都是不可变的,类似于字符串,也不存在线程安全问题。
官网地址:http://www.joda.org/joda-time/
网友评论