This commit is contained in:
Looly
2019-09-18 17:29:29 +08:00
parent 8b94fa0c5d
commit 8e70a2aca7
6 changed files with 346 additions and 279 deletions

View File

@@ -17,23 +17,28 @@ import cn.hutool.core.util.StrUtil;
/**
* 包装java.util.Date
*
* @author xiaoleilu
*
* @author xiaoleilu
*/
public class DateTime extends Date {
private static final long serialVersionUID = -5395712593979185936L;
/** 是否可变对象 */
/**
* 是否可变对象
*/
private boolean mutable = true;
/** 一周的第一天,默认是周一, 在设置或获得 WEEK_OF_MONTH 或 WEEK_OF_YEAR 字段时Calendar 必须确定一个月或一年的第一个星期,以此作为参考点。 */
/**
* 一周的第一天,默认是周一, 在设置或获得 WEEK_OF_MONTH 或 WEEK_OF_YEAR 字段时Calendar 必须确定一个月或一年的第一个星期,以此作为参考点。
*/
private Week firstDayOfWeek = Week.MONDAY;
/** 时区 */
/**
* 时区
*/
private TimeZone timeZone;
/**
* 转换时间戳为 DateTime
*
*
* @param timeMillis 时间戳,毫秒数
* @return DateTime
* @since 4.6.3
@@ -44,7 +49,7 @@ public class DateTime extends Date {
/**
* 转换JDK date为 DateTime
*
*
* @param date JDK Date
* @return DateTime
*/
@@ -57,7 +62,7 @@ public class DateTime extends Date {
/**
* 转换 {@link Calendar} 为 DateTime
*
*
* @param calendar {@link Calendar}
* @return DateTime
*/
@@ -67,11 +72,11 @@ public class DateTime extends Date {
/**
* 构造
*
* @see DatePattern
*
* @param dateStr Date字符串
* @param format 格式
* @param format 格式
* @return {@link DateTime}
* @see DatePattern
*/
public static DateTime of(String dateStr, String format) {
return new DateTime(dateStr, format);
@@ -79,7 +84,7 @@ public class DateTime extends Date {
/**
* 现在的时间
*
*
* @return 现在的时间
*/
public static DateTime now() {
@@ -87,9 +92,9 @@ public class DateTime extends Date {
}
// -------------------------------------------------------------------- Constructor start
/**
* 当前时间
*
*/
public DateTime() {
this(TimeZone.getDefault());
@@ -97,7 +102,7 @@ public class DateTime extends Date {
/**
* 当前时间
*
*
* @param timeZone 时区
* @since 4.1.2
*/
@@ -107,17 +112,20 @@ public class DateTime extends Date {
/**
* 给定日期的构造
*
*
* @param date 日期
*/
public DateTime(Date date) {
this(date.getTime(), TimeZone.getDefault());
this(
date.getTime(),//
(date instanceof DateTime) ? ((DateTime) date).timeZone : TimeZone.getDefault()
);
}
/**
* 给定日期的构造
*
* @param date 日期
*
* @param date 日期
* @param timeZone 时区
* @since 4.1.2
*/
@@ -127,28 +135,28 @@ public class DateTime extends Date {
/**
* 给定日期的构造
*
*
* @param calendar {@link Calendar}
*/
public DateTime(Calendar calendar) {
this(calendar.getTime(), (TimeZone) null);
this(calendar.getTime(), calendar.getTimeZone());
}
/**
* 给定日期毫秒数的构造
*
*
* @param timeMillis 日期毫秒数
* @since 4.1.2
*/
public DateTime(long timeMillis) {
this(timeMillis, (TimeZone) null);
this(timeMillis, TimeZone.getDefault());
}
/**
* 给定日期毫秒数的构造
*
*
* @param timeMillis 日期毫秒数
* @param timeZone 时区
* @param timeZone 时区
* @since 4.1.2
*/
public DateTime(long timeMillis, TimeZone timeZone) {
@@ -160,10 +168,10 @@ public class DateTime extends Date {
/**
* 构造
*
* @see DatePattern
*
* @param dateStr Date字符串
* @param format 格式
* @param format 格式
* @see DatePattern
*/
public DateTime(String dateStr, String format) {
this(dateStr, new SimpleDateFormat(format));
@@ -171,10 +179,10 @@ public class DateTime extends Date {
/**
* 构造
*
* @see DatePattern
* @param dateStr Date字符串
*
* @param dateStr Date字符串
* @param dateFormat 格式化器 {@link SimpleDateFormat}
* @see DatePattern
*/
public DateTime(String dateStr, DateFormat dateFormat) {
this(parse(dateStr, dateFormat), dateFormat.getTimeZone());
@@ -182,24 +190,25 @@ public class DateTime extends Date {
/**
* 构造
*
* @see DatePattern
* @param dateStr Date字符串
*
* @param dateStr Date字符串
* @param dateParser 格式化器 {@link DateParser},可以使用 {@link FastDateFormat}
* @see DatePattern
*/
public DateTime(String dateStr, DateParser dateParser) {
this(parse(dateStr, dateParser), dateParser.getTimeZone());
}
// -------------------------------------------------------------------- Constructor end
// -------------------------------------------------------------------- offset start
/**
* 调整日期和时间<br>
* 如果此对象为可变对象,返回自身,否则返回新对象,设置是否可变对象见{@link #setMutable(boolean)}
*
*
* @param datePart 调整的部分 {@link DateField}
* @param offset 偏移量,正数为向后偏移,负数为向前偏移
* @param offset 偏移量,正数为向后偏移,负数为向前偏移
* @return 如果此对象为可变对象,返回自身,否则返回新对象
*/
public DateTime offset(DateField datePart, int offset) {
@@ -213,9 +222,9 @@ public class DateTime extends Date {
/**
* 调整日期和时间<br>
* 返回调整后的新{@link DateTime},不影响原对象
*
*
* @param datePart 调整的部分 {@link DateField}
* @param offset 偏移量,正数为向后偏移,负数为向前偏移
* @param offset 偏移量,正数为向后偏移,负数为向前偏移
* @return 如果此对象为可变对象,返回自身,否则返回新对象
* @since 3.0.9
*/
@@ -229,10 +238,11 @@ public class DateTime extends Date {
// -------------------------------------------------------------------- offset end
// -------------------------------------------------------------------- Part of Date start
/**
* 获得日期的某个部分<br>
* 例如获得年的部分,则使用 getField(DatePart.YEAR)
*
*
* @param field 表示日期的哪个部分的枚举 {@link DateField}
* @return 某个部分的值
*/
@@ -243,7 +253,7 @@ public class DateTime extends Date {
/**
* 获得日期的某个部分<br>
* 例如获得年的部分,则使用 getField(Calendar.YEAR)
*
*
* @param field 表示日期的哪个部分的int值 {@link Calendar}
* @return 某个部分的值
*/
@@ -254,7 +264,7 @@ public class DateTime extends Date {
/**
* 设置日期的某个部分<br>
* 如果此对象为可变对象,返回自身,否则返回新对象,设置是否可变对象见{@link #setMutable(boolean)}
*
*
* @param field 表示日期的哪个部分的枚举 {@link DateField}
* @param value 值
* @return {@link DateTime}
@@ -266,7 +276,7 @@ public class DateTime extends Date {
/**
* 设置日期的某个部分<br>
* 如果此对象为可变对象,返回自身,否则返回新对象,设置是否可变对象见{@link #setMutable(boolean)}
*
*
* @param field 表示日期的哪个部分的int值 {@link Calendar}
* @param value 值
* @return {@link DateTime}
@@ -293,7 +303,7 @@ public class DateTime extends Date {
/**
* 获得年的部分
*
*
* @return 年的部分
*/
public int year() {
@@ -306,7 +316,7 @@ public class DateTime extends Date {
* 2第二季度<br>
* 3第三季度<br>
* 4第四季度<br>
*
*
* @return 第几个季度
* @deprecated 请使用{@link Quarter}代替
*/
@@ -317,7 +327,7 @@ public class DateTime extends Date {
/**
* 获得当前日期所属季度<br>
*
*
* @return 第几个季度 {@link Season}
* @deprecated 请使用{@link #quarterEnum}代替
*/
@@ -328,7 +338,7 @@ public class DateTime extends Date {
/**
* 获得当前日期所属季度从1开始计数<br>
*
*
* @return 第几个季度 {@link Quarter}
*/
public int quarter() {
@@ -337,7 +347,7 @@ public class DateTime extends Date {
/**
* 获得当前日期所属季度<br>
*
*
* @return 第几个季度 {@link Quarter}
*/
public Quarter quarterEnum() {
@@ -346,7 +356,7 @@ public class DateTime extends Date {
/**
* 获得月份从0开始计数
*
*
* @return 月份
*/
public int month() {
@@ -356,7 +366,7 @@ public class DateTime extends Date {
/**
* 获得月份从1开始计数<br>
* 由于{@link Calendar} 中的月份按照0开始计数导致某些需求容易误解因此如果想用1表示一月2表示二月则调用此方法
*
*
* @return 月份
*/
public int monthStartFromOne() {
@@ -365,7 +375,7 @@ public class DateTime extends Date {
/**
* 获得月份
*
*
* @return {@link Month}
*/
public Month monthEnum() {
@@ -378,7 +388,7 @@ public class DateTime extends Date {
* 2016年1月3日为周日如果一周的第一天为周日那这天是第二周返回2<br>
* 如果一周的第一天为周一那这天是第一周返回1<br>
* 跨年的那个星期得到的结果总是1
*
*
* @return 周
* @see #setFirstDayOfWeek(Week)
*/
@@ -391,7 +401,7 @@ public class DateTime extends Date {
* 此方法返回值与一周的第一天有关,比如:<br>
* 2016年1月3日为周日如果一周的第一天为周日那这天是第二周返回2<br>
* 如果一周的第一天为周一那这天是第一周返回1
*
*
* @return 周
* @see #setFirstDayOfWeek(Week)
*/
@@ -401,7 +411,7 @@ public class DateTime extends Date {
/**
* 获得指定日期是这个日期所在月份的第几天<br>
*
*
* @return 天
*/
public int dayOfMonth() {
@@ -410,7 +420,7 @@ public class DateTime extends Date {
/**
* 获得指定日期是星期几1表示周日2表示周一
*
*
* @return 星期几
*/
public int dayOfWeek() {
@@ -419,7 +429,7 @@ public class DateTime extends Date {
/**
* 获得天所在的周是这个月的第几周
*
*
* @return 天
*/
public int dayOfWeekInMonth() {
@@ -428,7 +438,7 @@ public class DateTime extends Date {
/**
* 获得指定日期是星期几
*
*
* @return {@link Week}
*/
public Week dayOfWeekEnum() {
@@ -437,7 +447,7 @@ public class DateTime extends Date {
/**
* 获得指定日期的小时数部分<br>
*
*
* @param is24HourClock 是否24小时制
* @return 小时数
*/
@@ -448,7 +458,7 @@ public class DateTime extends Date {
/**
* 获得指定日期的分钟数部分<br>
* 例如10:04:15.250 =》 4
*
*
* @return 分钟数
*/
public int minute() {
@@ -457,7 +467,7 @@ public class DateTime extends Date {
/**
* 获得指定日期的秒数部分<br>
*
*
* @return 秒数
*/
public int second() {
@@ -466,7 +476,7 @@ public class DateTime extends Date {
/**
* 获得指定日期的毫秒数部分<br>
*
*
* @return 毫秒数
*/
public int millsecond() {
@@ -475,7 +485,7 @@ public class DateTime extends Date {
/**
* 是否为上午
*
*
* @return 是否为上午
*/
public boolean isAM() {
@@ -484,7 +494,7 @@ public class DateTime extends Date {
/**
* 是否为下午
*
*
* @return 是否为下午
*/
public boolean isPM() {
@@ -493,7 +503,7 @@ public class DateTime extends Date {
/**
* 是否为周末,周末指周六或者周日
*
*
* @return 是否为周末,周末指周六或者周日
* @since 4.1.14
*/
@@ -505,9 +515,9 @@ public class DateTime extends Date {
/**
* 是否闰年
*
* @see DateUtil#isLeapYear(int)
*
* @return 是否闰年
* @see DateUtil#isLeapYear(int)
*/
public boolean isLeapYear() {
return DateUtil.isLeapYear(year());
@@ -515,7 +525,7 @@ public class DateTime extends Date {
/**
* 转换为Calendar, 默认 {@link Locale}
*
*
* @return {@link Calendar}
*/
public Calendar toCalendar() {
@@ -524,7 +534,7 @@ public class DateTime extends Date {
/**
* 转换为Calendar
*
*
* @param locale 地域 {@link Locale}
* @return {@link Calendar}
*/
@@ -534,7 +544,7 @@ public class DateTime extends Date {
/**
* 转换为Calendar
*
*
* @param zone 时区 {@link TimeZone}
* @return {@link Calendar}
*/
@@ -544,8 +554,8 @@ public class DateTime extends Date {
/**
* 转换为Calendar
*
* @param zone 时区 {@link TimeZone}
*
* @param zone 时区 {@link TimeZone}
* @param locale 地域 {@link Locale}
* @return {@link Calendar}
*/
@@ -562,7 +572,7 @@ public class DateTime extends Date {
/**
* 转换为 {@link Date}<br>
* 考虑到很多框架例如Hibernate的兼容性提供此方法返回JDK原生的Date对象
*
*
* @return {@link Date}
* @since 3.2.2
*/
@@ -572,7 +582,7 @@ public class DateTime extends Date {
/**
* 转为{@link Timestamp}
*
*
* @return {@link Timestamp}
*/
public Timestamp toTimestamp() {
@@ -581,7 +591,7 @@ public class DateTime extends Date {
/**
* 转为 {@link java.sql.Date}
*
*
* @return {@link java.sql.Date}
*/
public java.sql.Date toSqlDate() {
@@ -590,7 +600,7 @@ public class DateTime extends Date {
/**
* 计算相差时长
*
*
* @param date 对比的日期
* @return {@link DateBetween}
*/
@@ -600,7 +610,7 @@ public class DateTime extends Date {
/**
* 计算相差时长
*
*
* @param date 对比的日期
* @param unit 单位 {@link DateUnit}
* @return 相差时长
@@ -611,9 +621,9 @@ public class DateTime extends Date {
/**
* 计算相差时长
*
* @param date 对比的日期
* @param unit 单位 {@link DateUnit}
*
* @param date 对比的日期
* @param unit 单位 {@link DateUnit}
* @param formatLevel 格式化级别
* @return 相差时长
*/
@@ -624,9 +634,9 @@ public class DateTime extends Date {
/**
* 当前日期是否在日期指定范围内<br>
* 起始日期和结束日期可以互换
*
*
* @param beginDate 起始日期
* @param endDate 结束日期
* @param endDate 结束日期
* @return 是否在范围内
* @since 3.0.8
*/
@@ -640,7 +650,7 @@ public class DateTime extends Date {
/**
* 是否在给定日期之前
*
*
* @param date 日期
* @return 是否在给定日期之前
* @since 4.1.3
@@ -654,7 +664,7 @@ public class DateTime extends Date {
/**
* 是否在给定日期之前或与给定日期相等
*
*
* @param date 日期
* @return 是否在给定日期之前或与给定日期相等
* @since 3.0.9
@@ -668,7 +678,7 @@ public class DateTime extends Date {
/**
* 是否在给定日期之后
*
*
* @param date 日期
* @return 是否在给定日期之后
* @since 4.1.3
@@ -682,7 +692,7 @@ public class DateTime extends Date {
/**
* 是否在给定日期之后或与给定日期相等
*
*
* @param date 日期
* @return 是否在给定日期之后或与给定日期相等
* @since 3.0.9
@@ -703,7 +713,7 @@ public class DateTime extends Date {
* <li>{@link DateTime#setField(int, int)}</li>
* </ul>
* 如果为不可变对象,{@link DateTime#setTime(long)}将抛出异常
*
*
* @return 对象是否可变
*/
public boolean isMutable() {
@@ -718,7 +728,7 @@ public class DateTime extends Date {
* <li>{@link DateTime#setField(int, int)}</li>
* </ul>
* 如果为不可变对象,{@link DateTime#setTime(long)}将抛出异常
*
*
* @param mutable 是否可变
* @return this
*/
@@ -729,7 +739,7 @@ public class DateTime extends Date {
/**
* 获得一周的第一天,默认为周一
*
*
* @return 一周的第一天
*/
public Week getFirstDayOfWeek() {
@@ -740,7 +750,7 @@ public class DateTime extends Date {
* 设置一周的第一天<br>
* JDK的Calendar中默认一周的第一天是周日Hutool中将此默认值设置为周一<br>
* 设置一周的第一天主要影响{@link #weekOfMonth()}和{@link #weekOfYear()} 两个方法
*
*
* @param firstDayOfWeek 一周的第一天
* @return this
* @see #weekOfMonth()
@@ -753,7 +763,7 @@ public class DateTime extends Date {
/**
* 设置时区
*
*
* @param timeZone 时区
* @return this
* @since 4.1.2
@@ -764,10 +774,11 @@ public class DateTime extends Date {
}
// -------------------------------------------------------------------- toString start
/**
* 转为"yyyy-MM-dd yyyy-MM-dd HH:mm:ss " 格式字符串<br>
* 如果时区被设置,会转换为其时区对应的时间,否则转换为当前地点对应的时区
*
*
* @return "yyyy-MM-dd yyyy-MM-dd HH:mm:ss " 格式字符串
*/
@Override
@@ -778,7 +789,7 @@ public class DateTime extends Date {
/**
* 转为"yyyy-MM-dd yyyy-MM-dd HH:mm:ss " 格式字符串<br>
* 时区使用当前地区的默认时区
*
*
* @return "yyyy-MM-dd yyyy-MM-dd HH:mm:ss " 格式字符串
* @since 4.1.14
*/
@@ -789,7 +800,7 @@ public class DateTime extends Date {
/**
* 转为"yyyy-MM-dd yyyy-MM-dd HH:mm:ss " 格式字符串<br>
* 如果时区不为{@code null},会转换为其时区对应的时间,否则转换为当前时间对应的时区
*
*
* @param timeZone 时区
* @return "yyyy-MM-dd yyyy-MM-dd HH:mm:ss " 格式字符串
* @since 4.1.14
@@ -805,7 +816,7 @@ public class DateTime extends Date {
/**
* 转为"yyyy-MM-dd " 格式字符串
*
*
* @return "yyyy-MM-dd " 格式字符串
* @since 4.0.0
*/
@@ -820,7 +831,7 @@ public class DateTime extends Date {
/**
* 转为"HH:mm:ss" 格式字符串
*
*
* @return "HH:mm:ss" 格式字符串
* @since 4.1.4
*/
@@ -835,7 +846,7 @@ public class DateTime extends Date {
/**
* 转为字符串
*
*
* @param format 日期格式,常用格式见: {@link DatePattern}
* @return String
*/
@@ -850,7 +861,7 @@ public class DateTime extends Date {
/**
* 转为字符串
*
*
* @param format {@link DatePrinter} 或 {@link FastDateFormat}
* @return String
*/
@@ -860,7 +871,7 @@ public class DateTime extends Date {
/**
* 转为字符串
*
*
* @param format {@link SimpleDateFormat}
* @return String
*/
@@ -878,8 +889,8 @@ public class DateTime extends Date {
/**
* 转换字符串为Date
*
* @param dateStr 日期字符串
*
* @param dateStr 日期字符串
* @param dateFormat {@link SimpleDateFormat}
* @return {@link Date}
*/
@@ -899,9 +910,9 @@ public class DateTime extends Date {
/**
* 转换字符串为Date
*
*
* @param dateStr 日期字符串
* @param parser {@link FastDateFormat}
* @param parser {@link FastDateFormat}
* @return {@link Date}
*/
private static Date parse(String dateStr, DateParser parser) {
@@ -916,7 +927,7 @@ public class DateTime extends Date {
/**
* 设置日期时间
*
*
* @param time 日期时间毫秒
* @return this
*/

View File

@@ -1325,7 +1325,7 @@ public class DateUtil {
}
/**
* 获取指定日期偏移指定时间后的时间
* 获取指定日期偏移指定时间后的时间,生成的偏移日期不影响原日期
*
* @param date 基准日期
* @param dateField 偏移的粒度大小(小时、天、月等){@link DateField}
@@ -1333,10 +1333,7 @@ public class DateUtil {
* @return 偏移后的日期
*/
public static DateTime offset(Date date, DateField dateField, int offset) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(dateField.getValue(), offset);
return new DateTime(cal.getTime());
return dateNew(date).offset(dateField, offset);
}
/**

View File

@@ -5,13 +5,12 @@ import java.io.Serializable;
/**
* 计时器<br>
* 计算某个过程花费的时间,精确到毫秒
*
* @author Looly
*
* @author Looly
*/
public class TimeInterval implements Serializable{
public class TimeInterval implements Serializable {
private static final long serialVersionUID = 1L;
private long time;
private boolean isNano;
@@ -41,73 +40,92 @@ public class TimeInterval implements Serializable{
time = now;
return d;
}
/**
* 重新开始计算时间(重置开始时间)
*
* @return this
* @since 3.0.1
*/
public TimeInterval restart(){
public TimeInterval restart() {
time = DateUtil.current(isNano);
return this;
}
//----------------------------------------------------------- Interval
/**
* 从开始到当前的间隔时间(毫秒数)<br>
* 如果使用纳秒计时,返回纳秒差,否则返回毫秒差
*
* @return 从开始到当前的间隔时间(毫秒数)
*/
public long interval() {
return DateUtil.current(isNano) - time;
}
/**
* 从开始到当前的间隔时间毫秒数返回XX天XX小时XX分XX秒XX毫秒
*
* @return 从开始到当前的间隔时间(毫秒数)
* @since 4.6.7
*/
public String intervalPretty() {
return DateUtil.formatBetween(intervalMs());
}
/**
* 从开始到当前的间隔时间(毫秒数)
*
* @return 从开始到当前的间隔时间(毫秒数)
*/
public long intervalMs() {
return isNano ? interval() / 1000000L : interval();
}
/**
* 从开始到当前的间隔秒数,取绝对值
*
* @return 从开始到当前的间隔秒数,取绝对值
*/
public long intervalSecond(){
public long intervalSecond() {
return intervalMs() / DateUnit.SECOND.getMillis();
}
/**
* 从开始到当前的间隔分钟数,取绝对值
*
* @return 从开始到当前的间隔分钟数,取绝对值
*/
public long intervalMinute(){
public long intervalMinute() {
return intervalMs() / DateUnit.MINUTE.getMillis();
}
/**
* 从开始到当前的间隔小时数,取绝对值
*
* @return 从开始到当前的间隔小时数,取绝对值
*/
public long intervalHour(){
public long intervalHour() {
return intervalMs() / DateUnit.HOUR.getMillis();
}
/**
* 从开始到当前的间隔天数,取绝对值
*
* @return 从开始到当前的间隔天数,取绝对值
*/
public long intervalDay(){
public long intervalDay() {
return intervalMs() / DateUnit.DAY.getMillis();
}
/**
* 从开始到当前的间隔周数,取绝对值
*
* @return 从开始到当前的间隔周数,取绝对值
*/
public long intervalWeek(){
public long intervalWeek() {
return intervalMs() / DateUnit.WEEK.getMillis();
}
}

View File

@@ -1,31 +1,23 @@
package cn.hutool.core.date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.TimeZone;
import org.junit.Assert;
import org.junit.Test;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.BetweenFormater.Level;
import org.junit.Assert;
import org.junit.Test;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
/**
* 时间工具单元测试<br>
* 此单元测试依赖时区为中国+08:00
*
*
* <pre>
* export TZ=Asia/Shanghai
* </pre>
*
* @author Looly
*
* @author Looly
*/
public class DateUtilTest {
@@ -289,13 +281,13 @@ public class DateUtilTest {
}
@Test
public void parseTest4() throws ParseException {
public void parseTest4() {
String ymd = DateUtil.parse("2019-3-21 12:20:15", "yyyy-MM-dd").toString(DatePattern.PURE_DATE_PATTERN);
Assert.assertEquals("20190321", ymd);
}
@Test
public void parseTest5() throws ParseException {
public void parseTest5() {
// 测试时间解析
String time = DateUtil.parse("22:12:12").toString(DatePattern.NORM_TIME_FORMAT);
Assert.assertEquals("22:12:12", time);
@@ -321,21 +313,32 @@ public class DateUtilTest {
}
@Test
public void parseTest6() throws ParseException {
public void parseTest6() {
String str = "Tue Jun 4 16:25:15 +0800 2019";
DateTime dateTime = DateUtil.parse(str);
Assert.assertEquals("2019-06-04 16:25:15", dateTime.toString());
}
@Test
public void parseTest7() throws ParseException {
public void parseTest7() {
String str = "2019-06-01T19:45:43.000 +0800";
DateTime dateTime = DateUtil.parse(str, "yyyy-MM-dd'T'HH:mm:ss.SSS Z");
Assert.assertEquals("2019-06-01 19:45:43", dateTime.toString());
}
@Test
public void parseDateTest() throws ParseException {
public void parseAndOffsetTest() {
// 检查UTC时间偏移是否准确
String str = "2019-09-17T13:26:17.948Z";
DateTime dateTime = DateUtil.parse(str);
Assert.assertEquals("2019-09-17 13:26:17", dateTime.toString());
DateTime offset = DateUtil.offsetHour(dateTime, 8);
Assert.assertEquals("2019-09-17 21:26:17", offset.toString());
}
@Test
public void parseDateTest() {
String dateStr = "2018-4-10";
Date date = DateUtil.parseDate(dateStr);
String format = DateUtil.format(date, DatePattern.NORM_DATE_PATTERN);
@@ -411,7 +414,7 @@ public class DateUtilTest {
}
@Test
public void parseUTCTest() throws ParseException {
public void parseUTCTest() {
String dateStr1 = "2018-09-13T05:34:31Z";
DateTime dt = DateUtil.parseUTC(dateStr1);
@@ -426,44 +429,44 @@ public class DateUtilTest {
// 使用当前(上海)时区
dateStr = dt.toString(TimeZone.getTimeZone("GMT+8:00"));
Assert.assertEquals("2018-09-13 13:34:31", dateStr);
dateStr1 = "2018-09-13T13:34:32+0800";
dt = DateUtil.parseUTC(dateStr1);
dateStr = dt.toString(TimeZone.getTimeZone("GMT+8:00"));
Assert.assertEquals("2018-09-13 13:34:32", dateStr);
dateStr1 = "2018-09-13T13:34:33+08:00";
dt = DateUtil.parseUTC(dateStr1);
dateStr = dt.toString(TimeZone.getTimeZone("GMT+8:00"));
Assert.assertEquals("2018-09-13 13:34:33", dateStr);
dateStr1 = "2018-09-13T13:34:34+0800";
dt = DateUtil.parse(dateStr1);
dateStr = dt.toString(TimeZone.getTimeZone("GMT+8:00"));
Assert.assertEquals("2018-09-13 13:34:34", dateStr);
dateStr1 = "2018-09-13T13:34:35+08:00";
dt = DateUtil.parse(dateStr1);
dateStr = dt.toString(TimeZone.getTimeZone("GMT+8:00"));
Assert.assertEquals("2018-09-13 13:34:35", dateStr);
dateStr1 = "2018-09-13T13:34:36.999+0800";
dt = DateUtil.parseUTC(dateStr1);
final SimpleDateFormat simpleDateFormat = new SimpleDateFormat(DatePattern.NORM_DATETIME_MS_PATTERN);
simpleDateFormat.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));
dateStr = dt.toString(simpleDateFormat);
Assert.assertEquals("2018-09-13 13:34:36.999", dateStr);
dateStr1 = "2018-09-13T13:34:37.999+08:00";
dt = DateUtil.parseUTC(dateStr1);
dateStr = dt.toString(simpleDateFormat);
Assert.assertEquals("2018-09-13 13:34:37.999", dateStr);
dateStr1 = "2018-09-13T13:34:38.999+0800";
dt = DateUtil.parse(dateStr1);
dateStr = dt.toString(simpleDateFormat);
Assert.assertEquals("2018-09-13 13:34:38.999", dateStr);
dateStr1 = "2018-09-13T13:34:39.999+08:00";
dt = DateUtil.parse(dateStr1);
dateStr = dt.toString(simpleDateFormat);
@@ -471,7 +474,7 @@ public class DateUtilTest {
}
@Test
public void parseJDkTest() throws ParseException {
public void parseJDkTest() {
String dateStr = "Thu May 16 17:57:18 GMT+08:00 2019";
DateTime time = DateUtil.parse(dateStr);
Assert.assertEquals("2019-05-16 17:57:18", time.toString());
@@ -484,7 +487,7 @@ public class DateUtilTest {
DateTime endOfYear = DateUtil.endOfYear(date);
Assert.assertEquals("2019-12-31 23:59:59", endOfYear.toString());
}
@Test
public void endOfWeekTest() {
// 周日
@@ -521,7 +524,7 @@ public class DateUtilTest {
try {
range.next();
Assert.fail("已超过边界,下一个元素不应该存在!");
} catch (NoSuchElementException e) {
} catch (NoSuchElementException ignored) {
}
// 测试多步进的情况
@@ -535,7 +538,7 @@ public class DateUtilTest {
try {
range.next();
Assert.fail("不包含结束时间情况下,下一个元素不应该存在!");
} catch (NoSuchElementException e) {
} catch (NoSuchElementException ignored) {
}
}