java格式話時間報錯java cannot format given object as a date


代碼

String MonthYear = null;    
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("mm/yyyy");    
String dateformat = "2012-11-17T00:00:00.000-05:00"
MonthYear = simpleDateFormat.format(dateformat);    
System.out.println(MonthYear);

 解決方法:(這個錯誤的原因就是說,String類型無法直接parse為一個date類型,所以需要先把String字符串,格式化為一個datea類型,最后再格式化為你想要的格式)

DateFormat outputFormat = new SimpleDateFormat("yyyy/MM");
        DateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.US);
        Date date = null;
        String inputText = "2012-11-17";
        try {
            date = inputFormat.parse(inputText);
        }catch (Exception e){
            e.printStackTrace();
        }
        String outputText = outputFormat.format(date);
        System.out.println(outputText);

 

https://blog.csdn.net/S_body/article/details/80453023


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM