mysql date格式化工具类_日期处理工具类

packagecom.codeworld.fc.utils;importorg.hibernate.annotations.common.util.StringHelper;importjava.text.ParseException;importjava.text.SimpleDateFormat;importjava.time.Instant;importjava.time.LocalDateTime;importjava.time.ZoneId;importjava.time.format.DateTimeFormatter;importjava.util.Calendar;importjava.util.Date;/*** 日期工具类

**/

public classDateUtils {/*** 日期格式yyyy-MM-dd*/

public static final String pattern_date = "yyyy-MM-dd";/*** 日期时间格式yyyy-MM-dd HH:mm:ss*/

public static final String pattern_time = "yyyy-MM-dd HH:mm:ss";/*** 描述:日期格式化

*

*@paramdate 日期

*@return输出格式为 yyyy-MM-dd 的字串*/

public staticString formatDate(Date date) {returnformatDate(date, pattern_time);

}/*** 描述:日期格式化

*

*@paramdate 日期

*@parampattern 格式化类型

*@return

*/

public staticString formatDate(Date date, String pattern) {

SimpleDateFormat dateFormat= newSimpleDateFormat(pattern);returndateFormat.format(date);

}/*** 描述:解析日期字串

*

*@paramdateStr 日期字串

*@return按 yyyy-MM-dd HH:mm:ss 格式解析*/

public staticDate parseString(String dateStr) {return parseString(dateStr, "yyyy-MM-dd HH:mm:ss");

}/*** 描述:解析日期字串

*

*@paramdateStr 日期字串

*@parampattern 字串日期格式

*@return对应日期类型数据*/

public staticDate parseString(String dateStr, String pattern) {

SimpleDateFormat dateFormat= newSimpleDateFormat(pattern);try{if (!StringHelper.isEmpty(dateStr)) {returndateFormat.parse(dateStr);

}

}catch(ParseException ex) {

ex.printStackTrace();

System.err.println(dateStr+ "转换成日期失败,可能是不符合格式:" +pattern);

}return null;

}/*** 描述:获取指定日期的中文星期数

*

*@paramdate 指定日期

*@return星期数,如:星期一*/

public staticString getWeekStr(Date date) {

Calendar calendar=Calendar.getInstance();

calendar.setTime(date);int week = calendar.get(7);--week;

String weekStr= "";switch(week) {case 0:

weekStr= "星期日";break;case 1:

weekStr= "星期一";break;case 2:

weekStr= "星期二";break;case 3:

weekStr= "星期三";break;case 4:

weekStr= "星期四";break;case 5:

weekStr= "星期五";break;case 6:

weekStr= "星期六";

}returnweekStr;

}/*** 描述:间隔时间

*

*@paramdate1

*@paramdate2

*@return毫秒数*/

public static longgetDateMiliDispersion(Date date1, Date date2) {if ((date1 == null) || (date2 == null)) {return 0L;

}long time1 =date1.getTime();long time2 =date2.getTime();return time1 -time2;

}/*** 描述:间隔天数

*

*@paramdate1

*@paramdate2

*@return天数*/

public static intgetDateDiff(Date date1, Date date2) {if ((date1 == null) || (date2 == null)) {return 0;

}long time1 =date1.getTime();long time2 =date2.getTime();long diff = time1 -time2;

Long longValue= new Long(diff / 86400000L);returnlongValue.intValue();

}/*** 描述:获取指定日期之前多少天的日期

*

*@paramdate 指定日期

*@paramday 天数

*@return日期*/

public static Date getDataDiff(Date date, intday) {if (date == null) {return null;

}long time =date.getTime();

time-= 86400000L *day;return newDate(time);

}/*** 描述:获取当前周

*

*@return

*/

public static intgetCurrentWeek() {

Calendar calendar=Calendar.getInstance();int week = calendar.get(7);--week;if (week == 0) {

week= 7;

}returnweek;

}/*** 描述:获取中文当前周

*

*@return

*/

public staticString getCurrentWeekStr() {return getWeekStr(newDate());

}/*** 描述:获取本年

*

*@return

*/

public static intgetCurrentYear() {

Calendar calendar=Calendar.getInstance();return calendar.get(1);

}/*** 描述:获取本月

*

*@return

*/

public static intgetCurrentMonth() {

Calendar calendar=Calendar.getInstance();return calendar.get(2) + 1;

}/*** 描述:获取本月的当前日期数

*

*@return

*/

public static intgetCurrentDay() {

Calendar calendar=Calendar.getInstance();return calendar.get(5);

}/*** 描述:当前时间与指定时间的差

*

*@paramstr 秒数

*@return时间差,单位:秒*/

public static intgetUnixTime(String str) {if ((str == null) || ("".equals(str))) {return 0;

}try{long utime = Long.parseLong(str) * 1000L;

Date date1= newDate(utime);

Date date= newDate();long nowtime = (date.getTime() - date1.getTime()) / 1000L;return (int) nowtime;

}catch(Exception e) {

e.printStackTrace();

System.err.println("获取时差失败");

}return 0;

}/*** 描述:去除日期字串中原“-”和“:”

*

*@paramdateTime

*@return

*/

public staticString formatString(String dateTime) {if ((dateTime != null) && (dateTime.length() >= 8)) {

String formatDateTime= dateTime.replaceAll("-", "");

formatDateTime= formatDateTime.replaceAll(":", "");

String date= formatDateTime.substring(0, 8);returndate;

}return "";

}/*** 描述:当前时间与指定时间的差

*

*@paramstr yyyy-MM-dd HH:mm:ss 格式的日期

*@return时间差,单位:秒*/

public static intgetTimesper(String str) {if ((str == null) || ("".equals(str))) {return 0;

}try{

Date date1= newDate(Long.parseLong(str));

Date date= newDate();long nowtime = (date.getTime() - date1.getTime()) / 1000L;return (int) nowtime;

}catch(Exception e) {

e.printStackTrace();

System.err.println("日期转换出错");

}return 0;

}/*** 描述:获取16位日期时间,yyyyMMddHHmmss

*

*@paramdateTime 字串日期

*@return

*/

public staticString formatDateTime(String dateTime) {if ((dateTime != null) && (dateTime.length() >= 8)) {

String formatDateTime= dateTime.replaceAll("-", "");

formatDateTime= formatDateTime.replaceAll(":", "");

String date= formatDateTime.substring(0, 8);

String time= formatDateTime.substring(8).trim();for (int i = time.length(); i < 6; ++i) {

time= time + "0";

}return date +time;

}return "";

}/*** 描述:获取16位日期时间,yyyyMMddHHmmss

*

*@paramdate 日期

*@return

*/

public staticString formatDateTime(Date date) {

String dateTime=formatDate(date);returnformatDateTime(dateTime);

}public staticString formatInstant(Instant instant, String format) {

LocalDateTime localDateTime=LocalDateTime.ofInstant(instant, ZoneId.systemDefault());returnlocalDateTime.format(DateTimeFormatter.ofPattern(format));

}public staticString formatFullTime(LocalDateTime localDateTime, String pattern) {

DateTimeFormatter dateTimeFormatter=DateTimeFormatter.ofPattern(pattern);returnlocalDateTime.format(dateTimeFormatter);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值