package com.exe.df.util;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.util.StrUtil;
import com.diwork.intelliv.workbench.analysis.exe.df.pro.DateUtil;
import com.diwork.intelliv.workbench.report.formula.core.StringUtils;
import lombok.extern.slf4j.Slf4j;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;
import java.util.TimeZone;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* @className DateTimeZoneUtils
* @description: 多时区计算工具类
* @date 2022/5/6 15:46
* @version 1.0
*/
@Slf4j
public class DateTimeZoneUtils {
private static final SimpleDateFormat sdfDateTime = new SimpleDateFormat(DatePattern.NORM_DATETIME_PATTERN);
/**
* 将给定时间根据源时区转换到目标时区对应时间
* @param dateStr 需要转的时间字符串 如: 2022-05-06 12:17:10
* @param sourceTimeZone 源时间时区 如: 北京(Asia/Shanghai 或 UTC+08:00) ZoneId.of("+08:00")
* @param targetTimeZone 目标时间时区 如 东京 (Asia/Tokyo 或者 UTC+09:00) ZoneId.of("+09:00")
* @return 2022-05-06 13:17:10
*/
public static String converDateGMTByZoneId(String dateStr, ZoneId sourceTimeZone, ZoneId targetTimeZone) {
long offset= timeZoneOffset(sourceTimeZone,targetTimeZone);
return converDateGMTByZoneId(dateStr,offset);
}
/**
* 将给定时间根据源时区转换到目标时区对应时间
* @param dateStr 需要转的时间字符串 如: 2022-05-06 12:17:10
* @param offset 偏移量,单位是毫秒
* @return 2022-05-06 13:17:10
*/
public static String converDateGMTByZoneId(String dateStr, long offset){
if(StringUtils.isEmpty(dateStr) || offset==0 || !isValidDate(dateStr)){
return dateStr;
}
log.info("====初始日期:【{}】=====偏移量:【{}】,单位(毫秒)=======",dateStr,offset);
String itemTime= null;
try {
//获取传入的时间值(Date里存放的毫秒数是与时区无关的,都是根据格林威治时间计算所得)
Long time = sdfDateTime.parse(dateStr).getTime();
//格林威治时间毫秒数+偏移量就是目标时间long
Long targetTime =time + offset ;
itemTime = DateUtil.formatDateTime(new Date(targetTime));
log.info("====将给定时间根据源时区转换到目标时区对应时间,转换后【{}】=====",itemTime);
} catch (ParseException e) {
//上面有日期格式校验,所以不会走到这里
log.error("======日期转换异常====={}",e);
}
return itemTime;
}
/**
* 计算目标时区与源时区的偏移量
* @param sourceTimeZone 源时间时区 如: 北京(Asia/Shanghai 或 UTC+08:00) ZoneId.of("+08:00")
* @param targetTimeZone 目标时间时区 如 东京 (Asia/Tokyo 或者 UTC+09:00) ZoneId.of("+09:00")
* @return 2022-05-06 13:17:10
*/
public static long timeZoneOffset(ZoneId sourceTimeZone, ZoneId targetTimeZone){
if(sourceTimeZone==null || targetTimeZone==null){
return 0;
}
//获取源时区时间相对GMT时间的偏移量
int sourceRelativelyGMT = TimeZone.getTimeZone(sourceTimeZone).getRawOffset();
//目标时间时区相对GMT时间的偏移量
int targetRelativelyGMT = TimeZone.getTimeZone(targetTimeZone).getRawOffset();
//目标偏移量-源时区偏移量
int offset=targetRelativelyGMT-sourceRelativelyGMT;
log.info("====源时区【{}】,目标时区【{}】,偏移量【{}】,单位(毫秒)====",sourceTimeZone.getId(),targetTimeZone.getId(),offset);
return offset;
}
/**
* 将给定时间根据系统默认时区转换到目标时区对应时间
* 如:当前系统为北京时间 2022-05-06 11:17:10 目标时区为:东京 (Asia/Tokyo 或者UTC+09:00)
* 得到时间为:2022-05-06 12:17:10
* @param dateStr 需要转的时间字符串
* @param targetTimeZone 目标时间时区
* @return
* @throws ParseException
*/
public static String converDateByDefalutZoneId(String dateStr, ZoneId targetTimeZone) throws ParseException {
if(StringUtils.isEmpty(dateStr)){
return dateStr;
}
Instant instant=sdfDateTime.parse(dateStr).toInstant();
log.info("====系统默认时区【{}】,目标时区【{}】====",ZoneId.systemDefault().getId(),targetTimeZone.getId());
LocalDateTime ldt = LocalDateTime.ofInstant(instant, targetTimeZone);
String itemTime=ldt.format(DatePattern.NORM_DATETIME_FORMATTER);
log.info("====将给定时间根据系统默认时区转换到目标时区对应时间,转换前【{}】,转换后【{}】====",dateStr,itemTime);
return itemTime;
}
/**
* 转换时间时区
*
* @param dateStr 需要转的时间字符串
* @param sourceTimeZone 源时间时区 GMT+8
* @param targetTimeZone 目标时间时区 GMT+6
* @return
* @throws ParseException
*/
public static String converDateGMT(String dateStr, String sourceTimeZone, String targetTimeZone) throws ParseException {
log.info("====初始日期:【{}】,源时区【{}】,目标时区【{}】",dateStr,sourceTimeZone,targetTimeZone);
sourceTimeZone = StrUtil.isEmpty(sourceTimeZone) ? "GMT+8" : sourceTimeZone;
targetTimeZone = StrUtil.isEmpty(targetTimeZone) ? "GMT+8" : targetTimeZone;
//获取传入的时间值
Long time = sdfDateTime.parse(dateStr).getTime();
//获取源时区时间相对的GMT时间
Long sourceRelativelyGMT = time - TimeZone.getTimeZone(sourceTimeZone).getRawOffset();
//GMT时间+目标时间时区的偏移量获取目标时间
Long targetTime = sourceRelativelyGMT + TimeZone.getTimeZone(targetTimeZone).getRawOffset();
Date date = new Date(targetTime);
String targetTimeStr = DateUtil.formatDateTime(date);
log.info("转换成目标时区【{}】的时间=====【{}】",targetTimeZone, targetTimeStr);
return targetTimeStr;
}
/**
* @description: 通过long字符串日期转为对应时区的字符串日期,如果目标时区为空,直接按照系统默认时区转换
* @param: dateStr
* @param: targetTimeZone
* @return: java.lang.String
* @date: 2022/5/9 9:44
*/
public static String converLongDateByZoneStr(Long commentTime, String targetTimeZone) throws ParseException {
if(StringUtils.isEmpty(targetTimeZone)){
targetTimeZone=ZoneId.systemDefault().getId();
}
sdfDateTime.setTimeZone(TimeZone.getTimeZone(targetTimeZone));
// time为转换格式后的字符串
String time = sdfDateTime.format(new Date(commentTime));
log.info("转换成目标时区【{}】的时间=====【{}】",targetTimeZone,time);
return time;
}
/**
* @description: 是否是标准的日期类型 yyyy-MM-dd HH:mm:ss
* @param: str 日期字符串
* @return: boolean
* @date: 2022/5/27 16:23
*/
public static boolean isValidDate(String dateStr) {
boolean convertSuccess = true;
try {
sdfDateTime.setLenient(false);
sdfDateTime.parse(dateStr);
} catch (ParseException e) {
convertSuccess = false;
}
return convertSuccess;
}
/**
* @description: 日期格式是否匹配给定的格式
* @param: dateStr 日期字符串
* @param: strFormat 格式化字符串
* @return: boolean
* @date: 2022/5/27 16:23
*/
public static boolean isValidDate(String dateStr,String strFormat) {
boolean convertSuccess = true;
try {
SimpleDateFormat sdfFormat = new SimpleDateFormat(strFormat);
sdfFormat.setLenient(false);
sdfFormat.parse(dateStr);
} catch (ParseException e) {
convertSuccess = false;
}
return convertSuccess;
}
public static void main(String[] args) throws ParseException {
String dateStr="2022-12-31 18:25:25";
System.out.println(isValidDate(dateStr));
ZoneId zoneId2 = ZoneId.of("Asia/Shanghai");
ZoneId zoneId1 = ZoneId.of("Asia/Tokyo");
//converDateGMTByZoneId(dateStr,zoneId1,zoneId2);
timeZoneOffset(zoneId1,zoneId2);
converDateGMTByZoneId(dateStr,timeZoneOffset(zoneId1,zoneId2));
// Long time =System.currentTimeMillis();
// converLongDateByZoneStr(time,"");
// converLongDateByZoneStr(time,"Asia/Tokyo");
// converLongDateByZoneStr(time,"Asia/Shanghai");
}
}
java 时区工具类
最新推荐文章于 2024-07-28 16:11:54 发布