CountDownTimer比较两个日期

时间:2017-10-20 19:05:45

标签: android countdowntimer

我通过这个方法写了 我将两个日期比较在一起 我写了这个方法 我将两个日期比较在一起 两个日期之间的差异如下

 return day + " days " + hh + " hour " + mm + " min";

现在我想在CountDownTimer中显示日,小时和分钟的数量 我需要这方面的指导

我写的代码却错了:

int duration=81200000; 
new CountDownTimer(duration, 1000) {

    public void onTick(long millisUntilFinished) {
        long secondsInMilli = 1000;
        long minutesInMilli = secondsInMilli * 60;
        long hoursInMilli = minutesInMilli * 60;

        long elapsedHours = millisUntilFinished / hoursInMilli;
        millisUntilFinished = millisUntilFinished % hoursInMilli;

        long elapsedMinutes = millisUntilFinished / minutesInMilli;
        millisUntilFinished = millisUntilFinished % minutesInMilli;

        long elapsedSeconds = millisUntilFinished / secondsInMilli;

        String yy = String.format("%02d:%02d:%02d", elapsedHours, elapsedMinutes , elapsedSeconds);
        timer.setText(yy);
    }

    public void onFinish() {

        timer.setText("00:00");
    }
}.start();

2 个答案:

答案 0 :(得分:1)

我用两种不同的方法做了同样的事情,一种是自定义计算,另一种是尝试这种方法,另一种是TimeUnit。并按照我的Date and Time Complete Example

 public static final String SAMPLE_DATE_FORMAT = "yyyy-MM-dd";//;"DD/MM/YY";//"yyyy-MM-dd";

//minutes:Hours:days
    public static String getTimeSecMinutesHoursDays(String dateString) throws ParseException {


        SimpleDateFormat endDateFormat = new SimpleDateFormat(Constants.DateAndMonth.SAMPLE_DATE_TIME_FORMAT);
        String currentDateAndTime = endDateFormat.format(new Date());
        Date endDate = endDateFormat.parse(currentDateAndTime);

        SimpleDateFormat startDateFormat = new SimpleDateFormat(Constants.DateAndMonth.SAMPLE_DATE_TIME_FORMAT);
        Date startDate = startDateFormat.parse(dateString);
        // format the java.util.Date object to the desired format
      //  String startDateString = new SimpleDateFormat(Constants.DateAndMonth.SAMPLE_DATE_TIME_FORMAT).format(startDate);
//        long startMili = Tools.getMiliSecondsFromDateANDTIME(startDateString);
//        long endMili = Tools.getMiliSecondsFromDateANDTIME(currentDateAndTime);
        long difference = endDate.getTime() - startDate.getTime();

//        String[] separated = minutesHoursDays.split(":");
//        long seconds = TimeUnit.MILLISECONDS.toSeconds(difference);

        String minutesHoursDays=calculateTime(difference); //minutes:Hours:days

        return minutesHoursDays;

    }



 public static String calculateTime(long milis) {

                String time="";
                int day = (int) (m

ilis / (1000 * 60 * 60 * 24));
            int hours = (int) ((milis - (1000 * 60 * 60 * 24 * day)) / (1000 * 60 * 60));
            int minute = (int) (milis - (1000 * 60 * 60 * 24 * day) - (1000 * 60 * 60 * hours)) / (1000 * 60);
            int second = (int) ((milis / 1000) % 60);

            if(day>0){
                if(day==1){
                    time =day+" day";
                }else{
                    time =day+" days";
                }
            }else if(day<1 && hours>0){

                if(hours==1){
                    time =hours +" hr";
                }else{
                    time =hours +" hrs";
                }
            }else if(hours<1 && minute>0){
                if(minute==1){
                    time =minute +" min";
                }else{
                    time =minute +" mins";
                }
            }else if(minute<1 && second>0){

                if(second==1){
                    time =second +" sec";
                }else{
                    time =second +" secs";
                }
            }else {

            if (second <= 0) {
                time = "0 sec";
            }
        }

或尝试使用TimeUnit

        try {
            int timeUnitDay = (int) TimeUnit.MILLISECONDS.toDays(milis);
            long timeUnitHours = TimeUnit.MILLISECONDS.toHours(milis);
            long timeUnitMinutes = TimeUnit.MILLISECONDS.toMinutes(milis);
            long timeUnitSeconds = TimeUnit.MILLISECONDS.toSeconds(milis);
        } catch (Exception e) {
            e.printStackTrace();
        }


        //  System.out.println("Day " + day + " Hour " + hours + " Minute " + minute + " Seconds " + second);

        // time = "Day " + day + " : Hour " + hours + " : Minute " + minute + " : Seconds " + second;
        return time;
    }

答案 1 :(得分:0)

可以使用Handler实现计时器。这是代码:

public android.os.Handler handler;
public Runnable runnable;

 handler = new android.os.Handler();
    runnable = new Runnable() {
        @Override
        public void run() {
            handler.postDelayed(this, 1000);
            try {
                SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
                Date eventdate = dateFormat.parse("Your date in yyyy-mm-dd");
                Date currentdate = new Date(); //2nd date taken as current date

                if(!currentdate.after(eventdate))
                {
                    long diff = eventdate.getTime() - currentdate.getTime();
                    long days = diff / (24 * 60 * 60 * 1000);
                    diff -= days * (24 * 60 * 60 * 1000);
                    long hours = diff / (60 * 60 * 1000);
                    diff -= hours * (60 * 60 * 1000);
                    long minutes = diff / (60 * 1000);
                    diff -= minutes * (60 * 1000);
                    long seconds = diff / 1000;

                    String days = String.format("%02d",days);
                    String hours = String.format("%02d",hours);
                    String mins = String.format("%02d",minutes));
                    String seconds = String.format("%02d",seconds));

                }
 }
            catch (Exception e)
            {
                e.printStackTrace();
            }
        }
    };
    handler.postDelayed(runnable,0);
相关问题