比如,一个任务完成需要10分钟,让倒计时按秒来倒计时,怎么做?

解决方案 »

  1.   

    用java.util.Timer可以实现的。控制在10分钟然后处理你要的倒计时。
      

  2.   

    Timer就可以啊。
    我刚刚写了一个类似的代码段,不知道是不是lz所需要的。 final Action taskPerformer = new AbstractAction() { private static final long serialVersionUID = 1L; public void actionPerformed(ActionEvent evt) {
    Display.getDefault().syncExec(new Runnable() {
    public void run() {
                            // TODO };
    timer = new Timer(1000, taskPerformer);
    timer.start();
      

  3.   

    象这种倒计时  还用JAVA? 那也效率太低了吧
    用JS多好
      

  4.   

    非timer实现的倒计时public class 倒计时时钟 { //小时
    private int hours;

    //分钟
    private int min;

    //秒
    private int second;

    public 倒计时时钟(int hours,int min,int second) {
    this.hours = hours;
    this.min = min;
    this.second = second;
    } public int getHours() {
    return hours;
    } public void setHours(int hours) {
    this.hours = hours;
    } public int getMin() {
    return min;
    } public void setMin(int min) {
    this.min = min;
    } public int getSecond() {
    return second;
    } public void setSecond(int second) {
    this.second = second;
    }}
    public class 倒计时 implements Runnable{ private 倒计时时钟 clock;
    private long time;

    public 倒计时(倒计时时钟 clock) {
    this.clock = clock;
    //将时间换算成秒
    time = clock.getHours()*60*60+clock.getMin()*60+clock.getSecond();
    } public void run() {
    while(time >= 0) {
    try {
    Thread.sleep(1000);
    time -= 1;//时间减去一秒
    clock.setHours((int)time/(60*60));
    clock.setMin((int)(time/60)%60);
    clock.setSecond((int)time % 60);

    } catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }
    //return clock;
    }

    public 倒计时时钟 getTime() {
    return clock;
    }

    public static void main(String [] args) {
    倒计时时钟 clock = new 倒计时时钟(0,10,0);

    倒计时 jishi = new 倒计时(clock);
    显示 show = new 显示(jishi.getTime());
    //显示 show = new 显示();
    new Thread(show).start();
    new Thread(jishi).start();

    }
    } class 显示 implements Runnable {
    private 倒计时时钟 clock;
    public 显示(倒计时时钟 clock) {
    this.clock = clock;

    }

    public void run() {

    while(clock.getHours() != 0 || 
    clock.getMin() != 0 || 
    clock.getSecond() != 0) {
    try {
    System.out.println(String.format("%02d",clock.getHours())+
    ":"+String.format("%02d",clock.getMin())+
    ":"+String.format("%02d",clock.getSecond()));
    Thread.sleep(1000);
    } catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }

    }
    }
      

  5.   

    package com.leilei.www.dateDemo;
    //import java.sql.Timestamp;
    import java.text.DateFormat;
    //import java.text.ParseException;
    import java.text.SimpleDateFormat;
    //import java.util.Calendar;
    import java.util.Date;
    //import java.util.GregorianCalendar;
    public class DateUtil {
    public static final String DEFAULT_FORMAT = "yyyy-MM-dd";//定义一个常量

    // public static String getCurDate() {
    // Date date = new Date();
    // DateFormat format = new SimpleDateFormat(DEFAULT_FORMAT);
    // return format.format(date);
    //}
    public static String getCurDate(String viewFormat){
    Date date = new Date();
    DateFormat format = new SimpleDateFormat(viewFormat);
    return  format.format(date);
    }
    // public static String getDateString(Date date, String viewFormat) {
    // DateFormat format = new SimpleDateFormat(viewFormat);
    // return format.format(date);
    // }
    // public static String getYearByDate(Date date) {
    // DateFormat format = new SimpleDateFormat("yyyy");
    // return format.format(date);
    // }
    // public static String getHourByDate(Date date) {
    // DateFormat format = new SimpleDateFormat("HH");
    // return format.format(date);
    // }
    // public static String getMinuteByDate(Date date) {
    // DateFormat format = new SimpleDateFormat("mm");
    // return format.format(date);
    // }
    // public static String getCallDate() {
    // String viewFormat = "yyyyMMddHHmmss";
    // return getCurDate(viewFormat);
    // }
    //  public static String getSnByDate() {
    // return getCallDate();
    // }
    // public static String getCallDate(Date date){
    // String viewFormat = "yyyyMMddHHmmss";
    // return getDateString(date, viewFormat);
    // }
    // public static String getYMDDate(Date date) {
    // String viewFormat = "yyyy-MM-dd";
    // return getDateString(date, viewFormat);
    // }
    // public static String getDate(Date date) {
    // if(date == null) {
    // return "";
    // }
    // DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    // return format.format(date);
    // }
    // public static Date getYMDateByStr(String str) {
    // return getYMDateByStr(str, "yyyy-MM-dd");
    // }
    // public static Date getYMDHMSateByStr(String str) {
    // return getYMDateByStr(str, "yyyy-MM-dd HH:mm:ss");
    // }
    // public static Date getYMDateByStr(String str, String par) {
    //     if(str ==null){
    //         return null;
    //     }
    //     DateFormat format = new SimpleDateFormat(par);
    //     Date date = new Date();
    //            try {                
    //                date=(Date)format.parse(str);
    //            } catch (ParseException e) {
    //                // TODO Auto-generated catch block
    //                e.printStackTrace();
    //            }
    //         return date;
    // }
    // public static Date getDateByStr(String str) throws ParseException {
    // if(str == null) {
    // return null;
    // }
    // DateFormat date = DateFormat.getDateTimeInstance();
    // return date.parse(str);
    // }
    // public static Date GetDateNoStict(String dateStr) throws ParseException {
    // dateStr = dateStr + " 00:00:00";
    // return getDateByStr(dateStr);
    // }
    //
    // public static Timestamp getTimeStamp(Date date) {
    // Timestamp stamp = new Timestamp(date.getTime());
    // return stamp;
    // }
    //
    // public static String getCurrMillis() {
    // Calendar cal = new GregorianCalendar();
    // return String.valueOf(cal.getTimeInMillis());
    // }
    //
    // public static long getCurrMillisLong() {
    // return Calendar.getInstance().getTimeInMillis();
    // }
    //
    // public static int getAge(Date birthdayDate) {
    // int age=0;
    // if(!birthdayDate.equals(null)){
    // long currDateMillis = DateUtil.getCurrMillisLong();
    // long beginDateMills = DateUtil.getTimeStamp(birthdayDate).getTime();
    // long  decrease       = (currDateMillis-beginDateMills)/(24*3600*1000)+1;
    // decrease =decrease/365;
    // if(decrease<0){
    // age = 0;
    // }else{
    // age  =  new Long(decrease).intValue();
    //     }
    // }
    // return age;
    // }
    public static void main(String[] args) throws Exception {// Date date = new Date();
    // DateFormat nowTime = new SimpleDateFormat("今天是:yyyy年MM月dd日");
    // System.out.println(nowTime.format(date));
    //          
    //          Date date1 = new Date();
    //          System.out.println(date1.toLocaleString());
             
             getCurDate("23");
    }
    }