在 Java 中处理日期和时间是很常见的需求,基础的工具类就是我们熟悉的 Date 和 Calendar,之前我也曾经写过一篇文章利用这两个类,怎么处理时间,然而这些工具类的 api 使用并不是很方便和强大,于是就诞生了Joda-Time 这个专门处理日期时间的库。而且 Joda-Time 很优秀,用了他之后再也停不下来,其在 Java 8 出现前的很长时间内成为 Java 中日期时间处理的事实标准,用来弥补 JDK 的不足。项目中要想使用 Joda-Time 很简单,只需要引入依赖:
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.9.9</version>
</dependency>
下面说一些常用的例子,供平时参考
- 创建任意时间对象
DateTime dateTime=new DateTime(2018, 03, 31, 16, 57,55);
- 创建当前时间对象
DateTime dateTime=new DateTime();
- 格式化时间输出
DateTime dateTime = new DateTime();
System.out.println(dateTime.toString("yyyy-MM-dd HH:mm:ss"));
- 解析文本格式时间
DateTimeFormatter format = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss");
DateTime dateTime = DateTime.parse("2018-03-31 17:01:45", format);
- 在当前日期上加上 90 天并输出结果
DateTime dateTime = new DateTime();
System.out.println(dateTime.plusDays(90).toString("yyyy-MM-dd HH:mm:ss");
- 获取今天的开始时间
DateTime nowTime = new DateTime();
DateTime startOfDay = nowTime.withTimeAtStartOfDay();
- 获取今天的结束时间
DateTime nowTime = new DateTime();
DateTime endOfDay = nowTime.millisOfDay().withMaximumValue();
- 计算两个日期的相隔天数
DateTime nowTime = new DateTime();
DateTime futureTime = new DateTime(2019, 10, 1, 0, 0, 0);
Int days = Days.daysBetween(nowTime, futureTime).getDays();
- 本月的第一天和最后一天
DateTime dateTime = new DateTime();
DateTime firstDateTimeOfMonth = dateTime.dayOfMonth().withMinimumValue().withTimeAtStartOfDay();
DateTime lastDateTimeOfMonth = dateTime.dayOfMonth().withMaximumValue().withTimeAtStartOfDay();
- 上个月的第一天和最后一天
DateTime dateTime = new DateTime();
DateTime startDate = dateTime.plusMonths(-1).dayOfMonth().withMinimumValue().withTimeAtStartOfDay();
DateTime endDate = dateTime.plusMonths(-1).dayOfMonth().withMaximumValue().millisOfDay().withMaximumValue();
- 在每天的6:30处理一些东西
DateTime dt=new DateTime().withHourOfDay(6).withMinuteOfHour(30).withSecondOfMinute(0);
- 在每月的7号的6:30处理一些东西
DateTime dt=new DateTime().withDayOfMonth(7).withHourOfDay(6).withMinuteOfHour(30).withSecondOfMinute(0);
- 在每年的8月的7号的6:30处理一些东西
DateTime dt=new DateTime().withMonthOfYear(8).withDayOfMonth(7).withHourOfDay(6).withMinuteOfHour(30).withSecondOfMinute(0);
- 获取年终 月、日、时、分、秒、毫秒
DateTime dt = new DateTime();
//获取当前时间的年
int year = dt.getYear();
//获取当前时间的月
int month = dt.getMonthOfYear();
//获取当前时间是一年中的第几天
int dayOfYear = dt.getDayOfYear();
//获取一个月中的天
int day = dt.getDayOfMonth();
//获取一周中的周几
int week = dt.getDayOfWeek();
//一天中的第几小时(取整)
int hour = dt.getHourOfDay();
//获取星期年
int weekOfyear = dt.getWeekyear();
//当前时间的秒中的毫秒
int ms = dt.getMillisOfSecond();
//获取当前时间的秒
int second = dt.getSecondOfDay();
//获取当前时间的毫秒
long millis = dt.getMillis();
- 判断时间跨度是否包含当前时间,某个时间
Interval interval = new Interval(new DateTime(2018, 1, 1, 0, 0, 0), new DateTime(2018, 3, 30, 0, 0, 0));
System.out.println(interval.containsNow());
boolean contained = interval.contains(new DateTime("2012-03-01"));
- 与JDK互转换
//通过jdk时间对象构造
Date date = new Date();
DateTime dateTime = new DateTime(date);
Calendar calendar = Calendar.getInstance();
dateTime = new DateTime(calendar);
// Joda-time 各种操作…..
dateTime = dateTime.plusDays(1) // 增加天
.plusYears(1)// 增加年
.plusMonths(1)// 增加月
.plusWeeks(1)// 增加星期
.minusMillis(1)// 减分钟
.minusHours(1)// 减小时
.minusSeconds(1);// 减秒数
// 计算完转换成jdk 对象
Date date2 = dateTime.toDate();
Calendar calendar2 = dateTime.toCalendar(Locale.CHINA);
- 解析 CST 格式的时间字符串
DateTimeFormatter format = DateTimeFormat.forPattern("EEE MMM dd HH:mm:ss zzz yyyy");
DateTime dateTime = DateTime.parse("Fri Mar 30 00:00:00 CST 2018", format);
当然作为一个强大的 时间日期 处理工具类,他还有更多更强大的 API,可以参看:
http://www.joda.org/joda-time/apidocs/index.html
https://github.com/JodaOrg/joda-time