怎么在java里获取带有毫秒的时间

1.long java.util.Date.getTime()Returns the number of milliseconds since January 1, 1970, 00:00:00 GMT represented by this Date object.如上JDK文档说,在Date对象上用getTime()获得自1970年1月1日以来的毫秒数。2.System.currentTimeMillis(); 这个方法获取当前时间的毫秒数。3.以下实例代...
怎么在java里获取带有毫秒的时间
1.
long java.util.Date.getTime()
Returns the number of milliseconds since January 1, 1970, 00:00:00 GMT represented by this Date object.
如上JDK文档说,在Date对象上用getTime()获得自1970年1月1日以来的毫秒数。
2.
System.currentTimeMillis(); 这个方法获取当前时间的毫秒数。
3.
以下实例代码把通过毫秒数相减算的目前距2014-10-01 00:00:00的天数。
public class Test {public static void main(String[] args) throws ParseException {SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");String start="2014-10-01 00:00:00";//得到毫秒数long timeStart=sdf.parse(start).getTime();long justNow =System.currentTimeMillis();//两个日期想减得到天数long dayCount= (justNow-timeStart)/(24*3600*1000);System.out.println(dayCount);}}输出
25
2014-10-26
用Date的getTime方法来获取
public static void main(String[] args) {
// TODO Auto-generated method stub
Date dt= new Date();
Long time= dt.getTime();//这就是距离1970年1月1日0点0分0秒的毫秒数
System.out.println(System.currentTimeMillis());//与上面的相同
}2015-12-09
new java.util.Date().getTime();2014-10-26
Date date = new Date();
Long tmm = date.getTime();2015-09-15
import java.text.SimpleDateFormat;
import java.util.Date;

public class Test {

public static void main(String[] args) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SS");
String res = sdf.format(new Date());
System.out.println(res);
}
}2015-10-16
mengvlog 阅读 48 次 更新于 2025-10-28 21:36:59 我来答关注问题0
檬味博客在线解答立即免费咨询

Java相关话题

Copyright © 2023 WWW.MENGVLOG.COM - 檬味博客
返回顶部