0

I'm trying to create a Countdown app for an event. I will need to subtract the current time from the future event time, and display the difference in Days, Hours, minutes and seconds.

So far I have managed to make an app that displays the time with this code below. What do I need to add to it to display the time until say Christmas, for example? Preferably the results would change in real time.

    Calendar c = Calendar.getInstance();
    System.out.println("Current time => " + c.getTime());

    SimpleDateFormat df = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
    String formattedDate = df.format(c.getTime());

    text.setText(formattedDate); 
rolandnsharp
  • 281
  • 1
  • 4
  • 13

2 Answers2

1
 Calendar thatDay = Calendar.getInstance();
  thatDay.set(Calendar.DAY_OF_MONTH,25);
  thatDay.set(Calendar.MONTH,7); // 0-11 so 1 less
  thatDay.set(Calendar.YEAR, 1985);

  Calendar today = Calendar.getInstance();



long diff = today.getTimeInMillis() - thatDay.getTimeInMillis(); //result in millis

Here's an approximation...

long days = diff / (24 * 60 * 60 * 1000);
Digvesh Patel
  • 6,503
  • 1
  • 20
  • 34
0

Also take a look at https://developer.android.com/reference/android/text/format/DateUtils.html There are several support methods for caluclating time distances.

Peter
  • 1,769
  • 1
  • 14
  • 18