Date date = new Date();
SimpleDateFormat formatterDateTime01 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//SimpleDateFormat formatterDateTime02 = new SimpleDateFormat("yyyyMMdd HHmmss");
String dateString = formatterDateTime01.format(date);
SimpleDateFormat formatterTime = new SimpleDateFormat("HH:mm:ss");
SimpleDateFormat formatterDate = new SimpleDateFormat("yyyy-MM-dd");
//String 转Date类型
/**
* 获取现在时间
* @return 返回时间类型 yyyy-MM-dd HH:mm:ss
*/
public static Date getNowDate() {
Date currentTime = new Date();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dateString = formatter.format(currentTime);
ParsePosition pos = new ParsePosition(8);
Date currentTime_2 = formatter.parse(dateString, pos);
return currentTime_2;
}
/**
* 获取现在时间
* @return返回短时间格式 yyyy-MM-dd
*/
DateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");
DateFormat format 2= new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒");
Date date = null;
String str = null;
// String转Date
str = "2007-1-18";
try {
date = format1.parse(str);
data = format2.parse(str);
} catch (ParseException e) {
e.printStackTrace();
}