有关日期时间戳的工具类
/**
* 获取字符串时间戳(10位)
* @return
*/
public static String queryTime(){
//获取当前时间的时间戳(毫秒)
long time = System.currentTimeMillis();
return String.valueOf(time/1000);
}
/**
* 时间格式的截取
* 示例:2020-04-12 14:12:20 结果:20200412141220
* @param time
* @return
*/
public static String times(String time){
String replace = time.replace("-", "").replace(":", "").replace(" ", "");
return replace;
}
/**
* 字符串格式的日期转换为字符串格式的时间戳
* 示例:2020-04-12 14:12:20 结果:1586671940
* @param date
* @return
*/
public static String timeStr(String date){
SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
long time = sf.parse(date).getTime();
return String.valueOf(time/1000);
} catch (ParseException e) {
e.printStackTrace();
}
return "转换失败";
}
/**
* 时间戳转换为日期格式
* @param time
* @return
*/
public static String formatDate(String time){
if (time == null || time.isEmpty() || time.equals("null")) {
return "";
}
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
return sdf.format(new Date(Long.valueOf(time + "000")));
}
public static String formatDate1(String time){
if (time == null || time.isEmpty() || time.equals("null")) {
return "";
}
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return sdf.format(new Date(Long.valueOf(time + "000")));
}
有需要请留言!