【问题标题】:parse string date to jodaDateTime将字符串日期解析为 joda DateTime
【发布时间】:2015-10-17 20:28:59
【问题描述】:

我在将字符串日期转换为 joda DateTime 对象时遇到问题

我的日期格式是

   fromDate:2015-10-16T00:00:00.000+05:30
   toDate:2015-10-17T00:00:00.000+05:30

我不知道要使用哪种日期格式模式,将其转换为日期时间对象,当我有单独的整数时,我可以转换为这样

       fromDate = new DateTime().withDate(params?.fromDate_year.toInteger(), params?.fromDate_month.toInteger(), params?.fromDate_day.toInteger()).withTimeAtStartOfDay()
        toDate =  new DateTime().withDate(params?.toDate_year.toInteger(), params?.toDate_month.toInteger(), params?.toDate_day.toInteger()).withTimeAtStartOfDay()

如何将我的字符串转换为日期??

【问题讨论】:

    标签: datetime jodatime


    【解决方案1】:

    继续@roanjain 所说的,Joda 将负责解析这样的字符串,但是,创建的 DateTime 对象将显示默认时区。如果您的计算机不在“亚洲/加尔各答”时区,您需要告诉 Joda 您想要该时区的 DateTime,如下所示:

    public static void main(String[] args) {
        String time = "2015-10-16T00:00:00.000+05:30";
        DateTime dt = new DateTime(time);
        // Will show whatever time zone you are in
        System.out.println(dt);
        // Same point in time, but represented in a different time zone
        System.out.println(dt.withZone(DateTimeZone.forID("Asia/Kolkata")));
        // Create a DateTime object in the requested timezone
        dt = new DateTime(time).withZone(DateTimeZone.forID("Asia/Kolkata"));
        System.out.println(dt);
    }
    

    请注意,两个时间戳代表相同的时间点,并且将保持适当的可比性。

    【讨论】:

      【解决方案2】:

      这样做很简单,joda 库会为您处理一切,您只需将字符串日期传递给 DateTime()

        String fromDate = "2015-10-16T00:00:00.000+05:30"
        String toDate = "2015-10-17T00:00:00.000+05:30"
        fromDate = new DateTime(fromDate);
        toDate = new DateTime(toDate);
      

      干杯!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-11-21
        • 1970-01-01
        • 1970-01-01
        • 2020-05-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多