时间格式:2022-07-12T13:28:56.871Z
方法一:
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String kssj = (String) rowData.get("kssj");
Date sTime = sdf.parse(kssj);
long startTime = sTime.getTime();
String s=String.valueOf(startTime);
方法二:
long date=new Date((String) rowData.get("kssj")).getTime();
java.text.ParseException: Unparseable date: "2022-07-12T16:00:00.745Z"
方法一和二都不行,是不是时间格式有问题
转换成时间戳
public class Test {
public static void main(String[] args) throws ParseException {
String time = "2022-07-12T13:28:56.871Z";
String s = timeFormat(time);//转换为正常格式
String date = dateToStamp(s);//转换为时间戳
System.out.println(date);
}
public static String dateToStamp(String s) throws ParseException {
String res;
//设置时间模版
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = simpleDateFormat.parse(s);
long ts = date.getTime();
res = String.valueOf(ts);
return res;
}
// 转换日期格式
public static String timeFormat(String time){
String temp = time.replace("Z", " UTC");
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS Z");
Date date = null;
try {
date = sdf.parse(temp);
} catch (ParseException e) {
e.printStackTrace();
}
SimpleDateFormat newDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String finalDate = newDate.format(date);
return finalDate;
}
}
时间格式的问题
2022-07-12T13:28:56.871Z 修改为 2022-07-12 13:28:56 即可
先将T替换为空格再转换