JodaTime 日期时间推算处理、年、月、日、周格式转换、常用方法说明

日期时间的推算

几天(年\月\周\日\秒…)前:方法名前缀均为minus…
几天(年\月\周\日\秒…)后:方法名前缀均为plus…
如下示例:
推算5天前的日期: DateTime daysPast = now.minusDays(5);
推算5天后的日期: DateTime daysFuture = now.plusDays(5);

推算5个月前的日期: DateTime monthsPast = now.minusMonths(5);
推算5个月后的日期: DateTime monthsFuture = now.plusMonths(5);

推算日期时间的常用方法:

对象类型参数类型
.plusYears().minusYears()DateTimeint
.plusMonths().minusMonths()DateTimeint
.plusWeeks().minusWeeks()DateTimeint
.plusDays().minusDays()DateTimeint
小时.plusHours().minusHours()DateTimeint
分钟.plusMinutes().minusMinutes()DateTimeint
.plusSeconds().minusSeconds()DateTimeint
毫秒.plusMillis().minusMillis()DateTimeint

格式转换

将特定格式的字符串解析为DateTime类型的日期时间对象:
DateTime.parse(str, DateTimeFormat.forPattern(pattern));
其中,str是待解析的字符串,pattern为待解析的字符串的时间格式
例如:

String dateStr = "2021-11-15 10:20:35";
DateTime date = DateTime.parse(dateStr, DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss"));

这样就成功的将String类型的时间,转为可以直接运算处理的DateTime对象了。

将DateTime对象转为指定格式的日期时间

DateTime nowDate = new DateTime();
//此处.toString()方法内的参数,填写转换后的日期时间格式
String dateStr = nowDate.toString("yyyy-MM-dd");

将DateTime对象转为Date类型

DateTime nowDate = new DateTime();
Date date = nowDate.toDate();

获得常用的特殊时间的方法

获得一年内的第一天、最后一天,或是一小时内的第一分钟、最后一分钟
类似 2021-01-01 00:00:002021-12-31 23:59:59
先上代码:

DateTime nowDate = new DateTime();
//一年中的第一天,0秒 如: 2021-01-01 00:00:00
String startDate = nowDate.dayOfYear().withMinimumValue().secondOfDay().withMinimumValue().toString("yyyy-MM-dd HH:mm:ss");
//一年中的最后一天,最后一秒 如: 2021-12-31 23:59:59
String endDate = nowDate.dayOfYear().withMaximumValue().secondOfDay().withMaximumValue().toString("yyyy-MM-dd HH:mm:ss");

解释一下:
.dayOfYear()方法表示指定对一年中的天数进行设置,随后紧跟着的.withMinimumValue()方法表示取最小值,这样组合起来就构造出了一年中的第一天。
同理,时间的部分取最小值也可以通过.secondOfDay().withMinimumValue() 这俩方法的组合达到对应的效果。
.withMaximumValue(): 对指定的某个时间范围内(如.secondOfDay().dayOfMonth()…)取最大值。

设置时间日期属性方法说明直接返回对应的时间日期方法(int)
yearOfCentury()当前世纪中的年数getYearOfCentury()
year()常规年数getYear()
weekyear()按周计算的年数getWeekyear()
weekOfWeekyear()一年内的周数getWeekOfWeekyear()
monthOfYear()一年内的月份getMonthOfYear()
dayOfYear()一年内的天getDayOfYear()
dayOfMonth()一月内的天数getDayOfMonth()
dayOfWeek()一周内的天数getDayOfWeek()
hourOfDay()一天内的小时数getHourOfDay()
minuteOfDay()一天内的分钟数getMinuteOfDay()
minuteOfHour()一小时内的分钟getMinuteOfHour()
secondOfDay()一天内的秒数getSecondOfDay()
secondOfMinute()一分钟内的秒数getSecondOfMinute()
millisOfDay()一天内的毫秒数getMillisOfDay()
millisOfSecond()一秒内的毫秒数getMillisOfSecond()

注: 表格中左侧的方法均用于设置时间日期属性,便于处理时间日期,返回值类型为DateTime.Property,通常与withMaximumValue()withMinimumValue()get()等方法配合使用。

另外需要单独说明的方法是getWeekyear()getWeekOfWeekyear()以及他俩对应的get方法。

  • getWeekyear(): 是按周计算的年份。比如2021-01-01日,按常规年份来说是2021年,但是这一天是周五,它所在的那周是从2020-12-28日的周一开始的,所以它所返回的年份是2020。
  • getWeekOfWeekyear(): 同上。以周一为一周的开始,以每年的第一个周一作为第一周。所以2021-01-01日并不会被当作第一周,而是上一年的53周。
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值