3

i need to know tomorrows end date time in milliseconds. to get current datetime in miliseconds we use

long dateTimestamp = System.currentTimeMillis(); // 12/6/2014 7.50 PM

if today date is 12/6/2014 7:50 PM and Tomorrow date is 13/6/2014 and it ends on 11:59 PM. i need this in milliseconds.

Naruto
  • 9,476
  • 37
  • 118
  • 201

3 Answers3

11

Following is how you get the end of tomorrow -

Calendar cal = Calendar.getInstance(); //current date and time      
cal.add(Calendar.DAY_OF_MONTH, 1); //add a day
cal.set(Calendar.HOUR_OF_DAY, 23); //set hour to last hour
cal.set(Calendar.MINUTE, 59); //set minutes to last minute
cal.set(Calendar.SECOND, 59); //set seconds to last second
cal.set(Calendar.MILLISECOND, 999); //set milliseconds to last millisecond
long millis = cal.getTimeInMillis();
Bhesh Gurung
  • 50,430
  • 22
  • 93
  • 142
3
long currentTime = System.currentTimeMillis();
long endOfTomorrow = currentTime + DateUtils.DAY_IN_MILLIS
            + (DateUtils.DAY_IN_MILLIS - currentTime % DateUtils.DAY_IN_MILLIS);
Che Jami
  • 5,151
  • 2
  • 21
  • 18
  • I like this solution most, but it does not take timezone into account. To make it work with timezone, I had to subtract also this value: ` int gmtOffset = TimeZone.getDefault().getRawOffset(); ` – Sergii N. Nov 14 '16 at 10:36
2

This should work:

Calendar calendar = new GregorianCalendar();
calendar.setTime(dateTimestamp);
calendar.set(Calendar.HOUR_OF_DAY, 23);
calendar.set(Calendar.MINUTE, 59);
calendar.set(Calendar.SECOND, 59);
calendar.add(Calendar.DATE, 1);

calendar.getTime(); // retrieves msec time of the time set in calendar
Serhiy
  • 4,073
  • 3
  • 36
  • 66