SimpleDateFormat日期格式轉換的使用

weixin_50961458發表於2020-12-13

Data型別資料和String型別的轉換

    public void simpleDateFormat (){
        //將Date型別轉換為String型別
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
        String formatRightNow = simpleDateFormat.format(new Date());
        //將String型別轉換為Date型別
        String string = "2018-8-24 12:50:20:545";
        Date date = simpleDateFormat.parse(string);//呼叫parse()方法時 注意 傳入的格式必須符合simpleDateFormat物件的格式,即"yyyy-MM-dd HH:mm:ss:SSS" 否則會報錯!!
    }

相關文章