当前位置: 首页 > news >正文

工具类篇【三】日期Date转换

-


工具类篇大全


工具类篇【一】String字符串


工具类篇【二】BigDecimal计算


工具类篇【三】日期Date转换


工具类篇【四】日志脱敏


工具类篇【五】Random随机生成字符串


工具类篇【六】克隆对象的2种常用方法

 


前言
日期Date是编程中最常使用的util类之一,毫无疑问日期就是代表时间,数据的创建时间、更新时间、交易时间、记录时间等;是一个尤为重要的字段和属性。通常在前端展示、数据库存储、数据传输和程序中转换,格式也有多种:Date、String、Long;操作也分为时间格式转换、大小(先后)比较。


一、格式转换大全
` public static final String TIME_TEMPLATE = "yyyy-MM-dd HH:mm:ss";
public static final String TIME_TEMPLATE2 = "yyyyMMddHHmmss";

public static final String PERIOD_TEMPLATE = "yyyy-MM";

public static final String DATE_TEMPLATE_Y_M_D = "yyyy-MM-dd";
public static final String DATE_TEMPLATE_YMD = "yyyyMMdd";

/**
* 代替SimpleDateFormat DATETIME_PATTERN 线程不安全方案
*/

protected static final FastDateFormat TIME_FORMAT = FastDateFormat.getInstance(TIME_TEMPLATE);
protected static final FastDateFormat TIME_FORMAT2 = FastDateFormat.getInstance(TIME_TEMPLATE2);
/**
* 代替SimpleDateFormat DATE_PATTERN 线程不安全方案
*/
protected static final FastDateFormat DATE_FORMAT = FastDateFormat.getInstance(DATE_TEMPLATE_Y_M_D);

/**
* 代替SimpleDateFormat DATE_PATTERN 线程不安全方案
*/
protected static final FastDateFormat DATE_FORMAT2 = FastDateFormat.getInstance(DATE_TEMPLATE_YMD);

/**
* 代替SimpleDateFormat PERIOD_PATTERN 线程不安全方案
*/
protected static final FastDateFormat PERIOD_FORMAT = FastDateFormat.getInstance(PERIOD_TEMPLATE);`
Date转成String格式的时间

`
/**
* TITIL 日期转换成字符串 yyyy/MM/dd HH:mm:ss
* @DateTime 2017年6月23日 下午2:50:27
*
* @param date
* @return
*/
public static String convertDateToStringFormatTime(Date date) {

if (date == null) {
return null;
}
return TIME_FORMAT.format(date);
}

/**
* TITIL 日期转换成字符串 yyyymmddhhmiss
* @DateTime 2017年6月23日 下午2:50:27
*
* @param date
* @return
*/
public static String convertDateToStringFormatTime2(Date date) {

if (date == null) {
return null;
}
return TIME_FORMAT2.format(date);
}

/**
* TITIL 日期转换成字符串 yyyy-MM-dd
* @DateTime 2017年6月23日 下午2:50:27
*
* @param date
* @return
*/
public static String convertDateToString(Date date) {

if (date == null) {
return null;
}
return DATE_FORMAT.format(date);
}

/**
* TITIL 日期转换成字符串 yyyyMMdd
* @DateTime 2017年6月23日 下午2:50:27
*
* @param date
* @return
*/
public static String convertDateToString2(Date date) {

if (date == null) {
return null;
}
return DATE_FORMAT2.format(date);
}

/**
* @author
* @DateTime 2018年7月16日 下午8:47:34
*
* @param date
* @return
*/
public static String convertDateToPeriod(Date date) {
if (date == null) {
return null;
}
return PERIOD_FORMAT.format(date);
}
`

- String转成Date格式的时间

`
/**
* titil : 字符串(YYYY/MM/DD)转日期
* @DateTime 2018年1月18日 下午3:23:00
*
* @param strDate
* @return
* @throws ParseException
*/
public static Date convertStringToDate(String strDate) {

try {
return TIME_FORMAT.parse(strDate);
} catch (ParseException pe) {
throw new JobRuntimeException(pe.getMessage(), pe);
}
}

public static Date convertStringToDate2(String strDate) {

try {
return TIME_FORMAT2.parse(strDate);
} catch (ParseException pe) {
throw new JobRuntimeException(pe.getMessage(), pe);
}
}

/**
* titil : 字符串(YYYY/MM/DD)转日期
* @author
* @DateTime 2018年1月18日 下午3:23:00
*
* @param strDate
* @return
* @throws ParseException
*/
public static Date convertStringDateToDate(String strDate) {

try {
return DATE_FORMAT2.parse(strDate);
} catch (ParseException pe) {
throw new JobRuntimeException(pe.getMessage(), pe);
}
}

public static Date convertStringToDateFormatYmd(String strDate) {

try {
return DATE_FORMAT.parse(strDate);
} catch (ParseException pe) {
throw new JobRuntimeException(pe.getMessage(), pe);
}
}
`

