2012-11-06 190 views
0

如何比较当前系统时间与android数据库中存储的时间? 我用这个代码:比较当前系统的日期和时间与数据和时间库中存储的日期android

public static long s ; 
. 
. 
. 
. 

Time now = new Time(); 
now.setToNow(); 
String query = "SELECT Cart_ID,Cart_Date from Tbl_Cart where Cart_ID="+lastId+" "; 
     // String queryt = "SELECT Cart_Date from Tbl_Cart where Cart_ID="+lastId+" "; 
      Cursor c = db.rawQuery(query, null); 
      if (c != null && c.moveToFirst()) { 
       s = c.getLong(0); 


      } 

      if(s==now.setToNow()) 
{ 
... 
} 

回答

0

如果你的存储时间是毫秒,那么你可以用System.currentTimeMillis()比较。 你也可以仔细考虑与'=='比较。因为长时间变化如此之快,它很难与系统时间相匹配。你可以更好地寻找更少/更大或相同的时间框架。

0

此代码仅适用于您也可以做同样时间的数据。 SimpleDateFormat dateFormat = new SimpleDateFormat(“MM-dd-yyyy”);

CalCurrentDate = Calendar.getInstance(); 
      // that will pass cureent date . 
    Current_Date = dateFormat.format(CalCurrentDate.getTime());   

取日期&时间从DB使用SQL查询:

CURRENT_DATE:当前数据和该查询比较。现在

selectQuery = "SELECT * FROM MY_TABLE WHERE `Date`>='"+Current_Date+"'"; 

您可以运行此查询为rawQuery()

0

它取决于您如何在数据库中存储数据时间。如果您像日期时间那样存储,那么您可以将单个对象放入日历中,然后将其与System.currentTimeMillis()进行比较;

例如:

Calendar start_time = new GregorianCalendar(); 
    start_time.set(Calendar.HOUR_OF_DAY, sHour); 
    start_time.set(Calendar.MINUTE, sMinute); 

然后将它与SYSTEMTIME像

比较如果(start_time.getTimeInMillis()< = System.currentTimeMillis的())

OR

如果您将时间存储在Millis formate中,然后您可以直接比较它。

e.g:

如果(yourTimes < = System.currentTimeMillis的())

希望它会帮助你。

+0

会将System.currentTimeMillis()用于日期吗? – shadi

+0

是的。但是请注意,如果您打算将其与日期进行比较,那么您也正在使用日历。 –

+0

我在数据库中自动存储日期(当前系统日期型日期时间),我不需要日历,我这样做:String query =“SELECT Cart_Status,Cart_ID,Cart_Date from Tbl_Cart where Cart_ID =”+ lastId +“”; Cursor c = db.rawQuery(query,null); S2 = c.getLong(1); System.currentTimeMillis的()!= S2) – shadi

相关问题