当前位置: 首页 > 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.jsqmd.com/news/922431/

相关文章:

  • 抖音批量下载神器:5分钟掌握无水印视频高效下载
  • DRG存档编辑器:三步快速解锁《深岩银河》全职业高级体验
  • 2026年衡阳市CPPM报名十大核心问题全流程答疑 - 众智商学院课程中心
  • DLSS Swapper深度解析:5分钟实现游戏DLSS智能管理的一站式解决方案
  • 2026年苏州婚纱照拍摄全攻略:风格趋势与实力机构推荐 - 资讯快报
  • AI可解释性、责任与问责:构建可信赖人工智能治理框架
  • 重庆顺坤机械科技:九龙坡专业的螺纹生产厂家找哪家 - LYL仔仔
  • 09.前后端合并部署
  • 工具类篇【四】日志脱敏
  • TVA在传统安防迈向智能物联(AIoT)中的突破与应用(5)
  • SpringCloud--Config Server配置中心学习总结
  • 揭开黑盒:理解大模型内部运行逻辑对 QA 发现边界缺陷的帮助
  • 学术文献自动化管理革命:Zotero SciPDF插件深度解析
  • 昨日的欺骗的内容入口:听众为什么会搜索它
  • 聊聊我在第三方支付公司的经历
  • 从失忆到记住一切:Spring AI AutoMemoryTools 与 Session API 实战
  • 9大排序算法,你了解多少?
  • FeignClient注解及参数问题
  • 天赐范式第59天:“控制不动点“vs“数值僵尸“——当流场被钉在临界状态,是死了还是被控住了?
  • idea快速创建SpringCloud项目
  • EldenRingSaveCopier:拯救你的《艾尔登法环》游戏进度的终极指南
  • VUE跨页面传值的精妙
  • 网络技术12-FTP协议详解——传统文件传输的“老派方案“
  • FUXA管道动画制作:从静态流程图到动态工业监控的转变
  • Windows 11安装绕过工具终极指南:让老旧电脑也能流畅升级
  • 抽象之美——万物皆可设计
  • 济南倍乐管家:莱芜专业的深度清洁软装地毯公司选哪家 - LYL仔仔
  • MTK刷机工具终极指南:3步解锁联发科设备救砖与系统修复
  • 2026年宜昌市CPPM报名十大核心问题全流程答疑 - 众智商学院课程中心
  • 别再死记硬背公式了!用Python+PyTorch图解马尔可夫随机场(MRF)在图像去噪中的应用