软件编程
位置:首页>> 软件编程>> java编程>> Java日期操作类常见用法示例

Java日期操作类常见用法示例

作者:cakincqm  发布时间:2021-06-17 01:12:59 

标签:Java,日期操作类

本文实例讲述了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

0
投稿

猜你喜欢

手机版 软件编程 asp之家 www.aspxhome.com