Blackberry - Set Time part of Date to the Start of a Day (12:00 AM)
As i am working on a reminder application, i need to show the reminder dialog whenever the set date has arrived for a particular data. I am using the following code to store the date.
long now = System.currentTimeMillis();
DateField dateField = new DateField("",now,DateField.DATE);
long date = dateField.getDate();
For example, When the device Date & time is, 15 December, 2009 & 5:30:43 A.M, i am storing a data to Remind me o开发者_运维知识库n 18 December, 2009. When my app want to remind it, reminder comes on the same date as expected, but also in the same time only. i.e: 18 December, 2009, at 5:30:43 A.M. But i wanted to be shown as soon as 18 December, 2009 arrived when it is 12:00 A.M, i want to remind it.
I don't want to have time also in stored date. I just want to remind dialog as soon as the day arrived at 12:00 A.M.
I think when i store it, instead of 'System.currentTimeMillis', i should store with always '12:00 A.M" time in milliseconds?
Can someone please advice me to achieve it professionaly?
Thank you very much.
You can use math:
public long getDayStartByMath(long date) {
// 1000*60*60*24
long fullDayPart = 86400000;
return date - date % fullDayPart + (fullDayPart >> 1);
}
Other way is to use Calendar:
public long getDayStartByCalendar(long date) {
Calendar c = Calendar.getInstance();
c.setTime(new Date(date));
c.set(Calendar.AM_PM, Calendar.AM);
c.set(Calendar.HOUR, 12);
c.set(Calendar.MINUTE, 0);
c.set(Calendar.SECOND, 0);
c.set(Calendar.MILLISECOND, 0);
return c.getTime().getTime();
}
精彩评论