0

我有一个倒计时到指定日期的小部件。例如: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); 
         }
4

1 回答 1

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;
    }
于 2012-12-08T11:13:09.273 回答