天天看点

时间并格式化yyyy-MM-dd HH:mm:ss

获取现在的时间并格式化yyyy-MM-dd HH:mm:ss 

String s=new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
Date date=null;
try {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    date=sdf.parse(s);
}catch (Exception e){
    e.printStackTrace();
}      

将String转化为Date

String holdTime=2019-02-16 12:26:25

Date date=null;
try {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    date=sdf.parse(holdTime);
}catch (Exception e){
    e.printStackTrace();
}      

 Date转String

Date date =

new

Date();

SimpleDateFormat sdf =

new

SimpleDateFormat(

"yyyy-MM-dd"

);

System.out.println(sdf.format(date));

sdf =

new

SimpleDateFormat(

"yyyy-MM-dd HH:mm:ss"

);

System.out.println(sdf.format(date));

sdf =

new

SimpleDateFormat(

"yyyy年MM月dd日 HH:mm:ss"

);

System.out.println(sdf.format(date));

继续阅读