• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

在 Java 字符串转换为日历对象

用户头像
it1352
帮助3

问题说明

我是 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!

正确答案

#1
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

另见

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /reply/detail/tanghfiec
系列文章
更多 icon
同类精品
更多 icon
继续加载