private final static SimpleDateFormat sdfDay = new SimpleDateFormat("yyyy-MM-dd");
/**
* 获取YYYY-MM-DD格式
*
* @return
*/
public static String getDay() {
return sdfDay.format(new Date());
}
SimpleDateFormat sfdate =new SimpleDateFormat("yyyy-MM-dd");
if(DateUtil.getDay().equals(sfdate.parse(registerPd.getString("registration_date")).getTime())){
....
}
如果if()中用 == 就会报一下错误
可知SimpleDateFormat.format()格式化是返回的是string类型
而SimpleDateFormat.parse() 返回Date格式
public static Date fomatDate(String date) {
DateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
try {
return fmt.parse(date);
} catch (ParseException e) {
e.printStackTrace();
return null;
}
}
//SimpleDateFormat中的parse方法可以
//把String型的字符串转换成特定格式的date类型
public static void main(String[] args) {
String dStr = "2001.12.12-08:23:21";
Date d = null;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd-HH:mm:ss");
try {
d = sdf.parse(dStr);
} catch (ParseException pe) {
System.out.println(pe.getMessage());
}
System.out.println(d);
System.out.println(d.getTime());
}