【问题标题】:How to find difference in Joda time如何找到乔达时间的差异
【发布时间】:2013-02-08 23:25:32
【问题描述】:

我正在尝试使用 Joda Time 的 Interval 类,但我无法使用它的构造函数。它不采用格式。

我正在尝试以yyyy-MM-dd HH:mm:ss 格式从mysql DB 中提取两个DateTime,并以String 格式检索它。我正在尝试将其转换为日期格式,但间隔类无法采用日期格式。请帮助我应该使用什么???

    String text = "2011-10-02 18:48:05";
    String text2 = "2011-10-02 18:50:05";
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
            Date olddate=sdf.parse(text);
            Date newdate=sdf.parse(text2);
            System.out.println(olddate);
            System.out.println(newdate);
//            Interval interval = new Interval(olddate, newdate);



        } catch (ParseException ex) {
            JOptionPane.showMessageDialog(null, "Error at Timestamp subtract Format function Dategenerator" + ex.getMessage());

        }

【问题讨论】:

    标签: java jodatime


    【解决方案1】:

    不要使用java.util.Date,而是使用 Joda Time 的org.joda.time.DateTime

    使用 Joda Time 的 org.joda.time.format.DateTimeFormat 进行解析。

    应该可以的:

        String text = "2011-10-02 18:48:05";
        String text2 = "2011-10-02 18:50:05";
        DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss");
        DateTime oldDate = formatter.parseDateTime(text);
        DateTime newDate = formatter.parseDateTime(text2);
        System.out.println(oldDate);
        System.out.println(newDate);
        Interval interval = new Interval(oldDate, newDate);
        System.out.println(interval);
    

    【讨论】:

    • TY 非常喜欢,我使用的是旧日期格式和 joda 时间。这正是我所需要的。我可以轻松地从中获取会议记录。
    【解决方案2】:

    我能够在 joda time 中使用此代码获得分钟差:

    String text = "2011-10-02 15:48:05";
            String text2 = "2011-10-02 18:52:10";
            DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss");
            DateTime oldDate = formatter.parseDateTime(text);
            DateTime newDate = formatter.parseDateTime(text2);
            System.out.println(oldDate);
            System.out.println(newDate);
            Interval interval = new Interval(oldDate, newDate);        
            System.out.println(interval.toDuration().toPeriod().getHours()*60+  interval.toDuration().toPeriod().getMinutes());
    

    【讨论】:

    • interval.toDuration().getStandardMinutes() 得到的分钟数。
    • true 但它返回 double 而不是 int
    猜你喜欢
    • 2015-03-13
    • 2019-08-04
    • 1970-01-01
    • 2013-03-18
    • 1970-01-01
    • 2023-03-08
    • 2014-05-06
    • 2021-08-17
    • 1970-01-01
    相关资源
    最近更新 更多