package com.puboot.common.util;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.StrUtil;
import lombok.experimental.UtilityClass;
import lombok.extern.slf4j.Slf4j;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;
/**
* 日期工具类
*
* @author Linzhaoguan
* @version V1.0
* @date 2019年9月11日
*/
@Slf4j
@UtilityClass
public class DateUtil {
public static final long ONE_DAY_SECONDS = 86400;
public static final String SHORT_FORMAT = "yyyyMMdd";
public static final String LONG_FORMAT = "yyyyMMddHHmmss";
public static final String concurrentFormat = "yyyyMMddHHmmssSSS";
public static final String shortConcurrentFormat = "yyMMddHHmmssSSS";
public static final String webFormat = "yyyy-MM-dd";
public static final String webMonthFormat = "yyyy-MM";
public static final String timeFormat = "HH:mm:ss";
public static final String monthFormat = "yyyyMM";
public static final String chineseDtFormat = "yyyy年MM月dd日";
public static final String chineseYMFormat = "yyyy年MM月";
public static final String newFormat = "yyyy-MM-dd HH:mm:ss";
public static final String noSecondFormat = "yyyy-MM-dd HH:mm";
public static final String MdFormat = "MM-dd";
public static final long ONE_DAY_MILL_SECONDS = 86400000;
public static DateFormat getNewDateFormat(String pattern) {
DateFormat df = new SimpleDateFormat(pattern);
df.setLenient(false);
return df;
}
public static String format(Date date, String format) {
return new SimpleDateFormat(format).format(date);
}
public static String format(String dateStr, String oldFormat, String newFormat) throws ParseException {
String result = null;
DateFormat oldDateFormat = new SimpleDateFormat(oldFormat);
DateFormat newDateFormat = new SimpleDateFormat(newFormat);
Date date = oldDateFormat.parse(dateStr);
result = newDateFormat.format(date);
return result;
}
public static Date parseDateNoTime(String sDate) throws ParseException {
DateFormat dateFormat = new SimpleDateFormat(SHORT_FORMAT);
return dateFormat.parse(sDate);
}
public static Date parseDateNoTime(String sDate, String format) throws ParseException {
if (StrUtil.isBlank(format)) {
throw new ParseException("Null format. ", 0);
}
DateFormat dateFormat = new SimpleDateFormat(format);
return dateFormat.parse(sDate);
}
public static Date parseDateNoTimeWithDelimit(String sDate, String delimit) throws ParseException {
sDate = sDate.replaceAll(delimit, "");
DateFormat dateFormat = new SimpleDateFormat(SHORT_FORMAT);
return dateFormat.parse(sDate);
}
public static Date parseDateLongFormat(String sDate) throws ParseException {
DateFormat dateFormat = new SimpleDateFormat(LONG_FORMAT);
return dateFormat.parse(sDate);
}
public static Date parseDateNewFormat(String sDate) throws ParseException {
DateFormat dateFormat = new SimpleDateFormat(newFormat);
dateFormat.setLenient(false);
return dateFormat.parse(sDate);
}
public static Date parseDateNoSecondFormat(String sDate) throws ParseException {
DateFormat dateFormat = new SimpleDateFormat(noSecondFormat);
dateFormat.setLenient(false);
return dateFormat.parse(sDate);
}
public static Date parseDateWebFormat(String sDate) throws ParseException {
DateFormat dateFormat = new SimpleDateFormat(webFormat);
dateFormat.setLenient(false);
return dateFormat.parse(sDate);
}
public static Date parseDateWebMonthFormat(String sDate) throws ParseException {
DateFormat dateFormat = new SimpleDateFormat(webMonthFormat);
dateFormat.setLenient(false);
return dateFormat.parse(sDate);
}
/**
* 计算当前时间几小时之后的时间
*
* @param date
* @param hours
* @return
*/
public static Date addHours(Date date, long hours) {
return addMinutes(date, hours * 60);
}
/**
* 计算当前时间几分钟之后的时间
*
* @param date
* @param minutes
* @return
*/
public static Date addMinutes(Date date, long minutes) {
return addSeconds(date, minutes * 60);
}
/**
* @param date1
* @param secs
* @return
*/
public static Date addSeconds(Date date1, long secs) {
return new Date(date1.getTime() + secs * 1000);
}
/**
* 判断输入的字符串是否为合法的小时
*
* @param hourStr
* @return true/false
*/
public static boolean isValidHour(String hourStr) {
if (NumberUtil.isNumber(hourStr)) {
int hour = Integer.parseInt(hourStr);
return hour >= 0 && hour <= 23;
}
return false;
}
/**
* 判断输入的字符串是否为合法的分或秒
*
* @param str
* @return true/false
*/
public static boolean isValidMinuteOrSecond(String str) {
if (NumberUtil.isNumber(str)) {
int hour = Integer.parseInt(str);
return hour >= 0 && hour <= 59;
}
return false;
}
/**
* 取得新的日期
*
* @param date1 日期
* @param days 天数
* @return 新的日期
*/
public static Date addDays(Date date1, long days) {
Calendar cal = Calendar.getInstance();
cal.setTime(date1);
cal.add(Calendar.DATE, (int) days);
return cal.getTime();
}
public static String getTomorrowDateString(String sDate) throws ParseException {
Date aDate = parseDateNoTime(sDate);
aDate = addSeconds(aDate, ONE_DAY_SECONDS);
return getDateString(aDate);
}
public static String getTomorrowDateNewFMTString(String sDate) throws ParseException {
Date aDate = parseDateWebFormat(sDate);
aDate = addDays(aDate, 1);
return getWebDateString(aDate);
}
public static String getTomorrowDateNewFormatString(String sDate) throws ParseException {
Date aDate = parseDateNewFormat(sDate);
aDate = addDays(aDate, 1);
return getWebDateString(aDate);
}
public static String getLongDateString(Date date) {
DateFormat dateFormat = new SimpleDateFormat(LONG_FORMAT);
return getDateString(date, dateFormat);
}
public static String getNewFormatDateString(Date date) {
DateFormat dateFormat = new SimpleDateFormat(newFormat);
return getDateString(date, dateFormat);
}
public static String getWebFormatDateString(Date date) {
DateFormat dateFormat = new SimpleDateFormat(webFormat);
return getDateString(date, dateFormat);
}
public static String getConcurrentFormatDateString(Date date) {
DateFormat dateFormat = new SimpleDateFormat(concurrentFormat);
return getDateString(date, dateFormat);
}
public static String getDateString(Date date, DateFormat dateFormat) {
if (date == null || dateFormat == null) {
return null;
}
return dateFormat.format(date);
}
public static String getYesterDayDateString(String sDate) throws ParseException {
Date aDate = parseDateNoTime(sDate);
aDate = addSeconds(aDate, -ONE_DAY_SECONDS);
return getDateString(aDate);
}
/**
* @return 当天的时间格式化为"yyyyMMdd"
*/
public static String getDateString(Date date) {
DateFormat df = getNewDateFormat(SHORT_FORMAT);
return df.format(date);
}
public static String getWebDateString(Date date) {
DateFormat dateFormat = getNewDateFormat(webFormat);
return getDat
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论





























收起资源包目录





































































































共 2000 条
- 1
- 2
- 3
- 4
- 5
- 6
- 20
资源评论


lly202406
- 粉丝: 4234
上传资源 快速赚钱
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助


最新资源
- 基于物联网技术的垃圾桶智能管理系统设计与实现.doc
- 全国自考C加加程序设计试题.doc
- 计算机教育中计算机科学技术的运用探讨.docx
- (源码)基于Arduino的ITS150遥控器模拟器.zip
- 电子商务教研计划.doc
- 江西省中小学安全知识网络答题活动答案解析.doc
- Web前端技术课程实训分析报告.doc
- 电子商务网站盈利能力的理性分析.doc
- 移动互联网环境下混合式教学设计与实践.docx
- 教育系统安全大检查市级督查巡查工作记录单.docx
- 计算机网络安全技术实验四.doc
- AVR单片机的通信系统设计方案.doc
- 略谈工程项目管理中材料成本控制的难点及对策.docx
- 个人网络信息安全防范.doc
- 基于大数据时代下档案管理工作存在的问题与对策研究.docx
- (源码)基于Arduino的MPU9250陀螺仪运动处理单元俯仰角控制项目.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈



安全验证
文档复制为VIP权益,开通VIP直接复制
