1

I write a countdown timer and i set time for that and when i show the time its set hour+1 how can i correct it here is my code

    SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");   
    future=dateFormat.parse("2018-09-25 15:00:00");   
    Date now=new Date();

    if (!now.after(future)) {
        long diff = future.getTime() - now.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);
    }

...... for example if it should count from 1day 13:00:00, it count from 14

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
Saeed8697
  • 21
  • 4
  • 1
    if you want a count down timer you should use CountDownTimer Class. for more info please refer to this: [link](https://stackoverflow.com/questions/10032003/how-to-make-a-countdown-timer-in-android) – Fahd Al-Qahtani Sep 16 '18 at 10:24
  • As an aside consider throwing away the long outmoded and notoriously troublesome `SimpleDateFormat` and friends, and adding [ThreeTenABP](https://github.com/JakeWharton/ThreeTenABP) to your Android project in order to use `java.time`, the modern Java date and time API. It is so much nicer to work with. – Ole V.V. Sep 16 '18 at 10:32
  • I cannot reproduce your issue. At 12:40 on September 16 in my time zone (Europe/Copenhagen), I got 9 days 2 hours, which seems to be the correct amount of time until 15:00 on September 25. My guess would be that it is a time zone issue. Does summer time (daylight saving time) end in your time zone before (or on) September 25? How *would* you want your timer to behave across summer time ending? – Ole V.V. Sep 16 '18 at 10:41
  • If I set my time zone to Asia/Tehran, for example, I seem to reproduce your issue, I get 1 hour more than one should expect from the clock times. The question is whether this isn’t correct, though, since there *is* 1 hour more in the night when the clocks are turned back. Summer time ends in Iran on September 22 this year. – Ole V.V. Sep 16 '18 at 10:49
  • thanks buddy i havent consider that in my code – Saeed8697 Sep 16 '18 at 11:29

1 Answers1

0

Hope this may help you All you have to do it convert time to millis.. and all you have to do is use timer. to update your time left in label. here is Example code to convert dateTimet to millis.

String myDate = "2014/10/29 18:10:45";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date date = sdf.parse(myDate);
long millis = date.getTime();


            CountDownTimer    countDownTimer = new CountDownTimer(millis , 1000) {
                        @Override
                        public void onTick(long millisUntilFinished) {

                            if (getActivity() != null && !getActivity().isFinishing()) {
                                expiryLabel.setText(getResources().getString(R.string.expiresIn,
                                        " " +formatTime(millisUntilFinished));
                            }
                        }

                        @Override
                        public void onFinish() {

                            if (getActivity() != null && !getActivity().isFinishing()) {

                            // do what ever you want
                            }
                        }
                    };
                    countDownTimer.start();

                }




 public static String formatTime(long millis) {
        String output = "00:00:00";
        long seconds = millis / 1000;
        long minutes = seconds / 60;
        long hours = minutes / 60;

        seconds = seconds % 60;
        minutes = minutes % 60;
        hours = hours % 60;

        String secondsD = String.valueOf(seconds);
        String minutesD = String.valueOf(minutes);
        String hoursD = String.valueOf(hours);

        if (seconds < 10)
            secondsD = "0" + seconds;
        if (minutes < 10)
            minutesD = "0" + minutes;
        if (hours < 10)
            hoursD = "0" + hours;

        output = hoursD + ":" + minutesD + ":" + secondsD;



        return output;
    }
Atif AbbAsi
  • 5,633
  • 7
  • 26
  • 47