将字符串转换为Java中的日历对象 [英] Convert String to Calendar Object in Java

查看:97
本文介绍了将字符串转换为Java中的日历对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在尝试转换这个字符串:



< blockquote>

Mon Mar 14 16:02:37 GMT 2011


进入日历对象,以便我可以容易地拉这个年份和月份:

  String yearAndMonth = cal.get(Calendar.YEAR)+ cal.get 。月); 

手动解析是个坏主意吗?使用一个子串方法?



任何建议都有助于谢谢!

解决方案

 日历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)); //所有完成

注意:根据您的环境/要求设置区域设置






另请参阅




I am new to Java, usually work with PHP.

I am trying to convert this string:

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

note: set Locale according to your environment/requirement


See Also

这篇关于将字符串转换为Java中的日历对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