java中如何获得当前时间并输出:时,分,秒,

java中如何获得当前时间并输出:时,分,秒,,第1张

import javatext;

import javautil;

public class Test {

public static void main(String[] args) {

Date d = new Date();

SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ssSSS");//时:分:秒:毫秒

Systemoutprintln(sdfformat(d));

Long l = dgetTime();//返回自 1970 年 1 月 1 日 00:00:00 GMT 以来此 Date 对象表示的毫秒数。

l += 100000;//加100000毫秒

Date d1 = new Date(l);// 分配 Date 对象并初始化此对象,以表示自从标准基准时间(称为“历元(epoch)”,即 1970 年 1 月 1 日 00:00:00 GMT)以来的指定毫秒数。

Systemoutprintln(sdfformat(d1));

}

}

1

long javautilDategetTime()

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

SystemcurrentTimeMillis(); 这个方法获取当前时间的毫秒数。

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=sdfparse(start)getTime();

long justNow =SystemcurrentTimeMillis();

//两个日期想减得到天数

long dayCount= (justNow-timeStart)/(2436001000);

Systemoutprintln(dayCount);

}

}

输出

25

大写的s表示毫秒数

你的这个可以这么写

SimpleDateFormat dateFormatGmt = new SimpleDateFormat("yyyy-MM-dd aHH:mm:ss:SSS");

Systemoutprintln(dateFormatGmtformat(new Date()));

输出2014-03-04 下午18:13:05:627

这个627就是对应那个SSS,也就是当前毫秒数

public class TestTime {

public static void main(String[] args) {

SimpleDateFormat sdf = new SimpleDateFormat("yyyy年M月d日");

try {

Date d = sdfparse("2013年1月6日");

sdf = new SimpleDateFormat("yyyy-MM-dd");

Systemoutprintln(sdfformat(d));

Systemoutprintln(dgetTime());

} catch (ParseException e) {

eprintStackTrace();

}

}

}

可以使用Long类型存储,long value = new Date()getTime();

需要转成date的时候, Date date = new Date(value);

以上就是关于java中如何获得当前时间并输出:时,分,秒,全部的内容,包括:java中如何获得当前时间并输出:时,分,秒,、怎么在java里获取带有毫秒的时间、JAVA如何获取当前小时的毫秒数呢是当前小时,比如2014-03-04 下午16:00:00的毫秒数等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/9303579.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-04-27
下一篇2023-04-27

发表评论

登录后才能评论

评论列表(0条)

    保存