MySQL datetime和joda时间之间的区别

时间:2015-05-01 20:11:36

标签: java mysql jodatime

Mysql日期时间如下:2015-05-01 21:36:38.0

和joda当前时间:

DateTime now = new DateTime().toDateTime();
outputs
2015-05-01T22:08:15.705+02:00

如何在几分钟内获得这两个日期2015-05-01 21:36:38.02015-05-01T22:08:15.705+02:00之间的差异?

4 个答案:

答案 0 :(得分:3)

您需要为MySQL时间戳创建DateTimeFormatter。这是一个例子:

     DateTimeFormatter formatter = DateTimeFormat.forPattern("YYYY-MM-dd HH:mm:ss.S");
    String mysqldatetime = "2015-05-01 21:36:38.0";
    DateTime mysqldt = formatter.parseDateTime(mysqldatetime); 

    DateTime now = new DateTime().toDateTime();
    Period diff = new Period(now,mysqldt);
    System.out.println(diff.getMinutes());

答案 1 :(得分:2)

试试这个:

Timestamp _before = (Timestamp) MySQLString.getTime();
Timestamp _now = new Timestamp(new DateTime().getMillis());
System.out.println("CONVERT RESULT MINUTES: "+minutesBetweenTimestamps(_before, _now));

//Pass the variables to this method 

public Integer minutesBetweenTimestamps(Timestamp before, Timestamp after){
    long _timeGap = after.getTime() - before.getTime();
    return (int) (_timeGap / 1000 / 60);
}

答案 2 :(得分:1)

由于您没有指定字符串时间的时区,假设为UTC。

public static void main(String[] args) {

        String columnData = "2015-05-01 11:36:38.0";

        DateTimeFormatter fmt = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss.S");
        DateTime dateTime = fmt.withZoneUTC().parseDateTime(columnData);
        System.out.println("UTC Time: "+dateTime);

        DateTime now = DateTime.now();
        int mins = Minutes.minutesBetween(dateTime, now).getMinutes();
        System.out.println("Minutes : "+mins);

    }

输出:

UTC Time: 2015-05-01T11:36:38.000Z
Current Time 2015-05-01T16:21:41.404-04:00
Minutes : 525

答案 3 :(得分:-1)

String format = "dd-MMM-yy hh.mm.ss.SSSS"
DateTime dateTime  = DateTime.parse(input,DateTimeFormat.forPattern(format));