时间戳、日期格式、以及相互转换
2017-05-23 18:33
567 查看
获取当前时间
public static void main(String[] args) throws Exception { SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd"); String Today = format.format(new Date()); System.out.println(Today); }
时间格式可以自定义
SimpleDateFormat(“yyyyMMdd”)的格式是通过自己定义的,例:yyyyMMdd
dd/mm/yyyy
yyyy-MM-dd HH:mm:ss
Java日期格式化中字母大小写的区别
YYYY 代表 Week Year(周年)yyyy 代表year
MM 代表 月(Month)
mm代表 秒(Min)
HH代表24小时制 //HH 08 //H 8
hh代表12小时制
获取当前时间戳
public static void main(String[] args) throws Exception { long t = System.currentTimeMillis(); System.out.println(t); }
时间戳转换成当前时间
public static void main(String[] args) throws Exception { SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd : hh:mm:ss"); long t = System.currentTimeMillis(); String d = format.format(t); Date date = format.parse(d); System.out.println("Format To String(Date):" + d); System.out.println("Format To Date:" + date); }
当前时间转换成时间戳
public static void main(String[] args) throws Exception { SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String time = format.format(new Date()); Date date2 = format.parse(time); System.out.print("Format To times:" + date2.getTime()); }
注意:
创建SimpleDateFormat实例时,new SimpleDateFormat(“yyyy-MM-dd HH:mm:ss”);里面字符串头尾不要有空格,不然获取到的时间前后因为会有空格的字符串转成时间戳时要先定义一个long类型的变量。Java获取的时间戳精确到毫秒。除以1000精确到秒
相关文章推荐
- mysql 时间戳与日期格式的相互转换
- mysql 时间戳与日期格式的相互转换
- Quartz动态添加、修改和删除定时任务时间的方法以及日期转换cron表达式时间格式工具类
- 时间戳和日期格式相互转换
- 下面总结一下js中时间戳与日期格式的相互转换:
- Java日期时间以及日期相互转换
- js时间戳和日期格式相互转换
- js时间戳与日期格式的相互转换
- mysql 时间戳与日期格式的相互转换
- js 时间格式与时间戳的相互转换和计算几天后的日期是哪一天
- Java中日期时间格式与毫秒数的相互转换
- Java日期时间,以及相互转换
- js 字符串与数值相互转换,以及时间戳转换成日期形式字符串方法
- Java转换Json日期/Date(1487053489965+0800)/格式以及js时间格式 Tue Feb 14 2017 14:06:32 GMT+0800
- js中Date日期格式转换以及时间和毫秒之间的转换
- iOS 获取系统时间以及时间与日期格式互相转换
- mysql 时间戳与日期格式的相互转换
- js时间戳与日期格式之间相互转换
- 日期格式和时间戳相互转换 外带一个获取当前程序根目录
- JAVA,jsp,oracle获取系统当前时间以及日期格式转换!