本文介紹了在 Java 中將字符串轉換為日歷對象的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
限時送ChatGPT賬號..
我是 Java 新手,通常使用 PHP.
I am new to Java, usually work with PHP.
我正在嘗試轉換這個字符串:
I am trying to convert this string:
2011 年 3 月 14 日星期一 16:02:37 GMT
Mon Mar 14 16:02:37 GMT 2011
到一個日歷對象中,這樣我就可以像這樣輕松地提取年份和月份:
Into a Calendar Object so that I can easily pull the Year and Month like this:
String yearAndMonth = cal.get(Calendar.YEAR)+cal.get(Calendar.MONTH);
手動解析會是個壞主意嗎?使用子字符串方法?
Would it be a bad idea to parse it manually? Using a substring method?
任何建議都會有所幫助,謝謝!
Any advice would help thanks!
推薦答案
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy", Locale.ENGLISH);
cal.setTime(sdf.parse("Mon Mar 14 16:02:37 GMT 2011"));// all done
注意:根據您的環境/要求設置 Locale
note: set Locale
according to your environment/requirement
另見
- Javadoc
這篇關于在 Java 中將字符串轉換為日歷對象的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!