import org.springframework.stereotype.Component;
import org.springframework.util.Assert;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;
/**
* @program:
* @description: 时间工具类
* @author: cchan
* @create: 2020年7月16
**/
@Component
public class DateUtils {
public static String timeStamp2Date(String time) {
Long timeLong = Long.parseLong(time);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//要转换的时间格式
Date date;
try {
date = sdf.parse(sdf.format(timeLong));
return sdf.format(date);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/**
* 把13位时间戳转换为"yyyy-MM-dd HH:mm:ss"
* @return
*/
public static String dateToString(Date date){
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return sdf.format(date);
}
/**
* 字符串时间转成Long类型的时间戳
* @return
*/
public static Long convertTimeToLong(String time) {
Assert.notNull(time, "time is null");
DateTimeFormatter ftf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
LocalDateTime parse = LocalDateTime.parse(time, ftf);
return LocalDateTime.from(parse).atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
}
/**
* 获取当前时间的时间戳
*/
public static String NowToStr(){
//获得当前时间
LocalDateTime ldt = LocalDateTime.now();
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
return ldt.format(dtf);
}
/**
* 获取昨天的当前时间
*/
public static Date YesterdayNowTimeStr(){
Date uts = new Date(Calendar.getInstance().getTimeInMillis() - 24 * 60*60 * 1000);
System.out.println("uts:"+uts);
}
}
DateUtils
最新推荐文章于 2023-05-26 10:44:37 发布