java 获取当前时间的三种方法
作者:topEngineerray 时间:2022-10-11 21:54:04
总结java里面关于获取当前时间的一些方法
System.currentTimeMillis()
获取标准时间可以通过System.currentTimeMillis()
方法获取,此方法不受时区影响,得到的结果是时间戳格式的。例如:
1543105352845
我们可以将时间戳转化成我们易于理解的格式
SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd 'at' HH:mm:ss z");
Date date = new Date(System.currentTimeMillis());
System.out.println(formatter.format(date));
则该时间戳对应的时间为:
2018-11-25 at 01:22:12 CET
值得注意的是,此方法会根据我们的系统时间返回当前值,因为世界各地的时区是不一样的。
java.util.Date
在Java中,获取当前日期最简单的方法之一就是直接实例化位于Java包java.util的Date类。
Date date = new Date(); // this object contains the current date value
上面获取到的日期也可以被format成我们需要的格式,例如:
SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
System.out.println(formatter.format(date));
Calendar API
Calendar类,专门用于转换特定时刻和日历字段之间的日期和时间。
使用Calendar 获取当前日期和时间非常简单:
Calendar calendar = Calendar.getInstance(); // gets current instance of the calendar
与date一样,我们也可以非常轻松地format这个日期成我们需要的格式
SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
System.out.println(formatter.format(calendar.getTime()));
上面代码打印的结果如下:
25-11-2018 00:43:39
Date/Time API
Java 8提供了一个全新的API,用以替换java.util.Date和java.util.Calendar。Date / Time API提供了多个类,帮助我们来完成工作,包括:
LocalDate
LocalTime
LocalDateTime
ZonedDateTime
LocalDate
LocalDate只是一个日期,没有时间。 这意味着我们只能获得当前日期,但没有一天的具体时间。
LocalDate date = LocalDate.now(); // gets the current date
我们可以format它
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy");
System.out.println(date.format(formatter));
得到的结果只有年月日,例如:
25-11-2018
LocalTime
LocalTime与LocalDate相反,它只代表一个时间,没有日期。 这意味着我们只能获得当天的当前时间,而不是实际日期:
LocalTime time = LocalTime.now(); // gets the current time
可以按如下方式format
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm:ss");
System.out.println(time.format(formatter));
得到的结果类似如下:
00:55:58
LocalDateTime
最后一个是LocalDateTime,也是Java中最常用的Date / Time类,代表前两个累的组合 - 即日期和时间的值:
LocalDateTime dateTime = LocalDateTime.now(); // gets the current date and time
format的方式也一样
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm:ss");
System.out.println(dateTime.format(formatter));
得到的日期结果类似于:
25-11-2018 00:57:20
总结
以上所述是小编给大家介绍的java 获取当前时间的三种方法,网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!
来源:https://blog.csdn.net/topdeveloperr/article/details/91571311
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Android10 启动之SystemServer源码分析
![](https://img.aspxhome.com/file/2023/9/93269_0s.png)
Android编程开发之ScrollView嵌套GridView的方法
Java分布式事务管理框架之Seata
![](https://img.aspxhome.com/file/2023/1/63731_0s.png)
详解Java中clone的写法
![](https://img.aspxhome.com/file/2023/6/99766_0s.jpg)
Android实战教程第八篇之短信备份
Android Flutter中Offstage组件的使用教程详解
JAVA文件扫描(递归)的实例代码
详解基于Android App 安全登录认证解决方案
![](https://img.aspxhome.com/file/2023/5/125445_0s.png)
Go返回int64类型字段超出javascript Number范围的解决方法
![](https://img.aspxhome.com/file/2023/6/107356_0s.png)
关于easyExcel中读取Excel表头的实例说明
![](https://img.aspxhome.com/file/2023/7/86797_0s.png)
C# byte转为有符号整数实例
Java零基础教程之Windows下安装 JDK的方法图解
![](https://img.aspxhome.com/file/2023/7/110927_0s.jpg)
36个Android开发常用经典代码大全
Android 中Notification弹出通知实现代码
Java数据结构之顺序表的实现
![](https://img.aspxhome.com/file/2023/8/57418_0s.jpg)
c#转义字符串中的所有正则特殊字符方法示例
Android OkHttp实现全局过期token自动刷新示例
![](https://img.aspxhome.com/file/2023/9/105999_0s.png)
Mybatis分页插件PageHelper的使用详解
Java创建线程的七种方法总结(全网最全面)
![](https://img.aspxhome.com/file/2023/8/58698_0s.png)
java使用字符画一个海绵宝宝
![](https://img.aspxhome.com/file/2023/3/58453_0s.jpg)