定义一个字符串日期;
String dataStr="2022-06-01";
public static java.sql.Timestamp parseTimestamp(String dateStr) {
return parseTimestamp(dateStr, "yyyy/MM/dd HH:mm:ss");
}
public static java.sql.Timestamp parseTimestamp(String dateStr,
String format) {
java.util.Date date = parseDate(dateStr, format);
if (date != null) {
long t = date.getTime();
return new java.sql.Timestamp(t);
} else {
return null;
}
}
public static java.util.Date parseDate(String dateStr, String format) {
java.util.Date date = null;
try {
java.text.DateFormat df = new java.text.SimpleDateFormat(format);
String dt = dateStr.replaceAll("-", "/");
if ((!dt.equals(""))) {
dt +=
format.substring(dt.length()).replaceAll("[YyMmDdHhSs]", "0");
}
date = (java.util.Date) df.parse(dt);
} catch (Exception e) {
e.printStackTrace();
}
return date;
}
运行结果
相关文章
暂无评论...