求一天内的任何时间至晚上23:59:59秒的剩余时间. 返回类型要int 返回对应的int 使用Date date = new Date(int) 不会出现误差.. 求解. 自己写的有一个 没有计算年份 只计算了时分秒. 返回的时分秒是没问题的. 但set到的地方 使用date转换后在当前的年份上加了12年..
private static int resultTime(SimpleDateFormat simpleDateFormat) throws ParseException {
String lastTime = "23:59:59";
Date date = simpleDateFormat.parse(lastTime);
long result = date.getTime() - new Date().getTime();
return (int) result;
}
求改下.. 谢谢

解决方案 »

  1.   

    求一天内的任何时间至晚上23:59:59秒的剩余时间
    这个时间是秒数还是毫秒数?首先,这应该是一个线段返回值为int要求,使用Date date = new Date(int) 不会出现误差..
    Date 是一个具体的时间哪一年那一天几时几分几秒几毫秒,是一个
    一个怎么能和一个线段对应上呢?
    new Date(int),这个int是
    1970 年 1 月 1 日 00:00:00 GMT
    为起点加上int毫秒后这个的时间
      

  2.   

    引用 1 楼 sunyiz 的回复:求一天内的任何时间至晚上23:59:59秒的剩余时间
    这个时间是秒数还是毫秒数?首先,这应该是一个线段返回值为int要求,使用Date date = new Date(int) 不会出现误差..
    Date 是一个具体的时间哪一年那一天几时几分几秒几毫秒,是一个点
    一个点怎么能和一个线段对应上呢?
    new Date(int),这个int是……
    [/Quote]
      

  3.   

    是同一天么? 另:好好看看Calendar.java
      

  4.   

    如果我没理解错的话,这个代码应该是你想要的SimpleDateFormat f=new SimpleDateFormat("hh:mm:ss");
    Date d=null;
    try {
    d = f.parse("17:00:00");
    } catch (ParseException e) {
    e.printStackTrace();
    }

    Calendar cc=Calendar.getInstance();
    cc.set(Calendar.HOUR, d.getHours());
    cc.set(Calendar.MINUTE, d.getMinutes());
    cc.set(Calendar.SECOND, d.getSeconds());
    cc.getTime().getTime();

    Calendar c=Calendar.getInstance();
    c.set(Calendar.HOUR, 23);
    c.set(Calendar.MINUTE, 59);
    c.set(Calendar.SECOND, 59);
    c.getTime().getTime(); long l=c.getTime().getTime()-cc.getTime().getTime();

    System.out.println(l);
      

  5.   


    咱俩实现的结果差不多.. 时分秒可以对应上. 但set到cookie后的日期就又不对了. 这个是采用以上函数得到的结果
    Thu, 05-May-2022 09:12:31 GMT 想要实现的就是年月日不变. 只计算时间... 求解..