You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
177 lines
4.8 KiB
177 lines
4.8 KiB
package com.bfd.qanda.utils;
|
|
|
|
|
|
import java.math.BigInteger;
|
|
import java.security.MessageDigest;
|
|
import java.security.NoSuchAlgorithmException;
|
|
import java.text.ParseException;
|
|
import java.text.SimpleDateFormat;
|
|
import java.time.LocalDateTime;
|
|
import java.time.format.DateTimeFormatter;
|
|
import java.util.Date;
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
import com.alibaba.fastjson.JSON;
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
|
/**
|
|
* 日期工具类
|
|
*
|
|
* @author jian.mao
|
|
* @date 2022年11月15日
|
|
* @description
|
|
*/
|
|
@Slf4j
|
|
public class DateUtil {
|
|
|
|
/**
|
|
* @return
|
|
*/
|
|
public static String getTimeStrForNow() {
|
|
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHH");
|
|
return sdf.format(new Date());
|
|
}
|
|
|
|
|
|
public static String getTimeStrForDay(long time) {
|
|
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
|
|
|
|
return sdf.format(new Date(time * 1000));
|
|
}
|
|
|
|
public static String getTimeStrForDay() {
|
|
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
|
|
|
|
return sdf.format(new Date());
|
|
}
|
|
|
|
|
|
public static String getDateTime() {
|
|
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
String time = sdf.format(new Date());
|
|
return time;
|
|
}
|
|
|
|
public static String getDateTime(Long timestap) {
|
|
|
|
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
String time = sdf.format(new Date(timestap));
|
|
return time;
|
|
}
|
|
|
|
public static String getDate(Long timestap) {
|
|
|
|
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
|
|
String time = sdf.format(new Date(timestap));
|
|
return time;
|
|
}
|
|
|
|
public static String getDateTimeForMonth() {
|
|
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");
|
|
String time = sdf.format(new Date());
|
|
return time;
|
|
}
|
|
|
|
/**
|
|
* 休眠
|
|
*
|
|
* @param millis 毫秒
|
|
*/
|
|
public static void sleep(long millis) {
|
|
try {
|
|
Thread.sleep(millis);
|
|
} catch (InterruptedException e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 1. @Description:时间戳转时间
|
|
* 2. @Author: ying.zhao
|
|
* 3. @Date: 2023/3/28
|
|
*/
|
|
|
|
public static String timestampToDate(String time) {
|
|
int thirteen = 13;
|
|
int ten = 10;
|
|
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
// if (time.length() == thirteen) {
|
|
if (time.length() > ten) {
|
|
return sdf.format(new Date(Long.parseLong(time)));
|
|
} else {
|
|
return sdf.format(new Date(Integer.parseInt(time) * 1000L));
|
|
}
|
|
}
|
|
|
|
public static String parseCreated(String jsonTime){
|
|
String formattedDateTime = getDateTime();
|
|
try {
|
|
// 使用fastjson解析JSON数据
|
|
JSONObject jsonObject = JSON.parseObject(jsonTime);
|
|
// 获取日期和时间的值
|
|
JSONObject dateObject = jsonObject.getJSONObject("date");
|
|
int day = dateObject.getIntValue("day");
|
|
int month = dateObject.getIntValue("month");
|
|
int year = dateObject.getIntValue("year");
|
|
|
|
JSONObject timeObject = jsonObject.getJSONObject("time");
|
|
int hour = timeObject.getIntValue("hour");
|
|
int minute = timeObject.getIntValue("minute");
|
|
int second = timeObject.getIntValue("second");
|
|
|
|
// 创建LocalDateTime对象
|
|
LocalDateTime dateTime = LocalDateTime.of(year, month, day, hour, minute, second);
|
|
|
|
// 定义日期时间格式化器
|
|
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
|
|
|
|
// 格式化日期时间
|
|
formattedDateTime = dateTime.format(formatter);
|
|
} catch (Exception e) {
|
|
log.info("日期转换失败:{}",e);
|
|
}
|
|
return formattedDateTime;
|
|
}
|
|
|
|
/**
|
|
* 字符串转换日期
|
|
* @param format
|
|
* @param date
|
|
* @return
|
|
*/
|
|
public static Date strToDate(String format,String date){
|
|
SimpleDateFormat sdf = new SimpleDateFormat(format);
|
|
if (date == null || date.equals("")){
|
|
return new Date();
|
|
}else{
|
|
Date ru = null;
|
|
try {
|
|
ru = sdf.parse(date);
|
|
} catch (ParseException e) {
|
|
e.printStackTrace();
|
|
}
|
|
return ru;
|
|
}
|
|
}
|
|
/**
|
|
* 日期格式话
|
|
* @param format 日期格式
|
|
* @param dater 要转换的日期,默认当前时间
|
|
* @return
|
|
*/
|
|
public static String FormatDate(String format,Date date){
|
|
String fromatDate = null;
|
|
SimpleDateFormat sdf = new SimpleDateFormat(format);
|
|
if (date == null){
|
|
fromatDate = sdf.format(new Date());
|
|
}else{
|
|
fromatDate = sdf.format(date);
|
|
}
|
|
return fromatDate;
|
|
}
|
|
public static void main(String[] args) {
|
|
String time = timestampToDate("955814400000");
|
|
System.out.println(time);
|
|
}
|
|
}
|