怎么在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 阅读 32 次 更新于 2025-09-10 22:24:06 我来答关注问题0
  • 如上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 {...

  •  深空见闻 java 取当前时间

    使用java.util.Date类:Date类表示特定的瞬间,精确到毫秒。通过创建Date对象可以获取包含日期和时间信息的完整时间数据。但Date类的API设计较为陈旧,在Java 8之后逐渐被新的时间API取代。使用Java 8引入的java.time包中的类:Java 8引入了新的时间API,包括LocalDateTime、ZonedDateTime、Instant等类。Loca...

  • 一、获取毫秒数的代码:(1)System.currentTimeMillis() 这种方式速度最快。(2)Calendar.getInstance().getTimeInMillis() 这种方式速度最慢。二、获取微秒数的代码:微秒使用System.nanoTime()方法:如果Java程序需要高精度的计时,如1毫秒或者更小,使用System.nanoTime()方法,可以满足需求。

  •  湖北倍领科技 怎么在当前Java程序中获取当前年月日

    首先,使用System.currentTimeMillis()方法获取当前时间的毫秒数,将其存储在long类型变量l中:long l = System.currentTimeMillis();接着,创建一个Date对象,将毫秒数传递给该对象的构造函数:Date date = new Date(l);然后,使用SimpleDateFormat类将日期格式化为指定的字符串形式。这里,我们使用"yyy...

  • Java获取当前时间戳:在Java中,可以使用System.currentTimeMillis()方法来获取当前时间的时间戳(以毫秒为单位)。这个时间戳表示从1970年1月1日00:00:00 GMT开始到现在的毫秒数。如果需要以秒为单位的时间戳,可以将毫秒数除以1000。例如:long timestamp = System.currentTimeMillis() / 1000;。

檬味博客在线解答立即免费咨询

Java相关话题

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