# Joda-Time几个用法小记

### 1, 获取每天的零点

```DateTime dt=new DateTime().withMillisOfDay(0);
System.out.println(dt.toString("yyyy-MM-dd HH:mm:ss"));//结果2016-09-09 00:00:00```

### 2, 在每天的6:30处理一些东西

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

### 3, 在每月的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"));//结果2016-09-07 06:30:00```

### 4, 在每年的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"));//结果2016-08-07 06:30:00```

### 5, 获取每个月的第一天和最后一天

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

### 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：0days：1hours：14minutes：50second：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));```

### 9, 时间判断是否在某个范围以及获取时间差的毫秒

```DateTime start=new DateTime(2015,5,4,12,20);
DateTime end  =new DateTime(2015,5,5,12,00);
Interval interval=new Interval(start,end);
System.out.println(interval.contains(new DateTime(2015,5,5,11,00)));

Duration duration=new Duration(start,end);
System.out.println(duration.getStandardHours());```

#### 官网连接

http://www.joda.org/joda-time/userguide.html

487 篇文章72 人订阅

0 条评论

## 相关文章

35380

30570

21190

### C#的DataTable操作方法

1.将泛型集合类转换成DataTable（表中无数据时使用）： public static DataTable NullListToDataTable(IL...

28170

29810

390130

32770

31150

19670

272100