将字符串日期解析为JodDateTime

将字符串日期解析为JodDateTime,datetime,jodatime,Datetime,Jodatime,我在将字符串日期转换为joda DateTime对象时遇到问题 我的日期格式是 fromDate:2015-10-16T00:00:00.000+05:30 toDate:2015-10-17T00:00:00.000+05:30 我不知道该使用哪种日期格式模式,将其转换为datetime对象,我可以在有单独的整数时进行转换,如下所示 fromDate = new DateTime().withDate(params?.fromDate_year.toInteger(

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

我的日期格式是

   fromDate:2015-10-16T00:00:00.000+05:30
   toDate:2015-10-17T00:00:00.000+05:30
我不知道该使用哪种日期格式模式,将其转换为datetime对象,我可以在有单独的整数时进行转换,如下所示

       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()

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

简单地执行此操作,joda library将为您处理所有事情,您只需将字符串日期传递给DateTime()


干杯

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

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);
}
请注意,两个时间戳表示相同的时间点,并将保持适当的可比性

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);
}