如何将Date型数据转化成long型数据?

sxchun 2001-09-07 02:38:04
如何将Date型数据转化成long型数据?如果不能转的话,那么要计算两个时间的差值应该怎么办?
...全文
257 3 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
skyyoung 2001-09-08
  • 打赏
  • 举报
回复
Compute days between 2 dates
import java.util.*;
public class TestDate {
public static void main(String args[]){
/*
ex .
java TestDate 1999 0 1 1999 8 1
to get days between 1999-01-01 and 1999-09-01
(remember months are zero-based...)
*/

TestDate a = new TestDate(args);
}

TestDate(String args[]) {
Calendar c1 = new GregorianCalendar();;
Calendar c2 = new GregorianCalendar();;

// need more error checking here...
c1.set(Integer.parseInt(args[0]),
Integer.parseInt(args[1]) ,
Integer.parseInt(args[2]), 0, 0, 0);
c2.set(Integer.parseInt(args[3]),
Integer.parseInt(args[4]) ,
Integer.parseInt(args[5]), 0, 0, 0);

System.out.println
(daysBetween(c1.getTime(),c2.getTime()) +
" day(s) between " + args[0] + "-" + args[1] + "-" + args[2] +
" and " + args[3] + "-" + args[4] + "-" + args[5]);
}

static final long ONE_HOUR = 60 * 60 * 1000L;
public long daysBetween(Date d1, Date d2){
return ( (d2.getTime() - d1.getTime() + ONE_HOUR) /
(ONE_HOUR * 24));
}
}

byfree 2001-09-07
  • 打赏
  • 举报
回复
用compareTo(Date anotherDate)方法可得到差。
要转化也是可以的。
vdragon 2001-09-07
  • 打赏
  • 举报
回复
getTime
public long getTime()Returns the number of milliseconds since January 1, 1970, 00:00:00 GMT represented by this Date object.
Returns:
the number of milliseconds since January 1, 1970, 00:00:00 GMT represented by this date.

可以得到现在的时间距离1970年1月1日的毫秒数

81,122

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