/**
* Copyright © 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
*/
package com.hljt.bys.utils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.util.StringUtils;
import org.springframework.util.unit.DataUnit;
import java.math.BigInteger;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;
/**
* 日期工具类, 继承org.apache.commons.lang.time.DateUtils类
* @author ThinkGem
* @version 2014-4-15
*/
public class DateUtils extends org.apache.commons.lang3.time.DateUtils {
private static String[] parsePatterns = {
"yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy-MM",
"yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm", "yyyy/MM",
"yyyy.MM.dd", "yyyy.MM.dd HH:mm:ss", "yyyy.MM.dd HH:mm", "yyyy.MM", "dd-HH", "HH:mm"};
/**
* 得到当前日期字符串 格式(yyyy-MM-dd)
*/
public static String getDate() {
return getDate("yyyy-MM-dd");
}
/**
* 得到当前日期字符串 格式(yyyyMMdd)
*/
public static String getNewDate(){
return getDate("yyyyMMdd");
}
/**
* 得到当前日期字符串 格式(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");
//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");
}
/**
* 得到当前年份字符串 格式(yyyy)
*/
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",
* "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;
}
}
/**
* 获取过去的天数
*
* @param date
* @return
*/
public static long pastDays(Date date) {
long t = new Date().getTime() - date.getTime();
return t / (24 * 60 * 60 * 1000);
}
/**
* 获取过去的小时
*
* @param date
* @return
*/
public static long pastHour(Date date) {
long t = new Date().getTime() - date.getTime();
return t / (60 * 60 * 1000);
}
/**
* 获取过去的分钟
*
* @param date
* @return
*/
public static long pastMinutes(Date date) {
long t = new Date().getTime() - date.getTime();
return t / (60 * 1000);
}
/**
* 转换为时间(天,时:分:秒.毫秒)
*
* @param timeMillis
* @return
*/
public static String formatDateTime(long timeMillis) {
long day = timeMillis / (24 * 60 * 60 * 1000);
long hour = (timeMillis / (60 * 60 * 1000) - day * 24);
long min = ((timeMillis / (60 * 1000)) - day * 24 * 60 - hour * 60);
long s = (timeMillis / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);
long sss = (timeMillis - day * 24 * 60 * 60 * 1000 - hour * 60 * 60 * 1000 - min * 60 * 1000 - s * 1000);
return (day > 0 ? day + "," : "") + hour + ":" + min + ":" + s + "." + sss;
}
/**
* 获取两个日期之间的天数
*
* @param before
* @param after
* @return
*/
public static double getDistanceOfTwoDate(Date before, Date after) {
long beforeTime = before.getTime();
long afterTime = after.getTime();
return (afterTime - beforeTime) / (1000 * 60 * 60 * 24);
}
/**
* @param
* @throws ParseException
*/
public static String getDateUtc(String date) {
date = date.replace("Z", " UTC");
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd‘T‘HH:mm:ss.SSS Z");
Date dates = null;
try {
dates = format.parse(date);
} catch (ParseException e) {
}
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String forString= simpleDateFormat.format(dates);
return forString;
}
public static Date getDateUtZon(String date) {
date = date.replace("Z", " UTC");
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd‘T‘HH:mm:ss.SSS Z");
Date dates = null;
try {
dates = format.parse(date);
} catch (ParseException e) {
}
return dates;
}
/*public static String getDateUts(Date date) {
System.out.println(date);
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd‘T‘HH:mm:ss.SSS UTC");
String format = simpleDateFormat.format(date);
System.out.println(format);
return format;
}*/
public static String getZoon(Date date){
SimpleDateFormat myFmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
TimeZone timeZoneChina = TimeZone.getTimeZone("Asia/Shanghai");//获取中国的时区
myFmt.setTimeZone(timeZoneChina);//设置系统时区
return myFmt.format(date);
}
public static Date getDateString (Date date){
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String format= simpleDateFormat.format(date);
SimpleDateFormat simpleDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date parse = null;
try {
parse = simpleDate.parse(format);
} catch (ParseException e) {
e.printStackTrace();
}
return parse;
}
/**
* 将时间戳装换成时间
* @param s
* @return
*/
public static Date stampToDate(String s){
//String res;
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
long lt = new Long(s);
Date date = new Date(lt);
//res = simpleDateFormat.format(date);
return date;
}
/**
* 将时间戳装换成字符串
* @param s
* @return
*/
public static String stampDateString(String s){
String res;
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
long lt = new Long(s);
Date date = new Date(lt);
res = simpleDateFormat.format(date);
return res;
}
/**
* 将时间戳转换成字符串后多加24个小时减一秒
* @param time
* @return
*/
public static String formatTimeEight(String time) {
Date date = null;
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
long lt = new Long(time);
Date sd = new Date(lt);
long newTime = (sd.getTime() + 24 * 60 * 60 * 1000-1);
String format = simpleDateFormat.format(newTime);
return format;
}
/**
* 将时间转换成时间戳(字符串)
* @return
*/
public static String DateToString(){
Date date = new Date();
long time = date.getTime();
String lt = String.valueOf(time);
return String.valueOf(lt);
}
/**
* 将字符串时间转字符串时间
* @param time
* @return
*/
public static String subString(String time){
String newTime="";
if (!StringUtils.isEmpty(time)){
String reg = "(\\d{4})(\\d{2})(\\d{2})(\\d{2})(\\d{2})(\\d{2})";
newTime = time.replaceAll(reg, "$1-$2-$3 $4:$5:$6");
}
return newTime;
}
/**
* 将时间戳转成没有时分秒的时间字符串
* @param s
* @return
*/
public static String StrDateToStr(String s){
String res ="";
if (!StringUtils.isEmpty(s)){
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
long lt = new Long(s);
Date date = new Date(lt);
res = simpleDateFormat.format(date);
}
return res;
}
/**
* 获取两个日期之间的小时数
*
* @param beforeTime
* @param afterTime
* @return
*/
public static double getHoursOfTwoDate( long beforeTime, long afterTime ) {
/* long beforeTime = before.getTime();
long afterTime = after.getTime();*/
return (afterTime - beforeTime) / (1000 * 60 * 60 );
}
/**
* 转换为时间 获取当前小时
*
* @param timeMillis
* @return
*/
public static String formatDateTimeHours(long timeMillis) {
long day = timeMillis / (24 * 60 * 60 * 1000);
long hour = (timeMillis / (60 * 60 * 1000) - day * 24);
return hour + "";
}
/**
* 得到当天字符串 格式(dd)
*/
public static String getHours() {
return formatDate(new Date(), "HH");
}
/**
* 根据时间戳获取日期 是哪一天day
* @param s
* @return
*/
public static String getDayByString(String s){
String res ="";
if (!StringUtils.isEmpty(s)){
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd");
long lt = new Long(s);
Date date = new Date(lt);
res = simpleDateFormat.format(date);
}
return res;
}
public static void main(String[] args) {
/*String s = DateUtils.DateToString();
String dayByString = DateUtils.getDayByString(s);
Long f = Long.valueOf(s);
String hours = DateUtils.getHours();
System.out.println(hours);
long day= f / (1000 * 60 * 60 * 24 );
System.out.println(dayByString+"day");
System.out.println(s);
String date = DateUtils.getDate(s);
System.out.println(date);*/
String week = DateUtils.getWeek();
System.out.println(week);
/*long l = 1586152726000L;
long l2 =1586239126000L;
double hoursOfTwoDate = DateUtils.getHoursOfTwoDate(l, l2);
System.out.println(hoursOfTwoDate);
String s = DateUtils.formatDateTimeHours(1586152726000L);
System.out.println(s+"ssssssssssss");
String hours = DateUtils.getHours();
System.out.println(hours);*/
}
}
原文:https://www.cnblogs.com/dyh2021/p/14305921.html