From 03108d249989acc6d23ce26f7a93e621a4faa967 Mon Sep 17 00:00:00 2001 From: qi_liang Date: Wed, 7 Aug 2019 07:39:55 +0800 Subject: [PATCH] =?UTF-8?q?1.=E4=BF=AE=E6=94=B9=EF=BC=8C=E6=97=A5=E6=9C=9F?= =?UTF-8?q?=E8=BD=AC=E6=97=A5=E6=9C=9F=E5=AD=97=E7=AC=A6=E4=B8=B2=EF=BC=8C?= =?UTF-8?q?=E5=BD=93=E6=97=A5=E6=9C=9F=E4=B8=BAnull=EF=BC=8C=E4=B8=8D?= =?UTF-8?q?=E6=8A=9B=E5=BC=82=E5=B8=B8=EF=BC=8C=E8=BF=94=E5=9B=9Enull?= =?UTF-8?q?=E3=80=82=202.=E8=A1=A5=E5=85=85=E9=83=A8=E5=88=86=E6=96=B9?= =?UTF-8?q?=E6=B3=95=E6=B3=A8=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/com/lq/code/util/DateUtil.java | 54 +++++++++++++++----- 1 file changed, 42 insertions(+), 12 deletions(-) diff --git a/src/main/java/com/lq/code/util/DateUtil.java b/src/main/java/com/lq/code/util/DateUtil.java index a01dd7c..1dc3e6b 100755 --- a/src/main/java/com/lq/code/util/DateUtil.java +++ b/src/main/java/com/lq/code/util/DateUtil.java @@ -7,29 +7,30 @@ import java.text.SimpleDateFormat; import java.util.Date; /** - * Created by qi_liang on 2018/1/26. + * @author qi + * 日期辅佐工具类 */ public class DateUtil { + /** + * 默认转换日期格式 + */ public static final String DEFAULT_PATTERN="yyyy-MM-dd HH:mm:ss"; /** - * 返回日期 的字符串 默认格式:yyy-MM-dd HH:mm:ss + * 返回日期 的字符串 默认格式:yyyy-MM-dd HH:mm:ss * @param date * @return */ public static String getDateToStr(Date date){ - if (date==null){ - throw new NullPointerException("========date 对象为空null======"); - } return getDateToStr(date,DEFAULT_PATTERN); - } + } /** * 返回日期的字符串 * @param date 日期 - * @param pattern 格式 default:yyy-MM-dd HH:mm:ss + * @param pattern 格式 default:yyyy-MM-dd HH:mm:ss * @return */ public static String getDateToStr(Date date,String pattern){ @@ -37,15 +38,33 @@ public class DateUtil { if (StringUtil.isNull(pattern)){ pattern = DEFAULT_PATTERN; } - SimpleDateFormat sdf=new SimpleDateFormat(pattern); - if (date == null){ - throw new NullPointerException("====== date 对象为空 null======"); + if (date != null){ + SimpleDateFormat sdf=new SimpleDateFormat(pattern); + str=sdf.format(date); } - str=sdf.format(date); + return str; } + /** + * 日期字符串(String)转换日期对象(Date) + * @param strDate 日期字符串(默认格式为:yyyy-MM-dd HH:mm:ss) + * @return 日期对象 + * @throws ParseException + */ + public static Date strToDate(String strDate) throws ParseException { + + return strToDate(strDate,DEFAULT_PATTERN); + } + + /** + * 日期字符串(String)转换日期对象(Date) + * @param strDate 日期字符串 + * @param pattern 日期格式 (default:yyyy-MM-dd HH:mm:ss) + * @return 日期对象 + * @throws ParseException + */ public static Date strToDate(String strDate,String pattern) throws ParseException { if (StringUtil.isNull(pattern)){ pattern = DEFAULT_PATTERN; @@ -56,11 +75,22 @@ public class DateUtil { return sdf.parse(strDate); } - public static int disparityDay(Date startDate,Date endDate){ + /** + * 计算出两个date对象相差多少日 + * @param startDate 开始日期 + * @param endDate 结束日期 + * @return 相差日(int) + */ + public static int disparityDay(Date startDate,Date endDate){ int day = (int)(endDate.getTime()-startDate.getTime())/(24*60*60*1000); return day; } + /** + * 毫秒数转日期字符串 + * @param timeLong 毫秒数 + * @return 日期字符串 + */ public static String dataFormat(Long timeLong){ Long secondLong = timeLong/1000; Long millisecond = timeLong%1000;