【问题标题】:Difference between MySQL datetime and joda timeMySQL datetime 和 joda time 之间的区别
【发布时间】:2015-07-11 17:29:01
【问题描述】:

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.0 和2015-05-01T22:08:15.705+02:00 这两个日期之间的差异?

【问题讨论】:

  • 只需读取并比较两者作为时间戳(长值),例如。时间戳 ts = new Timestamp(now.getMillis());
  • MySQL的日期值是java.sql.TimeStamp的形式吗?
  • @MadProgrammer 是的,创建表时它的时间戳

标签: java mysql jodatime


【解决方案1】:

您需要为 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());

【讨论】:

    【解决方案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);
    }
    

    【讨论】:

      【解决方案3】:

      由于您没有指定字符串时间的时区,假设为 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
      

      【讨论】:

        【解决方案4】:

        无需考虑两个日期时间瞬间的String 表示。假设您正在从列 timestamp_col 查询数据库中的值,您可以简单地这样做:

        ResultSet rs = myStatement.executeQuery();
        
        if (rs.next()) {
            Timestamp databaseTime = rs.getTimestamp("timestamp_col");
            int minutesInBetween = Minutes.minutesBetween(
                    new DateTime(databaseTime),
                    DateTime.now()).getMinutes();
            // ...
        }
        

        如果您的数据库与 JVM 位于不同的时区,那么您需要在 Calendar 到 rs.getTimestamp 中传递正确的数据库时区。

        【讨论】:

          【解决方案5】:
          String format = "dd-MMM-yy hh.mm.ss.SSSS"
          DateTime dateTime  = DateTime.parse(input,DateTimeFormat.forPattern(format));
          

          【讨论】:

            猜你喜欢
            • 2012-08-15
            • 1970-01-01
            • 1970-01-01
            • 2014-05-12
            • 2012-10-31
            • 2013-07-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多