joda-time

2019-02-10  本文已影响0人  西三旗靓仔

最近在工作中接触到各种时间处理,在网上找到一个好用到库,分享给大家。
maven依赖

<dependency>
  <groupId>joda-time</groupId>
  <artifactId>joda-time</artifactId>
  <version>2.9.7</version>
</dependency>

1、获取每天凌晨

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

//结果
2019-02-10 00:00:00

2、在每年的8月的7号的6:30处理一些东西

DateTime dt=new DateTime().withMonthOfYear(8).withDayOfMonth(7).withHourOfDay(6).withMinuteOfHour(30).withSecondOfMinute(0);
System.out.println(dt.toString("yyyy-MM-dd HH:mm:ss"));
//结果
2019-08-07 06:30:00

3、获取每个月的第一天和最后一天

DateTime dateTime=new DateTime();
System.out.println(dateTime.dayOfMonth().withMinimumValue().dayOfMonth().get());
System.out.println(dateTime.dayOfMonth().withMaximumValue().dayOfMonth().get());
//结果
1
28

4、在每天的6:30处理一些东西

DateTime dt=new DateTime().withHourOfDay(6).withMinuteOfHour(30).withSecondOfMinute(0);
System.out.println(dt.toString("yyyy-MM-dd HH:mm:ss"));
//结果
2019-02-10 06:30:00

5、在每月的7号的6:30处理一些东西

DateTime dt=new DateTime().withDayOfMonth(7).withHourOfDay(6).withMinuteOfHour(30).withSecondOfMinute(0);
System.out.println(dt.toString("yyyy-MM-dd HH:mm:ss"));
//结果
2019-02-07 06:30:00

6、获取每天的零点的下一天零点

DateTime dt=new DateTime().withMillisOfDay(0).plusDays(1);
System.out.println(dt.toString("yyyy-MM-dd HH:mm:ss"));

7、获取日期差

DateTime start=new DateTime(2015,5,4,12,20);
DateTime start=new DateTime(2015,5,4,12,20);
DateTime end=new DateTime(2015,5,6,3,10);
Period period=new Period(start,end);
System.out.println("month:"+period.getMonths());
System.out.println("days:"+period.getDays());
System.out.println("hours:"+period.getHours());
System.out.println("minutes:"+period.getMinutes());
System.out.println("second:"+period.getSeconds());
//结果
month:0
days:1
hours:14
minutes:50
second:0

8、单独获取天,小时,分钟,秒

DateTime start=new DateTime(2015,5,4,12,20);
DateTime end  =new DateTime(2015,5,5,12,00);
System.out.println(Days.daysBetween(start,end).getDays());
System.out.println(Hours.hoursBetween(start,end).getHours());
System.out.println(Minutes.minutesBetween(start,end).getMinutes());
System.out.println(Seconds.secondsBetween(start,end));
上一篇 下一篇

猜你喜欢

热点阅读