- Long转成Date格式的时间

`
public static Date convertSecondLongToDate(Long secondLong) {
Date date = null;
if (secondLong == null || secondLong.longValue() == 0) {
return date;
}
date = new Date(secondLong * 1000);
return date;
}
`

- Date转成Long格式时间

`
public static Long convertSecondLongToDate(Date date) {
Long long = null;
if (date == null) {
return null;
}
long = date.getTime();
return long;
}
`

二、SimpleDateFormat 包

`
/**
* 日期型转换为YYYY-MM-DD类型
* @DateTime 2017年8月11日 下午2:07:41
*
* @param glDate
* @return
* @throws ParseException
* formatDateToDay
*/
public static Date convertByDate(Date glDate) throws ParseException {

SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
String s = simpleDateFormat.format(glDate);
Date date = simpleDateFormat.parse(s);
return date;

}
/**
* 日期型转换为YYYY-MM-DD类型
* @DateTime 2017年8月11日 下午2:07:41
*
* @param glDate
* @return
* @throws ParseException
* formatDateToDay
*/
public static Date convertByDateTime(Date glDate) throws ParseException {

SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String s = simpleDateFormat.format(glDate);
Date date = simpleDateFormat.parse(s);
return date;

}

/**
* 查询期间的最后一天
* @DateTime 2017年8月16日 下午2:15:53
*
* @param periodName
* @return
* @throws ParseException
* getLastDayOfPeriod
*/
public static Date getPeriodLastDay(String periodName) throws ParseException {
Calendar calendar = Calendar.getInstance();
DateFormat format1 = new SimpleDateFormat("yyyy-MM");
calendar.setTime(format1.parse(periodName));
calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
calendar.set(Calendar.HOUR_OF_DAY, calendar.getActualMaximum(Calendar.HOUR_OF_DAY));
calendar.set(Calendar.MINUTE, calendar.getActualMaximum(Calendar.MINUTE));
calendar.set(Calendar.SECOND, calendar.getActualMaximum(Calendar.SECOND));
calendar.set(Calendar.MILLISECOND, calendar.getActualMaximum(Calendar.MILLISECOND));
return calendar.getTime();
}

`

三、Calendar 包

`
/**
* 查询期间的最后一天
* @DateTime 2017年8月16日 下午2:15:53
*
* @param periodName
* @return
* @throws ParseException
* getLastDayOfPeriod
*/
public static Date getPeriodLastDay(String periodName) throws ParseException {
Calendar calendar = Calendar.getInstance();
DateFormat format1 = new SimpleDateFormat("yyyy-MM");
calendar.setTime(format1.parse(periodName));
calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
calendar.set(Calendar.HOUR_OF_DAY, calendar.getActualMaximum(Calendar.HOUR_OF_DAY));
calendar.set(Calendar.MINUTE, calendar.getActualMaximum(Calendar.MINUTE));
calendar.set(Calendar.SECOND, calendar.getActualMaximum(Calendar.SECOND));
calendar.set(Calendar.MILLISECOND, calendar.getActualMaximum(Calendar.MILLISECOND));
return calendar.getTime();
}

/**
* 查询期间的第一天
* @DateTime 2017年8月16日 下午2:15:53
*
* @param periodName
* @return
* @throws ParseException
*/
public static Date getPeriodFirstDay(String periodName) throws ParseException {
Calendar calendar = Calendar.getInstance();
DateFormat format1 = new SimpleDateFormat("yyyy-MM");
calendar.setTime(format1.parse(periodName));

return calendar.getTime();
}

/**获取具体时间
* @DateTime 2017年8月11日 下午4:47:16
*
* @param glDate
* @param dayNum
* @return
*/
public static Date addDayByDate(Date glDate, int dayNum) {
Calendar cl = Calendar.getInstance();
cl.setTime(glDate);
cl.add(Calendar.DATE, dayNum);
Date date = cl.getTime();
return date;
}

/**获取具体时间
* @DateTime 2017年9月19日 下午5:12:57
*
* @param date
* @return
*/
public static Date getUpperLastDay(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.set(Calendar.DAY_OF_MONTH, 1);
calendar.add(Calendar.DAY_OF_MONTH, -1);
return calendar.getTime();

}
`

