first commit
This commit is contained in:
141
src/main/java/com/example/sso/util/TimeUtil.java
Normal file
141
src/main/java/com/example/sso/util/TimeUtil.java
Normal file
@ -0,0 +1,141 @@
|
||||
package com.example.sso.util;
|
||||
|
||||
import java.text.DateFormat;
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.time.LocalDate;
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
import java.util.Locale;
|
||||
import java.util.TimeZone;
|
||||
|
||||
public class TimeUtil {
|
||||
public static String day() {
|
||||
LocalDate currentDate = LocalDate.now();
|
||||
String one = currentDate.toString();
|
||||
return one;
|
||||
}
|
||||
|
||||
public static long nowday() {
|
||||
Calendar calendar = Calendar.getInstance();
|
||||
calendar.set(Calendar.HOUR_OF_DAY, 0); // 将小时设置为0
|
||||
calendar.set(Calendar.MINUTE, 0); // 将分钟设置为0
|
||||
calendar.set(Calendar.SECOND, 0); // 将秒设置为0
|
||||
calendar.set(Calendar.MILLISECOND, 0); // 将毫秒设置为0
|
||||
|
||||
// 获取当天0点的时间戳(毫秒)
|
||||
long timestamp = calendar.getTimeInMillis();
|
||||
return timestamp;
|
||||
}
|
||||
|
||||
public static long tomorowday() {
|
||||
Calendar calendar = Calendar.getInstance();
|
||||
calendar.add(Calendar.DAY_OF_MONTH, 1); // 将日期增加1天,即获取明天的日期
|
||||
calendar.set(Calendar.HOUR_OF_DAY, 0); // 将小时设置为0
|
||||
calendar.set(Calendar.MINUTE, 0); // 将分钟设置为0
|
||||
calendar.set(Calendar.SECOND, 0); // 将秒设置为0
|
||||
calendar.set(Calendar.MILLISECOND, 0); // 将毫秒设置为0
|
||||
|
||||
// 获取明天0点的时间戳(毫秒)
|
||||
long timestamp = calendar.getTimeInMillis();
|
||||
return timestamp;
|
||||
}
|
||||
|
||||
public static String timeConversion(String originalDateTime) throws ParseException {
|
||||
|
||||
|
||||
|
||||
|
||||
// 创建日期时间格式化对象
|
||||
DateFormat originalFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
|
||||
originalFormat.setTimeZone(TimeZone.getTimeZone("UTC")); // 设置时区为UTC
|
||||
|
||||
// 解析原始日期时间字符串为Date对象
|
||||
Date date = originalFormat.parse(originalDateTime);
|
||||
|
||||
// 加上8个小时
|
||||
long timeInMillis = date.getTime() + (8 * 60 * 60 * 1000); // 8小时的毫秒数
|
||||
|
||||
// 创建新的日期对象
|
||||
Date newDate = new Date(timeInMillis);
|
||||
|
||||
// 创建日期时间格式化对象,用于格式化新的日期对象
|
||||
DateFormat newFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
||||
|
||||
// 格式化新的日期对象为字符串
|
||||
String newDateTime = newFormat.format(newDate);
|
||||
|
||||
// 输出结果
|
||||
|
||||
return newDateTime;
|
||||
|
||||
}
|
||||
|
||||
|
||||
public static String timeConversions(String originalDateTime) throws ParseException {
|
||||
|
||||
|
||||
|
||||
|
||||
// 创建日期时间格式化对象
|
||||
DateFormat originalFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
|
||||
|
||||
// 设置时区为UTC
|
||||
originalFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
|
||||
|
||||
// 解析原始日期时间字符串为Date对象
|
||||
Date date = originalFormat.parse(originalDateTime);
|
||||
|
||||
// 创建日期时间格式化对象,用于格式化Date对象为字符串
|
||||
DateFormat newFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
||||
|
||||
// 格式化Date对象为字符串
|
||||
String newDateTime = newFormat.format(date);
|
||||
return newDateTime;
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public static String month() {
|
||||
|
||||
Calendar calendar = Calendar.getInstance();
|
||||
|
||||
// 获取当前月份
|
||||
int year = calendar.get(Calendar.YEAR);
|
||||
int month = calendar.get(Calendar.MONTH) + 1; // 月份从 0 开始,所以要加 1
|
||||
|
||||
// 格式化为 "YYYY-MM"
|
||||
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM");
|
||||
String formattedMonth = dateFormat.format(calendar.getTime());
|
||||
return formattedMonth;
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
public static String now() {
|
||||
|
||||
Date currentDate = new Date();
|
||||
|
||||
// 创建日期格式化对象,指定目标格式
|
||||
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
|
||||
|
||||
// 格式化当前日期为指定格式的字符串
|
||||
String formattedDate = dateFormat.format(currentDate);
|
||||
return formattedDate;
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user