Java时间戳字符串解析

发布于 2024-10-25 14:20:28 字数 200 浏览 7 评论 0原文

我需要将字符串时间戳值转换为 Java Date 对象。该字符串的格式为“2011-03-16T09:00:00-05:00”。是否有时区表示形式,我可以使用 SimpleDateFormat 将此数据加载为 Date 对象? 'z'、'Z' 和 'zzzz' 是我所知道的唯一时区表示形式,但它们都不能代表我的时区数据 (-05:00)。有人解决这个问题了吗?

谢谢。

I need to convert a string time stamp value into Java Date object. The string is in '2011-03-16T09:00:00-05:00' format. Is there a time zone representation i can use to load this data as a Date object using SimpleDateFormat? 'z','Z' and 'zzzz' are the only time zone representations i am aware of and none of those can represent my time zone data (-05:00). Has anyone solved this problem?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

末蓝 2024-11-01 14:20:28

JodaTime 可能会有所帮助。考虑使用它和自定义格式化程序(称为“怪异格式化程序”)。

http://joda-time.sourceforge.net/userguide.html#Input_and_Output

JodaTime may help. Consider using it and a custom formatter (called "freaky formatters").

http://joda-time.sourceforge.net/userguide.html#Input_and_Output

黑寡妇 2024-11-01 14:20:28

不幸的是,时区中的冒号使事情变得有点复杂。您可能想看看这个问题

Unfortunately, the colon in the time zone complicates matters a bit. You might want to have a look at this question.

杯别 2024-11-01 14:20:28

看到提供的时间戳格式看起来像 XML 中使用的标准格式,您可以尝试以下操作:

public static void main(String[] args) throws DatatypeConfigurationException {

    String inDate = "2011-03-16T09:00:00-05:00";

    javax.xml.datatype.DatatypeFactory factory = DatatypeFactory.newInstance();

    javax.xml.datatype.XMLGregorianCalendar xmlGregCal = factory.newXMLGregorianCalendar(inDate);

    java.util.GregorianCalendar gregCal = xmlGregCal.toGregorianCalendar();

    java.util.Date dateObj = gregCal.getTime();

    System.out.println("cal = " + xmlGregCal.toString());
    System.out.println("cal.year = " + xmlGregCal.getYear());
    System.out.println("cal.month = " + xmlGregCal.getMonth());
    System.out.println("cal.day = " + xmlGregCal.getDay());
    System.out.println("cal.hour = " + xmlGregCal.getHour());
    System.out.println("cal.minute = " + xmlGregCal.getMinute());
    System.out.println("cal.second = " + xmlGregCal.getSecond());
    System.out.println("cal.timezone = " + xmlGregCal.getTimezone());
    System.out.println("cal.eonAndYear = " + xmlGregCal.getEonAndYear());
}

创建的输出如下:

cal = 2011-03-16T09:00:00-05:00
年份 = 2011
卡尔.月 = 3
卡尔日 = 16
校准小时 = 9
卡路里分钟 = 0
校准秒 = 0
cal.时区 = -300
cal.eonAndYear = 2011

Seeing that this timestamp format provided looks like the standard format used in XML, you could try the following:

public static void main(String[] args) throws DatatypeConfigurationException {

    String inDate = "2011-03-16T09:00:00-05:00";

    javax.xml.datatype.DatatypeFactory factory = DatatypeFactory.newInstance();

    javax.xml.datatype.XMLGregorianCalendar xmlGregCal = factory.newXMLGregorianCalendar(inDate);

    java.util.GregorianCalendar gregCal = xmlGregCal.toGregorianCalendar();

    java.util.Date dateObj = gregCal.getTime();

    System.out.println("cal = " + xmlGregCal.toString());
    System.out.println("cal.year = " + xmlGregCal.getYear());
    System.out.println("cal.month = " + xmlGregCal.getMonth());
    System.out.println("cal.day = " + xmlGregCal.getDay());
    System.out.println("cal.hour = " + xmlGregCal.getHour());
    System.out.println("cal.minute = " + xmlGregCal.getMinute());
    System.out.println("cal.second = " + xmlGregCal.getSecond());
    System.out.println("cal.timezone = " + xmlGregCal.getTimezone());
    System.out.println("cal.eonAndYear = " + xmlGregCal.getEonAndYear());
}

The output created is as follows:

cal = 2011-03-16T09:00:00-05:00
cal.year = 2011
cal.month = 3
cal.day = 16
cal.hour = 9
cal.minute = 0
cal.second = 0
cal.timezone = -300
cal.eonAndYear = 2011

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文