倒计时的小工具

时间:2012-12-08 10:48:40

标签: android

我有一个小部件,倒计时为指定日期。例如:12月10日是2天。 我想表明还剩多少小时甚至几分钟。怎么做?请帮帮我。

代码:

         @Override
         public void run() {
            Date date1 = new Date();
            Calendar calendar = new GregorianCalendar(2012, 11,11);
            long days = (((calendar.getTimeInMillis()- date1.getTime())/1000))/86400;
                 remoteViews.setTextViewText(R.id.xmas,""+ days);
                 appWidgetManager.updateAppWidget(thisWidget, remoteViews); 
         }

1 个答案:

答案 0 :(得分:0)

您可以通过多种不同方式完成此操作。这是1:

long theTimeYourCountingTo = 134264575674567;
long currentTime = System.currentTimeMillis();

long timeleft = theTimeYourCountingTo  - currentTime;

String formatteedTimeLeft = formatTime(timeleft);

private String formatTime(long millis) {
          String output = "";
          long seconds = millis / 1000;
          long minutes = seconds / 60;
          long hours = minutes / 60;
          long days = hours / 24;
          seconds = seconds % 60;
          minutes = minutes % 60;
          hours = hours % 24;

          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;
          }

          if( days > 0 ){
              output = days +"d ";
          }
              output += hoursD + "h " + minutesD + "m " + secondsD + "s";

          return output;
    }