900字范文,内容丰富有趣,生活中的好帮手!
900字范文 > Java时间戳格式化为日期字符串

Java时间戳格式化为日期字符串

时间:2022-04-09 23:04:53

相关推荐

Java时间戳格式化为日期字符串

1、时间戳简介

时间戳(TimeStamp),通常是指格林威治时间1970年01月01日00时00分00秒(北京时间1970年01月01日08时00分00秒)起至现在的总秒数,不考虑闰秒Java中时间戳是指格林威治时间1970年01月01日00时00分00秒起至现在的总毫秒数

2、Java获取毫秒值的方法(时间戳)

//方法1(最快)System.currentTimeMillis(); //方法2 Calendar.getInstance().getTimeInMillis(); //方法3 new Date().getTime();

3、时间戳格式化代码

public class TimeTest {public static void main(String[] args) {Long timeStamp = System.currentTimeMillis();SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");System.out.println("Long类型的时间戳:"+timeStamp);System.out.println("格式化后的时间:"+sdf.format(timeStamp));System.out.println("格式化后的时间带毫秒:"+sdf2.format(timeStamp));}}

4、代码运行结果

Long类型的时间戳:1662957597163格式化后的时间:-09-12 12:39:57格式化后的时间带毫秒:-09-12 12:39:57:163

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。