Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CountDown Timer - How to Show Year, Months, Weeks as well

In my program i have to show CountDown Timer and for that i wrote some code, which allow me to get Days, Hours, Minutes and Seconds, but not getting any idea how to calculate Year, Months and weeks as well.

Still i am getting this:

days:384 hours:6 minutes:27 seconds:25

but i need this:

year:1 months:2 weeks:5 days:125 hours:6 minutes:27 seconds:25

Check my below code :

      public class MainActivity extends Activity {
    String time1="31-08-2015 12:01:00";
    TextView tv;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_main);

        tv=(TextView)findViewById(R.id.textView1);

        final Handler handler = new Handler();
        handler.postDelayed(new Runnable() {

            @Override
            public void run() {
                Calendar start = Calendar.getInstance();  
                String time = "dd-MM-yyyy hh:mm:ss"; // 12:00

                String dateStart =(String) DateFormat.format(time, start);                          
                String dateStop = time1;


                 SimpleDateFormat format = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");  
                // Custom date format
                java.util.Date d1=null;
                java.util.Date d2=null;
                  try {
                        d1 = format.parse(dateStart);
                        d2 = format.parse(dateStop);
                    } catch (java.text.ParseException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }

                long diff = d2.getTime() - d1.getTime();             

                long diffdays    =(diff/ (1000*60*60*24));
                long diffHours = ((diff - (1000*60*60*24*diffdays)) / (1000*60*60));                             
                long diffMinutes = (diff - (1000*60*60*24*diffdays) - (1000*60*60*diffHours)) / (1000*60);  
                long diffSeconds =(diff - (1000*60*60*24*diffdays) - (1000*60*60*diffHours) -(1000*60*diffMinutes))/ (1000);                

                String out= "days:"+diffdays+  "hours:"+diffHours+  "Minutes:"+diffMinutes+  "seconds:"+diffSeconds;
                tv.setText(out);

                handler.postDelayed(this, 1000);
            }
        }, 1000); 
    }

}
like image 858
Sun Avatar asked Oct 21 '22 03:10

Sun


2 Answers

Or you can simple use Joda-Time library to do it

Interval interval = new Interval(startDate.getTime(), endDate.getTime());
Period period = interval.toPeriod();
String strDiff = "Year: " + period.getYears() + "/ Month: " + period.getMonths() + "/ Days: " + period.getDays() + "/ Hours: " + period.getHours() + "/ Minutes: " + period.getMinutes() + "/ Seconds: " + period.getSeconds();
like image 125
calvinfly Avatar answered Oct 23 '22 03:10

calvinfly


I have this code that works for me and is more simple:

public static String    dhmsDifference(Date d1, Date d2)
{
    long diff = d2.getTime() - d1.getTime();
    long diffSeconds = diff / 1000 % 60;
    long diffMinutes = diff / (60 * 1000) % 60;
    long diffHours = diff / (60 * 60 * 1000) % 24;
    long diffDays = diff / (24 * 60 * 60 * 1000);

    String difference=Long.toString(diffDays)+":"+String.format("%02d", diffHours)+":"+String.format("%02d", diffMinutes)+":"+String.format("%02d", diffSeconds);

    return difference;
}

It returns a String, but you can easily adapt it to your code I think.

For month and years, I'd use a Calendar (added it to my code):

Calendar cal1 = Calendar.getInstance();
Calendar cal2 = Calendar.getInstance();
cal1.setTime(d1);
cal2.setTime(d2);

    long months = cal2.get(Calendar.MONTH)-cal1.get(Calendar.MONTH);
    long years = cal2.get(Calendar.YEAR)-cal1.get(Calendar.YEAR);
    long weeks = cal2.get(Calendar.WEEK_OF_YEAR)-cal1.get(Calendar.WEEK_OF_YEAR);       

You'll need to add some magic for when the year changes, but that's the idea.

EDIT: Makes some tests with this more complete code please:

public static String    ymwdhmsDifference(Date d1, Date d2)
{
    long diff = d2.getTime() - d1.getTime();
    long diffSeconds = diff / 1000 % 60;
    long diffMinutes = diff / (60 * 1000) % 60;
    long diffHours = diff / (60 * 60 * 1000) % 24;
    long totalDiffDays = diff / (24 * 60 * 60 * 1000);
    long diffDays = totalDiffDays % 7;

    long diffWeeks = totalDiffDays/7;   // Full weeks are simply days / 7.
    diffDays = diffDays % 7;        // now we get the remaining days.

    Calendar cal1 = Calendar.getInstance();
    Calendar cal2 = Calendar.getInstance();
    cal1.setTime(d1);
    cal2.setTime(d2);


    long totalDiffYears = cal2.get(Calendar.YEAR)-cal1.get(Calendar.YEAR);
    long totalDiffMonths = Math.max(totalDiffYears*12+cal2.get(Calendar.MONTH)-cal1.get(Calendar.MONTH)-1,0);
    long diffYears = totalDiffMonths / 12;
    // remaining full months
    long diffMonths = totalDiffMonths % 12;

    // now we have to count how many weeks those full months represent, to substract them from the number of weeks...
    Calendar cal3 = Calendar.getInstance();
    cal3.setTime(d1);
    int month = cal1.get(Calendar.MONTH)+1, year = cal1.get(Calendar.YEAR), monthDays=0;
    for (int m=0;m<totalDiffMonths;m++) {
        cal3.set(Calendar.MONTH, month++);
        monthDays+=cal3.getActualMaximum(Calendar.DAY_OF_MONTH);
        if (month>=12) {
            month = 0;
            year++;
            cal3.set(Calendar.YEAR, year);
        }
    }
    diffWeeks-=monthDays/7;

    // Note that the number of weeks can be greater than 4 because they are part of non full months.

    String difference="Y: "+Long.toString(diffYears)+" M: "+Long.toString(diffMonths)+" W: "+Long.toString(diffWeeks)+" D: "+Long.toString(diffDays)+" H: "+String.format("%02d", diffHours)+" M: "+String.format("%02d", diffMinutes)+" S: "+String.format("%02d", diffSeconds);

    return difference;
}
like image 44
Wildcopper Avatar answered Oct 23 '22 02:10

Wildcopper