|
@@ -24,12 +24,14 @@ public final class DateUtils {
|
|
|
|
|
|
public static int getCurrentHour() {
|
|
|
Calendar calendar = Calendar.getInstance();
|
|
|
+ calendar.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
|
|
|
return calendar.get(Calendar.HOUR_OF_DAY);
|
|
|
}
|
|
|
|
|
|
public static String getBeforeDaysDateStr(String format, int d) {
|
|
|
SimpleDateFormat dateFormat = new SimpleDateFormat(format);
|
|
|
Calendar calendar = Calendar.getInstance();
|
|
|
+ calendar.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
|
|
|
calendar.setTime(new Date());
|
|
|
calendar.add(Calendar.DAY_OF_MONTH, -d);
|
|
|
Date previousDate = calendar.getTime();
|
|
@@ -162,6 +164,7 @@ public final class DateUtils {
|
|
|
Date date = sdf.parse(dateStr);
|
|
|
|
|
|
Calendar calendar = Calendar.getInstance();
|
|
|
+ calendar.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
|
|
|
calendar.setTime(date);
|
|
|
calendar.set(Calendar.HOUR_OF_DAY, 0);
|
|
|
calendar.set(Calendar.MINUTE, 0);
|
|
@@ -200,6 +203,7 @@ public final class DateUtils {
|
|
|
Date date = sdf.parse(dateStr);
|
|
|
|
|
|
Calendar calendar = Calendar.getInstance();
|
|
|
+ calendar.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
|
|
|
calendar.setTime(date);
|
|
|
calendar.set(Calendar.HOUR_OF_DAY, 23);
|
|
|
calendar.set(Calendar.MINUTE, 59);
|
|
@@ -245,6 +249,7 @@ public final class DateUtils {
|
|
|
|
|
|
public static Long getTodayStart() {
|
|
|
Calendar calendar = Calendar.getInstance();
|
|
|
+ calendar.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
|
|
|
calendar.set(Calendar.HOUR_OF_DAY, 0);
|
|
|
calendar.set(Calendar.MINUTE, 0);
|
|
|
calendar.set(Calendar.SECOND, 0);
|
|
@@ -254,6 +259,7 @@ public final class DateUtils {
|
|
|
|
|
|
public static Long getBeforeDayStart(int days) {
|
|
|
Calendar calendar = Calendar.getInstance();
|
|
|
+ calendar.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
|
|
|
calendar.set(Calendar.HOUR_OF_DAY, 0);
|
|
|
calendar.set(Calendar.MINUTE, 0);
|
|
|
calendar.set(Calendar.SECOND, 0);
|
|
@@ -273,7 +279,8 @@ public final class DateUtils {
|
|
|
// 创建日期时间格式化器
|
|
|
DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern("yyyyMMdd");
|
|
|
// 将时间戳转换为 LocalDateTime
|
|
|
- LocalDateTime dateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(timestamp), ZoneId.systemDefault());
|
|
|
+ ZoneId zone = ZoneId.of("Asia/Shanghai");
|
|
|
+ LocalDateTime dateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(timestamp), zone);
|
|
|
// 格式化日期时间并返回
|
|
|
return dateTime.format(dateFormat);
|
|
|
}
|
|
@@ -282,7 +289,8 @@ public final class DateUtils {
|
|
|
// 创建日期时间格式化器
|
|
|
DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern(pattern);
|
|
|
// 将时间戳转换为 LocalDateTime
|
|
|
- LocalDateTime dateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(timestamp), ZoneId.systemDefault());
|
|
|
+ ZoneId zone = ZoneId.of("Asia/Shanghai");
|
|
|
+ LocalDateTime dateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(timestamp), zone);
|
|
|
// 格式化日期时间并返回
|
|
|
return dateTime.format(dateFormat);
|
|
|
}
|
|
@@ -290,6 +298,7 @@ public final class DateUtils {
|
|
|
public static Date getDate(String dateString) {
|
|
|
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
|
|
|
LocalDate localDate = LocalDate.parse(dateString, formatter);
|
|
|
- return Date.from(localDate.atStartOfDay(ZoneId.systemDefault()).toInstant());
|
|
|
+ ZoneId zone = ZoneId.of("Asia/Shanghai");
|
|
|
+ return Date.from(localDate.atStartOfDay(zone).toInstant());
|
|
|
}
|
|
|
}
|