600字范文,内容丰富有趣,生活中的好帮手!
600字范文 > java时间戳 时间格式之间的转换

java时间戳 时间格式之间的转换

时间:2019-07-28 22:36:42

相关推荐

java时间戳 时间格式之间的转换

第一种获取系统时间的方法(Date)

//第一种转换时间,将系统时间转换为平时常见的形式Date date =new Date();//System.out.println(date);//输出结果 Sat Jul 30 14:42:54 CST long time1=date.getTime();//获取当前时间的毫秒表现格式Locale locale=Locale.getDefault();//类名.方法名--是静态方法的调用方式//获取当前的时区,--也可以使用该类获取当前的语言环境String p="yyyy年MM月dd HH:mm:ss";//设置要转换的日期格式//也可以是 yyyy-MM-dd HH:mm:ss yyyy\MM\dd HH:mm:ss 中间的字符可以任意修改SimpleDateFormat sformat=new SimpleDateFormat(p,locale);String time2=sformat.format(time1);System.out.println(time2);//输出结果 07月30 14:42:54

反向解析

列如:把06月70 08:42:54----->转换为 Mon Aug 09 08:42:54 CST

//将当前的时间,反向解析String time3="06月70 08:42:54";//要转换的时间Locale locale1=Locale.getDefault();//获取当前的时区,--也可以使用该类获取当前的语言环境String p1="yyyy年MM月dd HH:mm:ss";//设置要转换的日期格式SimpleDateFormat sFormat1=new SimpleDateFormat(p1,locale1);Date time4=sFormat1.parse(time3);//注意 写到这里会报错System.out.println(time4);//Fri Jul 30 14:42:54 CST

*注意:*在这里会报错

直接:点击这个

第二种方法(System.currentTimeMillis())

//第二种方法,获取系统long格式时间的方法long time5=System.currentTimeMillis();//获取系统long格式时间的第二种方法// System.out.println(time5);//输出为 1659164242998 ,这是系统的时间,当然每次运行都不一样Date date1=new Date(time5);//直接把Long类型的时间表示形式,改为dateSystem.out.println(date1);// Sat Jul 30 14:57:22 CST //把获取到的long类型时间表示格式转为datelong times=1659060242998L;Date date2=new Date(times);System.out.println(date2);//Fri Jul 29 10:04:02 CST

将获取到的long类型转换为常见的格式

获取到的系统时间

//将获取到的long类型,转换为常见的形式long time7=System.currentTimeMillis();//获取系统long格式时间的第二种方法Locale locale2=Locale.getDefault();String p2="yyyy-MM-dd HH:mm:ss";SimpleDateFormat sFormat2=new SimpleDateFormat(p2,locale2);String time6=sFormat2.format(time7);System.out.println(time6);

另外获得的long类型

列如:long times=1659060242998L; 转换为 -07-29 10:04:02

long time8=1659060242998L;Locale locale3=Locale.getDefault();String p3="yyyy-MM-dd HH:mm:ss";SimpleDateFormat sFormat3=new SimpleDateFormat(p3,locale3);System.out.println(sFormat3.format(time8));System.out.println( System.getProperties());//获取系统环境

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