制作一个倒数计时器

时间:2013-12-05 04:45:19

标签: java android

我想从android当前时间开始新年的倒数计时器......

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

Calendar today = Calendar.getInstance();
long diff =  thatDay.getTimeInMillis() - today.getTimeInMillis(); 

现在如何将此差异转换为日,小时,分钟,秒格式?请帮忙

6 个答案:

答案 0 :(得分:5)

以下代码

import java.util.Date;
import java.util.Calendar;

public class cal {
    public static int SECONDS_IN_A_DAY = 24 * 60 * 60;
    public static void main(String[] args) {
        Calendar thatDay = Calendar.getInstance();
        thatDay.setTime(new Date(0)); /* reset */
        thatDay.set(Calendar.DAY_OF_MONTH,1);
        thatDay.set(Calendar.MONTH,0); // 0-11 so 1 less
        thatDay.set(Calendar.YEAR, 2014);

        Calendar today = Calendar.getInstance();
        long diff =  thatDay.getTimeInMillis() - today.getTimeInMillis(); 
        long diffSec = diff / 1000;

        long days = diffSec / SECONDS_IN_A_DAY;
        long secondsDay = diffSec % SECONDS_IN_A_DAY;
        long seconds = secondsDay % 60;
        long minutes = (secondsDay / 60) % 60;
        long hours = (secondsDay / 3600); // % 24 not needed

        System.out.printf("%d days, %d hours, %d minutes and %d seconds\n", days, hours, minutes, seconds);
    }
}

产生

27 days, 17 hours, 18 minutes and 2 seconds
27 days, 17 hours, 18 minutes and 1 seconds
27 days, 17 hours, 18 minutes and 0 seconds
27 days, 17 hours, 17 minutes and 59 seconds
27 days, 17 hours, 17 minutes and 58 seconds
27 days, 17 hours, 17 minutes and 57 seconds
27 days, 17 hours, 17 minutes and 56 seconds
27 days, 17 hours, 17 minutes and 55 seconds
27 days, 17 hours, 17 minutes and 54 seconds
27 days, 17 hours, 17 minutes and 53 seconds

希望这是你想要的

答案 1 :(得分:1)

试试这个

 String formatStr = "yyyy-MM-dd HH:mm:ss";

 SimpleDateFormat sdf1 = new SimpleDateFormat(formatStr);
TimeZone obj = TimeZone.getTimeZone("CST");

sdf1.setTimeZone(obj);
Date date = sdf1.parse("your time"); // here put your time zone
long millis = date.getTime();

long currentTimeInMili = new Date().getTime();
MyCount counter = new MyCount(millis - currentTimeInMili, 1 * 1000);
  counter.start();



 class MyCount extends CountDownTimer {
    public MyCount(long millisInFuture, long countDownInterval) {
        super(millisInFuture, countDownInterval);
    }// MyCount

    public void onPause() {
        onPause();
    }// finish

    public void onTick(long millisUntilFinished) {
        dynamicMatchDesTextView.setText(""+ formatTime(millisUntilFinished)+ " till match start");

    }// on tick

    @Override
    public void onFinish() {
        onStop();

    }// finish
}

public String formatTime(long millis) {

    String output = "00:00";
    try {
        long seconds = millis / 1000;
        long minutes = seconds / 60;
        long hours = seconds / 3600;
        long days = seconds / (3600 * 24);

        seconds = seconds % 60;
        minutes = minutes % 60;
        hours = hours % 24;
        days = days % 30;

        String sec = String.valueOf(seconds);
        String min = String.valueOf(minutes);
        String hur = String.valueOf(hours);
        String day = String.valueOf(days);

        if (seconds < 10)
            sec = "0" + seconds;
        if (minutes < 10)
            min = "0" + minutes;
        if (hours < 10)
            hur = "0" + hours;
        if (days < 10)
            day = "0" + days;

        output = day + "D " + hur + "H " + min + "M " + sec + "S";
    } catch (Exception e) {
        e.printStackTrace();
    }
    return output;
}

答案 2 :(得分:0)

        Calendar thatDay = Calendar.getInstance();
        thatDay.set(Calendar.DAY_OF_MONTH,1);
        thatDay.set(Calendar.MONTH,0); // 0-11 so 1 less
        thatDay.set(Calendar.YEAR, 2014);
        thatDay.set(Calendar.HOUR_OF_DAY, 0);
        thatDay.set(Calendar.MINUTE, 0);
        System.out.println(thatDay.getTime());

        Calendar today = Calendar.getInstance();
        System.out.println(today.getTime());

        int diffInYears = thatDay.get(Calendar.YEAR) - today.get(Calendar.YEAR);
        int diffInDays = ((diffInYears*365) + thatDay.get(Calendar.DAY_OF_YEAR)) - today.get(Calendar.DAY_OF_YEAR);
        diffInDays--; // Decrementing because I will be taking 1 day (which is 24 hours) to do below hours calculation
        int diffInHours = thatDay.get(Calendar.HOUR_OF_DAY)+23 - today.get(Calendar.HOUR_OF_DAY); // Used 23 hours instead of 24 hours because I am using 1 hour (60 minutes) to do below calculation
        int diffInMins = thatDay.get(Calendar.MINUTE)+60 - today.get(Calendar.MINUTE);
        System.out.println(diffInDays + " days " + diffInHours + " hours " + diffInMins +" minutes remaining");

答案 3 :(得分:0)

您可以使用:

public String formatDate( long milliSec ) {
        long diffSec = milliSec / (1000);
        long diffMinutes = milliSec / (60 * 1000) % 60;
        long diffHours = milliSec / (60 * 60 * 1000) % 24;
        long diffDays = milliSec / (24 * 60 * 60 * 1000);
        StringBuilder date = new StringBuilder();
        if(diffDays != 0){
            date.append(diffDays).append("D ");
        }
        if(diffHours != 0){
            int length = (int)(Math.log10(diffHours)+1);
            if(length == 1){
                String s = "0"+diffHours;
                date.append(s).append(":");
            }else{
                date.append(diffHours).append(":");
            }
        }else{
            date.append("00").append(":");
        }
        if(diffMinutes != 0 ){
            int length = (int)(Math.log10(diffMinutes)+1);
            if(length == 1){
                String s = "0"+diffMinutes;
                date.append(s).append(":");
            }else{
                date.append(diffMinutes).append(":");;
            }
        }else{
            date.append("00");
        }
        if(diffSec != 0){
           int length = (int)(Math.log10(diffSec)+1);
            if(length == 1){
                String s = "0"+diffSec;
                date.append(s);
            }else{
                date.append(diffSec);
            }
        }else{
            date.append("00");
        }
        return date.toString();
    }

答案 4 :(得分:0)

int SECONDS = 1000;
int MINUTE = SECONDS * 60 ;
int HOUR =  MINUTE * 60;
int DAY = HOUR * 24;

我将从差异开始并按DAY划分,然后按DAY进行模块化以获得 剩下的部分。

repeat for HOUR, and MINUTE and SECOND

答案 5 :(得分:-2)

SimpleDateFormat:This will accomplish your goal quite easily.

只需执行以下操作即可...

SimpleDateFormat fmt = new SimpleDateFormat... (read the java docs)
fmt.format(your string)

无需重新发明轮子并使程序更大......此代码已存在于Java中。

编辑:由于OP无法读取.. S毫秒数978