Java 字符串轉日期 StringToDate


'20191010' 轉換成 2019-10-10 10:10:10
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

以下這個是轉2019-10-10
public static String stringToDate(String date)
{
    //把字符串轉成日期
    Date dateValue = null;
    try {
        dateValue = new SimpleDateFormat("yyyyMMdd").parse(date);
    } catch (ParseException e) {
        e.printStackTrace();
    }
    //把日期轉成需要的格式
    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
    String dateString = formatter.format(dateValue);
    return dateString;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章