129 lines
4.2 KiB
Java
129 lines
4.2 KiB
Java
package vvpkassistant.Tools;
|
||
|
||
import lombok.Data;
|
||
|
||
import java.time.Instant;
|
||
import java.time.LocalDate;
|
||
import java.time.LocalDateTime;
|
||
import java.time.ZoneId;
|
||
import java.time.format.DateTimeFormatter;
|
||
import java.util.Arrays;
|
||
import java.util.Calendar;
|
||
import java.util.HashMap;
|
||
import java.util.Map;
|
||
|
||
/************************
|
||
* 工具类 *
|
||
************************/
|
||
public class VVTools {
|
||
|
||
// 获取当前时间戳
|
||
public static long currentTimeStamp() {
|
||
long timeStamp = Calendar.getInstance().getTimeInMillis() / 1000;
|
||
return timeStamp;
|
||
}
|
||
|
||
// 查询当天所有的
|
||
public static Map<String,Long> startAndEndTimeStampForToday() {
|
||
// 获取当前日期(不含时间)
|
||
LocalDate today = LocalDate.now();
|
||
|
||
// 当天的 00:00:00
|
||
LocalDateTime startOfDay = today.atStartOfDay();
|
||
long startTimestamp = startOfDay.atZone(ZoneId.systemDefault()).toEpochSecond();
|
||
|
||
// 当天的 23:59:59
|
||
LocalDateTime endOfDay = today.atTime(23, 59, 59);
|
||
long endTimestamp = endOfDay.atZone(ZoneId.systemDefault()).toEpochSecond();
|
||
|
||
Map<String,Long> result = new HashMap<>();
|
||
result.put("start",startTimestamp);
|
||
result.put("end", endTimestamp);
|
||
return result;
|
||
}
|
||
|
||
/**
|
||
* 获取当前时间字符串
|
||
* @param pattern 时间格式
|
||
* @return 返回当前时间
|
||
*/
|
||
public static String getCurrentTime(String pattern) {
|
||
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
|
||
return LocalDateTime.now().format(formatter);
|
||
}
|
||
|
||
/**
|
||
* 替换替换字符串
|
||
* @param wordA 原文本
|
||
* @param wordB 需要替换的文本
|
||
*/
|
||
public static String replaceChar(String wordA, char wordB) {
|
||
|
||
if (wordA == null || wordA.length() <= 2) {
|
||
return wordA; // 如果字符串太短(<=2个字符),直接返回
|
||
}
|
||
|
||
char[] chars = wordA.toCharArray();
|
||
Arrays.fill(chars, 1, chars.length - 1, wordB);
|
||
return new String(chars);
|
||
}
|
||
|
||
|
||
/**
|
||
* 判断时间戳是否在当天内(基于系统默认时区)
|
||
* @param timestamp 毫秒级时间戳
|
||
* @return true-在当天内,false-不在当天内
|
||
*/
|
||
public static boolean isTimestampInToday(long timestamp) {
|
||
// 1. 获取当前时间戳对应的日期(系统默认时区)
|
||
LocalDate today = LocalDate.now();
|
||
|
||
// 2. 将目标时间戳转为日期(同一时区)
|
||
LocalDate targetDate = Instant.ofEpochMilli(timestamp)
|
||
.atZone(ZoneId.systemDefault())
|
||
.toLocalDate();
|
||
|
||
// 3. 比较日期是否相同
|
||
return targetDate.isEqual(today);
|
||
}
|
||
|
||
// 根据传入的时间戳。计算出当天的开始时间和结束时间的时间戳
|
||
public static Map<String, Long> getDayStartAndEndTimestamp(long timestamp) {
|
||
// 将10位时间戳转换为LocalDateTime
|
||
Instant instant = Instant.ofEpochSecond(timestamp);
|
||
LocalDateTime dateTime = LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
|
||
|
||
// 获取当天的LocalDate
|
||
LocalDate date = dateTime.toLocalDate();
|
||
|
||
// 计算当天的开始时间(00:00:00)
|
||
LocalDateTime startOfDay = date.atStartOfDay();
|
||
long startTimestamp = startOfDay.atZone(ZoneId.systemDefault()).toEpochSecond();
|
||
|
||
// 计算当天的结束时间(23:59:59)
|
||
LocalDateTime endOfDay = date.atTime(23, 59, 59);
|
||
long endTimestamp = endOfDay.atZone(ZoneId.systemDefault()).toEpochSecond();
|
||
|
||
// 创建并返回Map
|
||
Map<String, Long> result = new HashMap<>();
|
||
result.put("start", startTimestamp);
|
||
result.put("end", endTimestamp);
|
||
|
||
return result;
|
||
}
|
||
|
||
|
||
// 计算两个时间戳之间的小时数量
|
||
public static long calculateHoursRound(long expireTime, long currentTime) {
|
||
if (expireTime <= currentTime) return 0;
|
||
long diffSeconds = expireTime - currentTime;
|
||
return diffSeconds / 3600;
|
||
}
|
||
|
||
// 返还积分用(不足1小时忽略)
|
||
public static long calculateHoursFloor(long expireTime, long currentTime) {
|
||
if (expireTime <= currentTime) return 0;
|
||
return (expireTime - currentTime) / 3600;
|
||
}
|
||
}
|