本文介紹了如何轉換字符串“2011-11-29 12:34:25"迄今為止在“dd-MM-yyyy"中JAVA格式的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
我正在嘗試將格式為yyyy-MM-dd HH:mm:ss"的日期轉換為dd-MM-yyyy".
I am trying to convert date which is in string and got format of "yyyy-MM-dd HH:mm:ss" to "dd-MM-yyyy".
我已經實現了以下代碼,但它給出了:java.lang.IllegalArgumentException
I have implmented following code but its giving : java.lang.IllegalArgumentException
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
Date date = new Date(values);
String mydate = dateFormat.format(date);
推薦答案
首先,您必須將日期時間的字符串表示形式解析為 Date 對象.
First you have to parse the string representation of your date-time into a Date object.
DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = (Date)formatter.parse("2011-11-29 12:34:25");
然后您將 Date 對象格式化回您喜歡的格式的字符串.
Then you format the Date object back into a String in your preferred format.
DateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
String mydate = dateFormat.format(date);
這篇關于如何轉換字符串“2011-11-29 12:34:25"迄今為止在“dd-MM-yyyy"中JAVA格式的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!