Joda-Time 的使用

DateTime

构造函数

DateTime dateTime = new DateTime()

创建当前时间的 dateTime

DateTime dateTime = new DateTime(Integer year, Integer month, Integer day, Integer hour, Integer minute, Integer second);

传入参数创建对应的 dateTime

DateTime dateTime = new DateTime(Integer year, Integer month, Integer day, Integer hour, Integer minute);

传入参数创建对应的 dateTime

方法

方法 含义 举例
Property pro = .year() 获得年
Property pro = .monthOfYear() 获得月
Property pro = .dayOfMonth() 获得天
Property pro = .dayOfYear() 获得当天属于当年第多少天
Property pro = .dayOfWeek() 获得当前属于这个星期的星期几
Property pro = .hourOfDay() 获得小时
Property pro = .minuteOfHour() 获得分
Property pro = .secondOfMinute() 获得秒
Property pro = .millisOfSecond() 获得毫秒
DateTime dateTime = .plus(Long millisSecond) 当前dateTime加上+millsSecond毫秒 当前时间+10秒
new DateTime().plus(10000)
DateTime dateTime = .plusYears(Integer day)
plusMonths、plusDays、plusHours、plusMinutes、plusSeconds、plusWeeks
当前时间加上年、月、日、时、分、秒
plusWeeks 加几个周
如果是负数就是减n
获得下周当前时间
new DateTime().plusWeeks(1);
String str = .toString(String forMaterStr) 当前时间根据传入格式转为字符串 输出 yyyy-MM-dd HH:mm:ss
new DateTime().toString(“yyyy-MM-dd HH:mm:ss)
Boolean isBefore = .isBefore(Long millisSecond);
Boolean isBefore =.isBeforeNow()
比较当前时间是否在millisSecond之前,isBeforeNow和现在时间进行对比 Boolean isBefore = dateTime.isBeforeNow();
// 注意如果两个时间相差小于一毫秒则两者时间默认为相同时间
Boolean isAfter= .isAfter(Long millisSecond);
Boolean isAfter=.isAfterNow()
同上不过相反 同上不过相反
DateTime dateTime = .withYear(int year); 设置当前时间的年 dateTime =dateTime.withYear(2019)
withMonthOfYear(int month); 设置当前时间为第几个月
withDayOfMonth(int day); 设置当前时间为当月的第几天
withHourOfDay(int hour); 设置为今天的第几个小时
withMinuteOfHour(int minute) 设置为当前小时的第几分钟
withSecondOfMinute(int second); 设置为当前分钟的第几秒
withTime(int hour, int minute, int second, int millisSecond) 设置当前时间的小时、分钟、秒钟、毫秒
withDayOfYear(int day); 区别于withDayOfMonth(),这个是设置当前时间为当年的第多少天
withMillisOfDay(Long millisSeconds); 设置时间为当天的第多少毫秒

区间判断


Duration

构造方法

Duration duration = new Duration(DateTime beginTime,DateTime endTime)

方法

  • long lon = getStandardMinutes();获得两者的分钟差值
  • long lon = getStandardHours();获得两者的小时钟差值
  • long lon = getStandardDays();获得两者的天差值
  • long lon = getStandardSeconds();获得两者的秒钟差值
  • long lon = getMillis();获得两者的毫秒差值
System.out.println("区间值"+
new Duration(new DateTime().plusMinutes(1),new DateTime()).getMillis()
);
-- 区间值 -60000

Interval

构造方法

Interval i = new Interval(DateTime beginTime,DateTime endTime);

判断目标时间是否在两者质检

boolean contained = i.contains(new DateTime().plus(-1000));

//起始时间当前时间 - 1分钟
DateTime startTime = new DateTime().plusMinutes(-1);
//终止时间当前时间 + 1天
DateTime endTime = new DateTime().plusDays(1);
Interval interval = new Interval(startTime, endTime);
DateTime dateTime1 = new DateTime();
if (interval.contains(dateTime1)) {
System.out.println(dateTime1.toString(DATEFORMAT)+",在区间内");
}
dateTime1 = new DateTime().plusDays(2);
if (!interval.contains(dateTime1)) {
System.out.println(dateTime1.toString(DATEFORMAT)+",不在区间内");
}
--
2019-11-11 09:16:47,在区间内
2019-11-13 09:16:47,不在区间内 

