Date 类型有个getTime()的方法,返回类型是long/// 豪秒数这样我们可以判断大小, 还有计算时间,,1天是1000*60*60*24豪秒:)

解决方案 »

  1.   

    肯定必须先把这两个时间的格式统一后,采用方法来比较实现!下面是时间比较的js。可以看一看!!<html><head>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
    <meta name="GENERATOR" content="Microsoft FrontPage 4.0">
    <meta name="ProgId" content="FrontPage.Editor.Document">
    <title>You are&nbsp; from June 21</title>
    </head><body onLoad="countdown()">
    <!-- --><SCRIPT><!--//    Script Editor:   Howard Chen
    //    Browser Compatible for this script: IE 2.0 or Higher
    //                                        Netscape 2.0 or Higher
    //    Get more JavaScripts at http://www.geocities.com/SiliconValley/Network/7432/
    //    Bug report to [email protected]
    //    This script is free as long as you keep its creditsvar theDay = new Date("July 21, 2004")  //The day has to be in the format Month Day, Yearvar DayTill     //The string that is going to put all numbers together and make sense.function countdown(){var today = new Date()    //Create an Date Object that contains today's date.var second = Math.floor((theDay.getTime() - today.getTime())/1000)var minute = Math.floor(second/60)  //Devide "second" into 60 to get the minutevar hour = Math.floor(minute/60)  //Devide "minute" into 60 to get the hourvar day = Math.floor(hour/24)   //Devide "hour" into 60 to get the dayCDay= day     //Correct dayCHour= hour % 24    //Correct hour, after devide into 24, the remainder deposits here.CMinute= minute % 60    //Correct minute, after devide into 60, the remainder deposits here.CSecond= second % 60    //Correct second, after devide into 60, the remainder deposits here.DayTill =  CDay + " days, " + CHour + " hours, " + CMinute + " minutes and " + CSecond + " seconds "//Rewrite the string to the correct information.document.clock.countdown.value = DayTill //Make the particular form chart become "Daytill"var counter = setTimeout("countdown()", 1000)}//--></SCRIPT><!-- -->    <! You must have countdown() here to run the script --><!-- --><FORM NAME = "clock">You are <INPUT TYPE="TEXT" NAME= "countdown" SIZE= "45"> from June 21, 2004.</FORM><!-- -->
    </body></html>
      

  2.   

    把2004-6-10 10:10:10 转换成Date,然后用getTime()取得它的毫秒数,就可以比较了。这里有个例子:
    import java.util.Date;
    import java.text.SimpleDateFormat;
    import java.text.ParseException;public class a{
    /**将一个表示时间的字符串转换成Date
    *@param strTime --一个表示时间的字符串
    *@return date
    */
    public static Date Convert(String strTime){
    String format = "yyyy-MM-dd mm:hh:ss";
    Date date = null;

    try{
    SimpleDateFormat tf = new SimpleDateFormat(format);
    date = tf.parse(strTime);
    }catch(NullPointerException e){
    System.out.println("the pattern is null.");
    }catch(IllegalArgumentException ex){
    System.out.println("the pattern is invalid.");
    }catch(ParseException e){
    System.out.println("time format error.");
    }

    return date;
    }

    public static void main(String[] args){
    String time = "2004-6-10 10:10:10";
    Date date = Convert(time);
    long time1 = date.getTime();
    System.out.println(time1);
    }
    }
      

  3.   

    楼上彻底帮我解惑了,马上给分。最后提一个问题如果反过来把1087349661转换成yyyy-MM-dd mm:hh:ss的格式,是不是就需要计算每年的天数了,有没有经典的算法?
      

  4.   

    获得差值使用getTime() long type ,除1000*24*60*60