四、时间大小比较

`
/**
* @author
* @DateTime 2018年2月8日 下午8:57:16
*
* @param d1
* @param d2
* @return
*/
public static int compareDate(Date d1, Date d2) {

try {
if (d1 == null && d2 == null) {
return 0;
} else if (d1.getTime() > d2.getTime()) {
return 1;
} else if (d1.getTime() < d2.getTime()) {
return -1;
} else {
return 0;
}
} catch (Exception exception) {
Log.error(exception.getMessage());
throw new RuntimeException(exception.getMessage(), exception);
}
}
`

既然都看完了整篇文章,相信对你一定有所帮助。原创不易,勿做伸手党。


点击下方【打赏】小编,或者关注公众号给予支持,你们的每一份鼓励都将是小编伟大的动力。



![](https://i-blog.csdnimg.cn/blog_migrate/c6588a897387ffefcdda1428dcd8e7dd.jpeg)

同名原创公众号:   
程序大视界

http://www.rkmt.cn/news/1433756.html

相关文章:

  • 2026 成都奢品回收图鉴,多维度测评,解锁包包变现新思路 - 奢侈品回收测评
  • 2026年苏州婚纱照拍摄全攻略:风格趋势与实力机构推荐 - 资讯快报
  • AI可解释性、责任与问责:构建可信赖人工智能治理框架
  • 揭开黑盒:理解大模型内部运行逻辑对 QA 发现边界缺陷的帮助
  • idea快速创建SpringCloud项目
  • 天赐范式第59天:“控制不动点“vs“数值僵尸“——当流场被钉在临界状态,是死了还是被控住了?
  • EldenRingSaveCopier:拯救你的《艾尔登法环》游戏进度的终极指南
  • Windows 11安装绕过工具终极指南:让老旧电脑也能流畅升级
  • 济南倍乐管家:莱芜专业的深度清洁软装地毯公司选哪家 - LYL仔仔
  • 7步精通思源宋体TTF:开源中文字体终极解决方案
  • Redis安装部署
  • 源码分析【三】ArrayList与LinkedList的比较
  • TVA在传统安防迈向智能物联(AIoT)中的突破与应用(2)
  • LibreDWG完全指南:5个关键优势解决DWG文件处理难题
  • 告别低效写作:盘点2026年实力封神的的降AI率平台 - 降AI小能手
  • 《中间件》——kafka的工作原理解析
  • 终极魔兽争霸III游戏优化工具:简单三步提升你的游戏体验
  • 2026 年 5 个最佳 Agent Skills 平台推荐
  • 3个关键词让你的小爱音箱智能下载歌曲:Xiaomusic语音指令实用指南
  • 手把手教你用VMware安装华为EulerOS 2.0 SP5(附详细分区与开发环境配置)
  • Qt版本管理实战:从5.12.3平滑降级到5.9.8,并让VS2022同时识别多个Qt版本
  • D3KeyHelper终极指南:5分钟掌握暗黑3自动化战斗技能宏工具
  • 别等硬盘挂了才后悔!保姆级教程:用smartctl给你的Linux服务器硬盘做个全面体检(附关键指标解读)
  • Debian11最小化安装后,浏览器中文乱码?5分钟搞定中文字体配置(附常用字体包清单)
  • 3大技术突破:douyin-downloader如何实现批量无水印视频的智能获取?
  • TCSVT期刊投稿实战:如何用LaTeX高效排版并处理图表与多媒体文件
  • LinkSwift:九大网盘直链解析工具,告别下载等待的终极解决方案
  • 统信UOS任务栏高效模式 vs 时尚模式,哪个更适合你的工作流?
  • 避坑指南:用铅画纸打印骰子教具,图案模糊、嵌套失败的3个关键原因与解决方案
  • 2026CRM软件大盘点:三梯队10款主流产品解析 - Joyky