博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
日期时间工具类
阅读量:4641 次
发布时间:2019-06-09

本文共 6587 字,大约阅读时间需要 21 分钟。

import java.sql.Timestamp;import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.Date;import java.util.HashMap;import java.util.Map;import org.apache.commons.lang3.time.DateFormatUtils;import org.slf4j.Logger;import org.slf4j.LoggerFactory;/** * 日期时间工具类 */public class DateUtils extends org.apache.commons.lang.time.DateUtils{        private final static Logger logger = LoggerFactory.getLogger(DateUtils.class);        /** 日期格式 */    public static final String DATE_FORMAT = "yyyy-MM-dd";        /** 年月格式 */    public static final String DATE_FORMAT_MONTH = "yyyy-MM";        /** 时间格式 */    public static final String TIME_FORMAT = "HH:mm:ss";        /** 日期时间格式 */    public static final String DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";        /** 时间戳格式 */    public static final String TIMESTAMP_FORMAT = "yyyy-MM-dd HH:mm:ss.S";        private static String[] parsePatterns = { "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm",             "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm" };        /** 日期格式化缓冲器 */    private static Map
date_format_cache = new HashMap
(); /** * 功能描述: 将日期对象格式化为指定格式。 * @param date 日期 * @param dateFormat 格式化字符串 * @return 已格式化的时期 */ public static String date2String(final Date date,final String dateFormat) { SimpleDateFormat format = date_format_cache.get(dateFormat); if(null == format){ format = new SimpleDateFormat(dateFormat); date_format_cache.put(dateFormat, format); } return format.format(date); } /** * 功能描述: 将日期字符串解析为java.util.Date * @param dateString 日期字符串 * @param dateFormat 格式化字符串 * @return 日期对象 java.util.Date * @throws ParseException 日期解析异常 */ public static Date string2Date(final String dateString,final String dateFormat){ try { SimpleDateFormat format = date_format_cache.get(dateFormat); if(null == format){ format = new SimpleDateFormat(dateFormat); date_format_cache.put(dateFormat, format); } return format.parse(dateString); } catch (ParseException e) { logger.error("转化日期函数失败,dataFormat={}",dateFormat,e); throw new Exception(e); } } /** * 功能描述: 得到当前日期字符串 格式(yyyy-MM-dd) * */ public static String getDate() { return getDate("yyyy-MM-dd"); } /** * 功能描述: 得到当前日期字符串 格式(yyyy-MM-dd) pattern可以为:"yyyy-MM-dd" "HH:mm:ss" "E" * */ public static String getDate(String pattern) { return DateFormatUtils.format(new Date(), pattern); } /** * 功能描述: 得到日期字符串 默认格式(yyyy-MM-dd) pattern可以为:"yyyy-MM-dd" "HH:mm:ss" "E" * */ public static String formatDate(Date date, Object... pattern) { String formatDate = null; if (pattern != null && pattern.length > 0) { formatDate = DateFormatUtils.format(date, pattern[0].toString()); } else { formatDate = DateFormatUtils.format(date, "yyyy-MM-dd"); } return formatDate; } /** * 功能描述: 得到日期时间字符串,转换格式(yyyy-MM-dd HH:mm:ss) * */ public static String formatDateTime(Date date) { return formatDate(date, "yyyy-MM-dd HH:mm:ss"); } /** * 功能描述: 得到当前时间字符串 格式(HH:mm:ss) * */ public static String getTime() { return formatDate(new Date(), "HH:mm:ss"); } /** * 功能描述: 得到当前日期和时间字符串 格式(yyyy-MM-dd HH:mm:ss) * */ public static String getDateTime() { return formatDate(new Date(), "yyyy-MM-dd HH:mm:ss"); } /** * 功能描述: 得到年 * */ public static String getYear() { return formatDate(new Date(), "yyyy"); } /** * 功能描述: 得到当前月份字符串 格式(MM) * */ public static String getMonth() { return formatDate(new Date(), "MM"); } /** * 功能描述: 得到当天字符串 格式(dd) * */ public static String getDay() { return formatDate(new Date(), "dd"); } /** * 功能描述: 得到当前星期字符串 格式(E)星期几 * */ public static String getWeek() { return formatDate(new Date(), "E"); } /** * 日期型字符串转化为日期 格式 * { "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", * "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm" } */ public static Date parseDate(Object str) { if (str == null){ return null; } try { return parseDate(str.toString(), parsePatterns); } catch (ParseException e) { return null; } } /** * 功能描述: 获取过去的天数 * */ public static long pastDays(Date date) { long t = new Date().getTime()-date.getTime(); return t/(24*60*60*1000); } public static Date getDateStart(Date date) { if(date==null) { return null; } SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); try { date= sdf.parse(formatDate(date, "yyyy-MM-dd")+" 00:00:00"); } catch (ParseException e) { e.printStackTrace(); } return date; } public static Date getDateEnd(Date date) { if(date==null) { return null; } SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); try { date= sdf.parse(formatDate(date, "yyyy-MM-dd") +" 23:59:59"); } catch (ParseException e) { e.printStackTrace(); } return date; } /** * 功能描述: 判断字符串是否是日期 * */ public static boolean isDate(String timeString){ SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd"); format.setLenient(false); try{ format.parse(timeString); }catch(Exception e){ return false; } return true; } /** * 功能描述: 格式化时间 * */ public static String dateFormat(Date timestamp){ SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); return format.format(timestamp); } /** * 功能描述: 获取系统时间Timestamp * */ public static Timestamp getSysTimestamp(){ return new Timestamp(new Date().getTime()); } /** * 功能描述: 获取系统时间Date * */ public static Date getSysDate(){ return new Date(); } /** * 功能描述: 生成时间随机数 * */ public static String getDateRandom(){ String s=new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date()); return s; }}
View Code

 

转载于:https://www.cnblogs.com/wang-yi/p/9816762.html

你可能感兴趣的文章
如何在我们项目中利用开源的图表(js chart)
查看>>
nfs服务器工作原理
查看>>
C3P0连接池工具类使用
查看>>
SVN常用命令备注
查看>>
孩子教育
查看>>
解决Cacti监控图像断断续续问题
查看>>
结构体的传参理解成员的存储方式
查看>>
python 进程与线程(理论部分)
查看>>
什么是API
查看>>
Java反射中method.isBridge() 桥接方法
查看>>
[shiro学习笔记]第二节 shiro与web融合实现一个简单的授权认证
查看>>
强名称程序集(strong name assembly)——为程序集赋予强名称
查看>>
1028. List Sorting (25)
查看>>
BZOJ 1613: [Usaco2007 Jan]Running贝茜的晨练计划
查看>>
ubuntu 重启命令,ubuntu 重启网卡方法
查看>>
Linux的学习:
查看>>
JavaScript中的原型继承原理
查看>>
Python logger模块
查看>>
jquery控制css的display(控制元素的显示与隐藏)
查看>>
关于python做人工智能的一个网页(很牛逼)
查看>>