符合或更晚。不需要字符串,也不需要java.sql.*类

符合或更晚。不需要字符串,也不需要java.sql.*类,java,validation,date,compareto,Java,Validation,Date,Compareto,从哪里获得java.time类 然后 内置的 标准JavaAPI的一部分,带有捆绑实现 Java9添加了一些次要功能和修复 及 大部分java.time功能都在中向后移植到Java6和Java7 更高版本的Android捆绑包实现了java.time类 对于早期的Android(不相关(我不知道你在问什么),但答案要么是>0,因为根据数据库中的日期2018-06-12应该等于当前日期。但在这种情况下,它不是。我只是以

从哪里获得java.time类

  • 然后
    • 内置的
    • 标准JavaAPI的一部分,带有捆绑实现
    • Java9添加了一些次要功能和修复
    • 大部分java.time功能都在中向后移植到Java6和Java7
    • 更高版本的Android捆绑包实现了java.time类

    • 对于早期的Android(不相关(我不知道你在问什么),但答案要么是>0,因为根据数据库中的日期2018-06-12应该等于当前日期。但在这种情况下,它不是。我只是以<0Yes.
      rs.getDate()
      返回一个日期,没有时间组件(或者更准确地说,时间成分是午夜)。但是
      newdate()
      提供当前日期和时间。请使用dateFormat.format(rs.getDate(5))在所有if语句中,所有语句都将采用相同的格式。您需要的是
      LocalDate
      from。它是一个没有时间的日期,因此不能对您耍这种把戏。另外,
      java.time
      使用起来更好。或者使用java 8中引入的日期/时间API,
      sqlDate.toLocalDate().compareTo(LocalDate.now())
      :PThanks,我从来没有想过时间也包括在内。这是如此简单。并在顶部为您升级到现代API。热烈推荐。感谢您的详细回复!但现在我面临一个新问题,我不知道为什么,但当您尝试在DB中存储日期时,没有发生任何错误或任何错误事情和日期不会更新,知道吗?@Kristofer我添加了一个完整的H2数据库示例。至于:(a)你在
      updateDate
      SQL中声明了两个占位符,但你只设置了其中一个。(b)你从来没有时间执行准备好的语句。因此没有尝试更新。(c)MySQL语法中是否允许回勾字符?据我所知,标准SQL中不允许回勾字符。(d)提示:使用try with resources语句,这是Java 9中新改进的语句。
      private static void Resetter() throws ParseException, SQLException {
          String host = "****";
          String username = "root";
          String mysqlpassword = "";
      
          //Querys
          String query = "select * from accounts";
          String queryy = "update accounts set daily_search_count = 0 where id = ?";
          Connection con = DriverManager.getConnection(host, username, mysqlpassword);
          Statement st = con.createStatement();
          ResultSet rs = st.executeQuery(query);
      
          DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
          dateFormat.setTimeZone( TimeZone.getTimeZone( "UTC" ));
          Date currentDate = new Date();
      
          while(rs.next()){
              System.out.println(dateFormat.format(currentDate));
              if (rs.getDate(5).compareTo(currentDate) > 0) {
                 // System.out.println("Database-date is after currentDate");
              } else if (rs.getDate(5).compareTo(currentDate) < 0) {
                 // System.out.println("Database-date is before currentDate");
                  PreparedStatement updatexdd = con.prepareStatement(queryy);
                  updatexdd.setInt(1, rs.getInt(1));
                  int updatexdd_done = updatexdd.executeUpdate();
              } else if (rs.getDate(5).compareTo(currentDate) == 0) {
                 // System.out.println("Database-date is equal to currentDate");
              } else {
                  System.out.println("Any other");
              }
          }
      }
      
      Calendar cal = Calendar.getInstance();
      cal.set(Calendar.HOUR_OF_DAY, 0);
      cal.set(Calendar.MINUTE, 0);
      cal.set(Calendar.SECOND, 0);
      cal.set(Calendar.MILLISECOND, 0);
      Date currentDate = cal.getTime();
      
      Date currentDate = new java.sql.Date(System.currentTimeMillis());
      
      while(rs.next()){
          System.out.println(dateFormat.format(currentDate));
          if (rs.getDate(5).after(currentDate)) {
             // System.out.println("Database-date is after currentDate");
          } else if (rs.getDate(5).before(currentDate)) {
             // System.out.println("Database-date is before currentDate");
              PreparedStatement updatexdd = con.prepareStatement(queryy);
              updatexdd.setInt(1, rs.getInt(1));
              int updatexdd_done = updatexdd.executeUpdate();
          } else {
             // System.out.println("Database-date is equal to currentDate");
          }
      }
      
      LocalDate today = LocalDate.now( ZoneOffset.UTC ) ;
      Instant instant = myResultSet.getObject( … , Instant.class ) ;  // Retrieve a `TIMESTAMP WITH TIME ZONE` value in database as an `Instant` for a date with time-of-day in UTC with a resolution as fine as nanoseconds.
      LocalDate ld = instant.atOffset( ZoneOffset.UTC ).toLocalDate() ;  // Extract a date-only value without time-of-day and without time zone.
      
      if ( ld.isBefore( today ) ) { … }       // Compare `LocalDate` objects.
      else if ( ld.isEqual( today ) ) { … }
      else if ( ld.isAfter( today ) ) { … }
      else { … handle error }
      
      LocalDate ld = myResultSet.get( … , LocalDate.class ) ;  // Retrieving from database.
      myPreparedStatement.setObject( … , ld ) ;  // Storing in database.
      
      Instant instant = myResultSet.get( … , Instant.class ) ;  // Retrieving from database.
      myPreparedStatement.setObject( … , instant ) ;  // Storing in database.
      
      Boolean overdue = someLocalDate.isAfter( otherLocalDate ) ;
      
      OffsetDateTime odt = instant.atOffset( ZoneOffset.UTC ) ;  
      LocalDate ld = odt.toLocalDate() ;   // Extract a date-only value without time-of-day and without time zone.
      
      LocalDate today = LocalDate.now( ZoneOffset.UTC ) ;  // Specify the offset/zone by which you want to perceive the current date.
      
      if ( ld.isBefore( today ) ) { … }
      else if ( ld.isEqual( today ) ) { … }
      else if ( ld.isAfter( today ) ) { … }
      else { … handle error }
      
      String output = LocalDate.now().toString() ;  // Ex: 2018-01-23
      
      try (
          Connection conn = DriverManager.getConnection( "jdbc:h2:mem:trashme" )
      ) {
          String sql = "CREATE TABLE " + "tbl_" + " (\n" +
                           "  uuid_ UUID DEFAULT random_uuid() , \n" +  // Every table should have a primary key.
                           "  when_ DATE \n" +                          // Three columns per the Question.
                           " );";
          try (
              Statement stmt = conn.createStatement() ;
          ) {
              stmt.execute( sql );
          }
      
          sql = "INSERT INTO tbl_ ( when_ ) VALUES ( ? ) ;";
          LocalDate start = LocalDate.of( 2018 , Month.JANUARY , 23 );
          LocalDate ld = start;  // Keep the `start` object for use later.
          try (
              PreparedStatement ps = conn.prepareStatement( sql )
          ) {
              for ( int i = 1 ; i <= 10 ; i++ ) {
                  ps.setObject( 1 , ld );
                  ps.executeUpdate();
                  // Prepare for next loop.
                  int randomNumber = ThreadLocalRandom.current().nextInt( 1 , 5 + 1 ); // Pass minimum & ( maximum + 1 ).
                  ld = ld.plusDays( randomNumber ); // Add a few days, an arbitrary number.
              }
          }
      
          // Dump all rows, to verify our populating of table.
          System.out.println( "Dumping all rows: uuid_ & when_ columns." );
          sql = "SELECT uuid_ , when_ FROM tbl_ ; ";
          int rowCount = 0;
          try (
              Statement stmt = conn.createStatement() ;
              ResultSet rs = stmt.executeQuery( sql ) ;
          ) {
              while ( rs.next() ) {
                  rowCount++;
                  UUID uuid = rs.getObject( 1 , UUID.class );
                  LocalDate localDate = rs.getObject( 2 , LocalDate.class );
                  System.out.println( uuid + " " + localDate );
              }
          }
          System.out.println( "Done dumping " + rowCount + " rows." );
      
      
          // Dump all rows, to verify our populating of table.
          System.out.println( "Dumping rows where `when_` is after " + start + ": uuid_ & when_ columns." );
          sql = "SELECT uuid_ , when_ FROM tbl_ WHERE when_ > ? ; ";
          rowCount = 0; // Reset count.
          final PreparedStatement ps = conn.prepareStatement( sql );
          ps.setObject( 1 , start );
          try (
              ps ;
              ResultSet rs = ps.executeQuery() ;
          ) {
              while ( rs.next() ) {
                  rowCount++;
                  UUID uuid = rs.getObject( 1 , UUID.class );
                  LocalDate localDate = rs.getObject( 2 , LocalDate.class );
                  System.out.println( uuid + " " + localDate );
              }
          }
          System.out.println( "Done dumping " + rowCount + " rows." );
      
      } catch ( SQLException eArg ) {
          eArg.printStackTrace();
      }