字符串和Date互相转化

话不多说,上程序,如图

public class TestDate {
    public static void main(String[] args) throws ParseException {
        String str = "2016-6-28 8:00:00";
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //字符串转Date
        Date date = sdf.parse(str);
        //Date转字符串
        str = sdf.format(date);

        System.out.println(date);
        System.out.println(str);
    }
}

结果:

Tue Jun 28 08:00:00 CST 2016
2016-06-28 08:00:00

 

 打印昨天的当前时刻

public class YesterdayCurrent {  
    public static void main(String[] args){  
        Calendar cal = Calendar.getInstance();  
        cal.add(Calendar.DATE, -1);  
        System.out.println(cal.getTime());  
    }  
}  

 




posted @ 2018-10-09 20:06  何其小静  阅读(366)  评论(0编辑  收藏  举报