import java.util.Date;
import java.text.SimpleDateFormat;
SimpleDateFormat currtime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
System.out.println(currtime.format(new Date()));// new Date()为获取当前系统时间
https://www.cnblogs.com/jxtx92/p/8005620.html
public static Date getDate(String time) {
Date date = null;
//注意:SimpleDateFormat构造函数的样式与strDate的样式必须相符
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
// SimpleDateFormat sDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //加上时间
//必须捕获异常
try {
date = simpleDateFormat.parse(time);
} catch (ParseException px) {
px.printStackTrace();
} finally {
return date;
}
}
public static String getDateStr(Date date){
SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd");
return simpleDateFormat.format(date);
}