rolandnsharp
rolandnsharp

Reputation: 281

Simple Android / Java Countdown App

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); 

Upvotes: 0

Views: 228

Answers (2)

Peter
Peter

Reputation: 1789

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

Upvotes: 0

Digvesh Patel
Digvesh Patel

Reputation: 6533

 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);

Upvotes: 1

Related Questions