Java日期操作类常见用法示例
作者:cakincqm 时间:2021-06-17 01:12:59
本文实例讲述了Java日期操作类常见用法。分享给大家供大家参考,具体如下:
一 取出当前日期时间
1 代码
import java.time.*;
public class GetDatetime
{
public static void main(String[] args)
{
// 创建时间对象,获取当前时间
LocalDateTime timePoint = LocalDateTime.now( ); // 当前时间
System.out.println("--当前时间----");
System.out.println(timePoint);
System.out.println("--获取时间的各个部分----");
System.out.println(timePoint.toLocalDate( ));
System.out.println(timePoint.getMonth( ));
System.out.println(timePoint.getDayOfMonth( ));
System.out.println(timePoint.getSecond( ));
}
}
2 运行
--当前时间----
2019-07-06T08:10:31.458
--获取时间的各个部分----
2019-07-06
JULY
6
31
二 将时间对象转化为字符串
1 代码
import java.time.*;
import java.time.format.*;
public class DateFormatDemo
{
public static void main(String[] args)
{
//获取当前时间
LocalDate locDate = LocalDate.now();
//指定格式化规则
DateTimeFormatter dateFmt1 = DateTimeFormatter.ofPattern("dd/MM/uuuu");
//将当前时间格式化
String str1 = locDate.format(dateFmt1);
System.out.println("时间格式1 : " + str1);
DateTimeFormatter dateFmt2 = DateTimeFormatter.ofPattern("uuuu年MM月dd日");
//将当前时间格式化
String str2 = locDate.format(dateFmt2);
System.out.println("时间格式2 : " + str2);
}
}
2 运行
时间格式1 : 06/07/2019
时间格式2 : 2019年07月06日
PS:这里再为大家推荐几款关于日期与时间计算的在线工具供大家参考使用:
在线日期/天数计算器:
http://tools.jb51.net/jisuanqi/date_jisuanqi
在线万年历日历:
http://tools.jb51.net/bianmin/wannianli
在线阴历/阳历转换工具:
http://tools.jb51.net/bianmin/yinli2yangli
Unix时间戳(timestamp)转换工具:
http://tools.jb51.net/code/unixtime
希望本文所述对大家java程序设计有所帮助。
来源:https://blog.csdn.net/chengqiuming/article/details/94826707
标签:Java,日期操作类
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java中十进制和十六进制的相互转换方法
2022-04-21 11:54:06
Java 添加超链接到 Word 文档方法详解
2023-01-29 08:49:30
![](https://img.aspxhome.com/file/2023/8/70538_0s.png)
详解Java实现单例的五种方式
2021-06-11 03:54:03
Java 8 lambda表达式引入详解及实例
2022-10-25 22:41:37
SpringBoot默认使用HikariDataSource数据源方式
2022-03-18 01:04:46
Java Web Fragment在项目中使用方法详解
2022-04-11 14:17:38
![](https://img.aspxhome.com/file/2023/5/64775_0s.png)
Java 数据结构与算法系列精讲之贪心算法
2023-09-12 17:56:13
![](https://img.aspxhome.com/file/2023/3/65303_0s.jpg)
Java ArrayList源码深入分析
2023-06-16 16:30:26
![](https://img.aspxhome.com/file/2023/8/60918_0s.png)
使用Java8 Stream流的skip + limit实现批处理的方法
2023-11-29 06:17:39
细说C#中的枚举:转换、标志和属性
2021-07-10 06:01:05
Maven配置多仓库无效的解决
2023-11-29 04:37:10
![](https://img.aspxhome.com/file/2023/1/60681_0s.jpg)
jar包运行时提示jar中没有主清单属性的解决
2023-11-23 19:04:10
![](https://img.aspxhome.com/file/2023/2/59532_0s.png)
Java 通过反射给实体类赋值操作
2023-11-26 10:11:41
C#中数组初始化、反转和排序用法实例
2023-05-03 23:03:10
详解在Spring Boot中使用JPA
2022-08-03 15:44:13
![](https://img.aspxhome.com/file/2023/9/69309_0s.png)
Java8新特性之默认方法(default)浅析
2023-10-03 10:41:13
![](https://img.aspxhome.com/file/2023/1/62051_0s.png)
Unity自定义编辑器界面(Inspector界面)
2023-01-09 08:40:46
![](https://img.aspxhome.com/file/2023/6/76426_0s.jpg)
opencv 做人脸识别 opencv 人脸匹配分析
2023-07-09 06:34:44
![](https://img.aspxhome.com/file/2023/8/101078_0s.jpg)
Spring+MyBatis实现数据读写分离的实例代码
2021-08-31 04:34:48
C#面向对象设计原则之里氏替换原则
2022-08-27 23:59:22