-1

我一直在研究实时代码,并借助我在互联网上找到的一些代码,我设法找到了一些可行的东西。

我从数据库中获取日期并将其拆分并放入毫秒内,这就是我找到的代码接管的地方。

现在剩下的都很完美,除了那些日子,它似乎总是 30 多天,但还剩下多少天??

这是代码:

        GregorianCalendar endDate = new GregorianCalendar(Integer.parseInt(separated[0]), Integer.parseInt(separated[1]),  Integer.parseInt(separated[2]), Integer.parseInt(Time[0]), Integer.parseInt(Time[1]), 00);
    GregorianCalendar startDate = new GregorianCalendar(year, month, day, hours, minutes, seconds);

    long diffInMs = endDate.getTimeInMillis() - startDate.getTimeInMillis();


    new CountDownTimer(diffInMs, 1000) {

         public void onTick(long millisUntilFinished) { 
             int seconds = (int)(millisUntilFinished / 1000) % 60 ;
             int minutes = (int)((millisUntilFinished / (1000*60)) % 60);
             int hours = (int)((millisUntilFinished / (1000*60*60)) % 24);
             int days = (int)((millisUntilFinished / (1000*60*60*24)) % 365);
             //int years = (int)(millisUntilFinished / 1000*60*60*24*365);

             ArrayList<String> timeArray = new ArrayList<String>();

    //               if(years > 0)   
    //                   timeArray.add(String.valueOf(years)   + "y");

             if(days > 0)    
                 timeArray.add(String.valueOf(days) + "d");

             if(hours>0)   
                 timeArray.add(String.valueOf(hours) + "h");

             if(minutes>0) 
                 timeArray.add(String.valueOf(minutes) + "min");

             if(seconds>0) 
                 timeArray.add(String.valueOf(seconds) + "sec");

             String time = "";
             for (int i = 0; i < timeArray.size(); i++) 
             {
                 time = time + timeArray.get(i);
                 if (i != timeArray.size() - 1)
                     time = time + ", ";
             }

             if (time == "")
               time = "0 sec";

             Countdown.setText("Database Lock: " + time );


         }

现在我不明白为什么日子会出错?

关于什么可能是错的任何想法?

谢谢,克里斯

4

1 回答 1

1

如果你的意思是花几天、一小时、几分钟、一秒的时间,这就是代码对我有用。

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 DateDiff{ 

  public static void main(String[] args){

  Date date = null;
  Date date1 = new Date();
  System.out.println(date1.toString());


  try { 
  String str_date="Tue, 25 Jun 2012 05:43:27";
  DateFormat formatter ;
  //Date date ;
  formatter = new SimpleDateFormat("EEE,dd MMM yy HH:mm:ss");
  date = (Date)formatter.parse(str_date); 
  System.out.println("Today is " +date );
  } catch (ParseException e)
   {

     System.out.println("Exception :"+e); 

  } 

  long diff = date1.getTime() - date.getTime();
  long seconds = diff / 1000;
  long minutes = seconds / 60;
  long hours = minutes / 60;
  long days = hours / 24;


       System.out.println("Time in seconds: " + seconds
      + " seconds.");
       System.out.println("Time in minutes: " + minutes
     + " minutes.");
       System.out.println("Time in hours: " + hours
     + " hours.");
       System.out.println("Time in days: " +days
     + " days.");



  }
}
于 2012-06-29T11:49:47.860 回答