2012-10-29 60 views
1

我如何獲得前一天晚上11:59 PM和今晚12:00 AM以long值表示?Android獲取日期爲long值

所以很多不同的日期選項我很困惑

+1

'長'值相對於什麼?你有約會嗎?或者你只是想要他們代表多少秒鐘? (86399 vs 0) – Eric

+0

根據手機的當前日期在今晚12:00 AM和前一晚在11:59 PM。我使用它作爲遊標,所以遊標接受長的值不是日期 –

回答

0

您可以使用此計算這樣做。

//11:59 PM of the previous night and 12:00 AM of tonight 

    Calendar prevDate = Calendar.getInstance(); 
    if(prevDate.get(Calendar.DATE)==1) 
    { 
     if(prevDate.get(Calendar.MONTH)==0) 
     { 
      prevDate.set(Calendar.YEAR, prevDate.get(Calendar.YEAR)-1); 
      prevDate.set(Calendar.MONTH,Calendar.DECEMBER); 
      prevDate.set(Calendar.DAY_OF_MONTH,31); 
     } 
     else 
     { 
      prevDate.set(Calendar.MONTH,prevDate.get(Calendar.MONTH)-1); 
      prevDate.set(Calendar.DAY_OF_MONTH, prevDate.getMaximum(Calendar.DAY_OF_MONTH)); 
     } 
    } 
    else 
    { 
     prevDate.set(Calendar.DAY_OF_MONTH,prevDate.get(Calendar.DAY_OF_MONTH)-1); 
    } 

    prevDate.set(Calendar.HOUR_OF_DAY, 11); 
    prevDate.set(Calendar.MINUTE, 59); 

    Calendar currDate = Calendar.getInstance(); 
    currDate.set(Calendar.HOUR_OF_DAY, 0); 
    currDate.set(Calendar.MINUTE, 0); 

    long prevDateLong = prevDate.getTimeInMillis(); 
    long currDateLong = currDate.getTimeInMillis(); 

    Log.i("", prevDate.toString()+" >>>>>>>> "+currDate.toString()); 
    Log.i("", prevDateLong+" >>>>>>>>> "+currDateLong); 
+0

非常感謝你 –

+0

currDate.set(Calendar.HOUR_OF_DAY,0);應該是currDate.set(Calendar.HOUR_OF_DAY,24); –

0

所有的Java日期都在下面。這是自1970年1月1日午夜以來的毫秒數。

Date today = new SimpleDateFormat("yyyy-MMM-dd").parse("2012-Oct-28"); // today at midnight 
long millis = today.getTime(); 

我建議你看看java.util.Calendar類。這並不難,只是單調乏味。

http://www.exampledepot.com/search/luceneapi_node/Calendar

+0

是的,但那會得到一個硬編碼的日期。我今天要在凌晨12:00根據電話日曆的當前日期 –

+0

和前一天晚上 –