Java 做一个倒计时器

Java 做一个倒计时器,java,android,Java,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 = thatDa

我想在安卓系统中从当前时间开始制作一个新年倒计时

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(); 
现在,如何将此diff转换为日、时、分、秒格式?请帮助

简化格式:

        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");
只需执行以下操作

SimpleDateFormat fmt = new SimpleDateFormat... (read the java docs)
fmt.format(your string)
无需重新发明轮子,使您的程序更大。。。此代码已经存在于Java中

编辑:因为OP无法读取。。 S毫秒数978

简化格式:

只需执行以下操作

SimpleDateFormat fmt = new SimpleDateFormat... (read the java docs)
fmt.format(your string)
无需重新发明轮子,使您的程序更大。。。此代码已经存在于Java中

编辑:因为OP无法读取。。 S毫秒数978

您可以使用:

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();
    }
您可以使用以下选项:

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();
    }
我会从diff开始,按天除,然后按天取模,得到 下一部分的剩余部分

repeat for HOUR, and MINUTE and SECOND
我会从diff开始,按天除,然后按天取模,得到 下一部分的剩余部分

repeat for HOUR, and MINUTE and SECOND
试试这个

 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;
}
字符串格式str=“yyyy-MM-dd HH:MM:ss”; SimpleDataFormat sdf1=新的SimpleDataFormat(formatStr); 时区obj=时区.getTimeZone(“CST”); sdf1.设置时区(obj); Date Date=sdf1.parse(“您的时间”);//这是你的时区 long millis=date.getTime(); long currentTimeInMili=new Date().getTime(); MyCount计数器=新的MyCount(毫秒-CurrentTimeInMilli,1*1000); counter.start(); 类MyCount扩展了倒计时{ 公共MyCount(长百万未来,长倒计时间隔){ 超级(毫秒未来,倒计时间隔); }//我的帐户 公共无效暂停(){ onPause(); }//完成 公共void onTick(长毫秒未完成){ dynamicMatchDesTextView.setText(“+formatTime(毫秒直至完成)+”直到匹配开始”); }//滴答声 @凌驾 公共无效onFinish(){ 顶部(); }//完成 } 公共字符串格式化时间(长毫秒){ 字符串输出=“00:00”; 试一试{ 长秒=毫秒/1000; 长分钟=秒/60; 长时间=秒/3600; 长天数=秒/(3600*24); 秒=秒%60; 分钟=分钟%60; 小时数=小时数%24; 天数=天数%30; 字符串秒=字符串的值(秒); String min=String.valueOf(分钟); String hur=String.valueOf(小时); String day=String.valueOf(天); 如果(秒<10) sec=“0”+秒; 如果(分钟<10) min=“0”+分钟; 如果(小时<10) hur=“0”+小时; 如果(天数<10天) 天=“0”+天; 输出=日+D+hur+H+min+M+sec+S; }捕获(例外e){ e、 printStackTrace(); } 返回输出; } 试试这个

 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;
}
字符串格式str=“yyyy-MM-dd HH:MM:ss”; SimpleDataFormat sdf1=新的SimpleDataFormat(formatStr); 时区obj=时区.getTimeZone(“CST”); sdf1.设置时区(obj); Date Date=sdf1.parse(“您的时间”);//这是你的时区 long millis=date.getTime(); long currentTimeInMili=new Date().getTime(); MyCount计数器=新的MyCount(毫秒-CurrentTimeInMilli,1*1000); counter.start(); 类MyCount扩展了倒计时{ 公共MyCount(长百万未来,长倒计时间隔){ 超级(毫秒未来,倒计时间隔); }//我的帐户 公共无效暂停(){ onPause(); }//完成 公共void onTick(长毫秒未完成){ dynamicMatchDesTextView.setText(“+formatTime(毫秒直至完成)+”直到匹配开始”); }//滴答声 @凌驾 公共无效onFinish(){ 顶部(); }//完成 } 公共字符串格式化时间(长毫秒){ 字符串输出=“00:00”; 试一试{ 长秒=毫秒/1000; 长分钟=秒/60; 长时间=秒/3600; 长天数=秒/(3600*24); 秒=秒%60; 分钟=分钟%60; 小时数=小时数%24; 天数=天数%30; 字符串秒=字符串的值(秒); String min=String.valueOf(分钟); String hur=String.valueOf(小时); String day=String.valueOf(天); 如果(秒<10) sec=“0”+秒; 如果(分钟<10) min=“0”+分钟; 如果(小时<10) hur=“0”+小时; 如果(天数<10天) 天=“0”+天; 输出=日+D+hur+H+min+M+sec+S; }捕获(例外e){ e、 printStackTrace(); } 返回输出; } 以下代码

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
希望这是您想要的

以下代码

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

希望这就是你想要的

帖子不要只是用链接来回答。如果链接断了,答案就没有意义了。也许Oracle会卖给微软。请解释SimpleDateFormat如何将此差异转换为日、时、分、秒格式?不,仍然不买。没有需要毫秒的SimpleDataFormat构造函数。请停止文盲,阅读java文档。“S”是毫秒的指示器。Post不只是用链接回答。如果链接断了,答案就没有意义了。也许Oracle会卖给微软。请解释SimpleDateFormat如何将此差异转换为日、时、分、秒格式?不,仍然不买。没有需要毫秒的SimpleDataFormat构造函数。请停止文盲,阅读java文档。“S”是以毫秒为单位的指示器。