将字符串转换为 Java 中的日历对象 [英] Convert String to Calendar Object in Java
本文介绍了将字符串转换为 Java 中的日历对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是 Java 新手,通常使用 PHP.
I am new to Java, usually work with PHP.
我正在尝试转换此字符串:
I am trying to convert this string:
格林威治标准时间 2011 年 3 月 14 日星期一 16:02:37
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
另见
这篇关于将字符串转换为 Java 中的日历对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文