Android将日期作为长值

时间:2012-10-29 00:33:50

标签: android date

我如何得到前一天晚上11:59 PM和今晚中午12点表示长值???

我混淆了很多不同的日期选项

2 个答案:

答案 0 :(得分: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

答案 1 :(得分: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);