mirror of
https://gitee.com/jzsw-it/yexuejc-base.git
synced 2025-06-06 13:54:03 +08:00
[update] 日期时间工具丰富
This commit is contained in:
parent
4c77c4ebe8
commit
b0b8cbc7ab
@ -1,11 +1,13 @@
|
||||
package com.yexuejc.base.util;
|
||||
|
||||
import java.text.ParseException;
|
||||
import java.time.*;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.time.temporal.TemporalAdjuster;
|
||||
import java.time.temporal.TemporalAdjusters;
|
||||
import java.util.Date;
|
||||
|
||||
|
||||
/**
|
||||
* 新版时间日期出来 工具
|
||||
*
|
||||
@ -258,9 +260,19 @@ public class DateTimeUtil {
|
||||
* @return
|
||||
*/
|
||||
public static LocalDateTime parseLocalDateTime13(long timestamp) {
|
||||
return parseLocalDateTime13(timestamp, ZoneId.systemDefault());
|
||||
}
|
||||
|
||||
/**
|
||||
* Long 转 LocalDateTime
|
||||
*
|
||||
* @param timestamp 13位(毫秒)
|
||||
* @param zoneId 时区
|
||||
* @return
|
||||
*/
|
||||
public static LocalDateTime parseLocalDateTime13(long timestamp, ZoneId zoneId) {
|
||||
Instant instant = Instant.ofEpochMilli(timestamp);
|
||||
ZoneId zone = ZoneId.systemDefault();
|
||||
return LocalDateTime.ofInstant(instant, zone);
|
||||
return LocalDateTime.ofInstant(instant, zoneId);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -270,9 +282,32 @@ public class DateTimeUtil {
|
||||
* @return
|
||||
*/
|
||||
public static LocalDateTime parseLocalDateTime10(long timestamp) {
|
||||
return parseLocalDateTime10(timestamp, ZoneId.systemDefault());
|
||||
}
|
||||
|
||||
/**
|
||||
* Long 转 LocalDateTime
|
||||
*
|
||||
* @param timestamp 10位(秒)
|
||||
* @param zoneId 时区
|
||||
* @return
|
||||
*/
|
||||
public static LocalDateTime parseLocalDateTime10(long timestamp, ZoneId zoneId) {
|
||||
Instant instant = Instant.ofEpochMilli(timestamp * 1000);
|
||||
ZoneId zone = ZoneId.systemDefault();
|
||||
return LocalDateTime.ofInstant(instant, zone);
|
||||
return LocalDateTime.ofInstant(instant, zoneId);
|
||||
}
|
||||
|
||||
/**
|
||||
* UTC(格林威治,标准)时间
|
||||
*
|
||||
* @param timestamp 13位(毫秒)/ 10位(秒)
|
||||
* @return
|
||||
*/
|
||||
public static LocalDateTime parserUTC(Long timestamp) {
|
||||
if (String.valueOf(timestamp).length() == 10) {
|
||||
timestamp = timestamp * 1000;
|
||||
}
|
||||
return parseLocalDateTime13(timestamp, ZoneId.of("UTC"));
|
||||
}
|
||||
|
||||
/**
|
||||
@ -299,6 +334,7 @@ public class DateTimeUtil {
|
||||
return instant.toEpochMilli();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 格式化时间 <br/>
|
||||
* 格式 yyyy-MM-dd HH:mm:ss
|
||||
@ -351,11 +387,57 @@ public class DateTimeUtil {
|
||||
return df.format(dateTime);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取UTC(格林威治,标准)时间
|
||||
*
|
||||
* @param date
|
||||
* @return
|
||||
*/
|
||||
public static LocalDateTime convertUTC(LocalDateTime date) {
|
||||
return convertTimezone(date, null, "UTC");
|
||||
}
|
||||
|
||||
/** public static void main(String[] args) {
|
||||
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
||||
System.out.println(df.format(zonedDateTime2Date(ZonedDateTime.now())));
|
||||
System.out.println(df2.format(date2ZonedDateTime(new Date())));
|
||||
/**
|
||||
* 时区转换(当前时区)
|
||||
*
|
||||
* @param date 时间
|
||||
* @param zoneId 转换时区
|
||||
* @return
|
||||
*/
|
||||
public static LocalDateTime convertTimezone(LocalDateTime date, String zoneId) {
|
||||
return convertTimezone(date, null, zoneId);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 时区转换
|
||||
*
|
||||
* @param date 时间
|
||||
* @param currentZoneId 当前时间的时区
|
||||
* @param targetZoneId 转换时区
|
||||
* @return
|
||||
*/
|
||||
public static LocalDateTime convertTimezone(LocalDateTime date, String currentZoneId, String targetZoneId) {
|
||||
ZoneId currentZone;
|
||||
if (currentZoneId == null) {
|
||||
currentZone = ZoneId.systemDefault();
|
||||
} else {
|
||||
currentZone = ZoneId.of(currentZoneId);
|
||||
}
|
||||
ZoneId targetZone = ZoneId.of(targetZoneId);
|
||||
return date.atZone(currentZone).withZoneSameInstant(targetZone).toLocalDateTime();
|
||||
}
|
||||
|
||||
|
||||
/*public static void main(String[] args) throws ParseException {
|
||||
System.out.println(parserUTC(1684140338161L));
|
||||
System.out.println(convertUTC(LocalDateTime.now()));
|
||||
Date date = DateUtil.str2dateTime("2023-05-15 08:28:05.327");
|
||||
LocalDateTime localDateTime = parseLocalDateTime(date);
|
||||
System.out.println(date);
|
||||
System.out.println(localDateTime);
|
||||
LocalDateTime utc = convertTimezone(localDateTime, "UTC", "Asia/Shanghai");
|
||||
System.out.println(utc);
|
||||
|
||||
System.out.println(getWeek4First());
|
||||
System.out.println(format(getWeek4First(LocalDate.parse("2018-02-10")).atTime(LocalTime.MIN)));
|
||||
|
@ -3,6 +3,7 @@ package com.yexuejc.base.util;
|
||||
import java.text.DateFormat;
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.time.ZoneId;
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
import java.util.Locale;
|
||||
@ -191,4 +192,44 @@ public class DateUtil {
|
||||
return calendar.getTime();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取UTC(格林威治,标准)时间
|
||||
*
|
||||
* @param date
|
||||
* @return
|
||||
*/
|
||||
public static Date convertUTC(Date date) {
|
||||
return convertTimezone(date, null, "UTC");
|
||||
}
|
||||
|
||||
/**
|
||||
* 时区转换(当前时区)
|
||||
*
|
||||
* @param date 时间
|
||||
* @param zoneId 转换时区
|
||||
* @return
|
||||
*/
|
||||
public static Date convertTimezone(Date date, String zoneId) {
|
||||
return convertTimezone(date, null, zoneId);
|
||||
}
|
||||
|
||||
/**
|
||||
* 时区转换
|
||||
*
|
||||
* @param date 时间
|
||||
* @param currentZoneId 当前时间的时区
|
||||
* @param targetZoneId 转换时区
|
||||
* @return
|
||||
*/
|
||||
public static Date convertTimezone(Date date, String currentZoneId, String targetZoneId) {
|
||||
ZoneId currentZone;
|
||||
if (currentZoneId == null) {
|
||||
currentZone = ZoneId.systemDefault();
|
||||
} else {
|
||||
currentZone = ZoneId.of(currentZoneId);
|
||||
}
|
||||
ZoneId targetZone = ZoneId.of(targetZoneId);
|
||||
return Date.from(date.toInstant().atZone(currentZone).withZoneSameInstant(targetZone).toInstant());
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user