怎么在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 阅读 8 次 更新于 2025-07-19 15:12: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中,可以使用System.currentTimeMillis()方法来获取当前时间的时间戳(以毫秒为单位)。这个时间戳表示从1970年1月1日00:00:00 GMT开始到现在的毫秒数。如果需要以秒为单位的时间戳,可以将毫秒数除以1000。例如:long timestamp = System.currentTimeMillis() / 1000;。Python获取当前时间戳:在...

  • 1. 使用 `java.util.Date` 类获取当前时间:java Date date = new Date();2. 获取当前时间的毫秒数:java long millis = System.currentTimeMillis();3. 使用 `java.time` 包中的类获取当前时间(Java 8及以上):java LocalDateTime now = LocalDateTime.now();4. 获取当前时间的时区:java ...

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

  • 在Java中,获取时间戳有多种方法,以下是几种常见的方式:使用System.currentTimeMillis:答案:这是最直接和常用的方法,返回当前时间与Unix纪元之间的毫秒差。javalong timestamp = System.currentTimeMillis;2. 使用Calendar类: 答案:通过Calendar.getInstance.getTimeInMillis也可以获取当前时间的时间戳。

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

Java相关话题

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