Property

构造函数

Property的创建大多数是由DateTime 的方法 构成的,比如:

Property pro = .year(); //获得年Property 
Property pro = .monthOfYear(); //获得月Property 
Property pro = .dayOfMonth(); //获得天Property 
Property pro = .dayOfYear(); //获得当天属于当年第多少天
Property pro = .dayOfWeek(); //获得当前属于这个星期的星期几
Property pro = .hourOfDay(); //获得小时
Property pro = .minuteOfHour(); //获得分
Property pro = .secondOfMinute(); //获得秒
Property pro = .millisOfSecond(); //获得毫秒

方法

字符串输出当前时间的选择

String str = getAsString()

e.g.:

System.out.println("今天是几月:"+new DateTime().dayOfMonth().getAsString());
// 今天是几月:11

所选Property 增加 i 个时间

DateTime time = addToCopy(Integer i)

e.g.

System.out.println("addToCopy+1后是几月:"+property.addToCopy(1).dayOfMonth().getAsString());
// addToCopy+1后是几月:12

转为对应得DateTime类型

DateTime time = getDateTime()

e.g.:

dateTime = new DateTime().plusWeeks(1);
DateTime.Property property = dateTime.dayOfMonth(); System.out.println(dateTime.toString(DATEFORMAT)); System.out.println(property.getDateTime().toString(DATEFORMAT));
// 2019-11-18 11:18:46
// 2019-11-18 11:18:46 

获得正在使用得字段

DateTimeField dateTimeField = getField();

e.g.:

DateTime dateTime = new DateTime();
DateTime.Property property = dateTime.dayOfMonth();
System.out.println(property.getField().getName());
// dayOfMonth 

获得毫秒数

Long lon =getMillis()

获得明天的开始时间(天花板)

DateTime time = .roundCeilingCopy()

e.g.:

DateTime dateTime = new DateTime();
System.out.println(dateTime.toString(DATEFORMAT));
System.out.println(dateTime.dayOfMonth().roundCeilingCopy().toString(DATEFORMAT));
// 2019-11-11 11:54:50
// 2019-11-12 00:00:00

获得今天的开始时间(地板)

DateTime time = .roundHalfFloorCopy()

e.g.:

DateTime dateTime = new DateTime();
System.out.println(dateTime.toString(DATEFORMAT));
System.out.println(dateTime.dayOfMonth().roundHalfFloorCopy().toString(DATEFORMAT));
// 2019-11-11 11:54:58
// 2019-11-11 00:00:00

获得时间的阈值时间

也就是获得今天的开始时间或明天的开始时间

DateTime time = .roundHalfCeilingCopy() //获得时间的阈值,今天得开始时间<12:00<=明天的开始时间
DateTime time = .roundHalfFloorCopy() //获得时间的阈值,今天得开始时间<=12:00<明天的开始时间
注意两者的不同 两则在中间时间的取值不一样
e.g.:

DateTime dateTime = new DateTime();
System.out.println(dateTime.toString(DATEFORMAT));
System.out.println(dateTime.dayOfMonth().roundHalfFloorCopy().toString(DATEFORMAT));
// 2019-11-11 11:54:58
// 2019-11-11 00:00:00

获得当前时间字段的最大(小)值

DateTime time = .withMaximumValue(); //根据property所属的字段来获得其最大,例如是 dayOfMonth() 则为当月的最大一日 30 or 31
DateTime time = .withMinimumValue(); //根据property所属的字段来获得其最小,例如是 dayOfMonth() 则为当月的最小 1

注意年的最大(小)值为292278993(-292275054)

System.out.println("max:" + new DateTime().monthOfYear().withMaximumValue().toString(DATEFORMAT));
System.out.println("min:" + new DateTime().monthOfYear().withMinimumValue().toString(DATEFORMAT));
// max:2019-12-11 14:08:41
// min:2019-01-11 14:08:41
原文地址:https://www.cnblogs.com/zhuyanpeng/p/12720003.html