Java 中的日期与时间处理!

蜂信物联FastBee平台https://gitee.com/beecue/fastbee

阿里资料开源项目https://gitee.com/vip204888

百度低代码前端框架https://gitee.com/baidu/amis

OpenHarmony开源项目https://gitcode.com/openharmony

仓颉编程语言开放项目https://gitcode.com/Cangjie
Date date = new Date();

// 年份

System.out.println(date.getYear() + 1900);

// 月份

System.out.println(date.getMonth() + 1);

// 日期

System.out.println(date.getDate);

// 转换为本地时间

System.out.println(date.toLocaleString());

// 转换为 GMT 时区

System.out.println(date.toGMTString());

}

}

  • 预定义的字符串

  • yyyy:年

  • MM:月

  • dd:日

  • HH:小时

  • mm:分钟

  • ss:秒

  • 存在的问题

  • 不能转换时区;

  • 无法对日期和时间进行运算操作;

Calendar

可用于获取并设置年、月、日、时、分、秒,比 Date 多了一个可以作简单日期和时间运算的功能;

  • 基本用法

import java.util.*;

public class Main{

public staitc void main(String[] args) throws Exception{

// 获取当前时间

Calendar cal = Calendar.getInstance();

// 获取年、月、日、时、分、秒

int year = cal.get(Calendar.YEAR);

int month = cal.get(Calendar.MONTH);

int day = cal.get(Calendar.DAY_OF_MONTH);

int hour = cal.get(Calendar.HOUR_OF_DAY);

int minute = cal.get(Calendar.MINUTE);

int second = cal.get(Calendar.SECOND);

}

}

  • 利用 getTime() 方法,可以将一个 Calendar 对象转换为 Date 对象,然后利用 SimpleDateFormat 进行格式化;

TimeZone

相较于 DateCalendar ,提供了时区转换功能,主要步骤如下:

  1. 清除所有字段;

  2. 设定指定时区;

  3. 设定日期和时间;

  4. 创建 SimpleDateFormat 并设定目标时区;

  5. 格式化获取的 Date 对象(对象无时区信息,时区信息存储在 SimpleDateFormat 中);

import java.util.*;

import java.text.*;

public class Main{

public static void main(String[] args) throws Exception{

Calendar cal = Calendar.getInstance();

cal.clear();

// 设定时区

cal.setTimeZone(TimZone.getTimeZone(“Asia/Shanghai”));

// 设定时间

cal.set(2020, 3 /* 4 月 */, 25, 13, 30, 0);

// 显示时间:

var sdf = new SimpleDateFormat(“yyyy-MM-dd HH:mm:ss”);

sdf.setTimeZone(TimeZone.getTimeZone(“America/New_York”));

System.out.println(sdf.format(c.getTime()));

}

}

LocalDateTime


  • Java 8 引入 java.time 中所提供的新的时间和日期 API,主要涉及的类型:

  • 本地日期和时间:LocalDateTime、LocalDate、LocalTime

  • 带时区的日期和时间:ZonedDateTime

  • 时刻:Instant

  • 时区:ZoneId、ZoneOffset

  • 时间间隔:Duration

  • 格式化:DateTimeFormatter

  • 基本用法

import java.time.*;

public class Main{

public static void main(String[] args) throws Exception{

// 当前日期

LocalDate date = LocalDate.now();

// 当前时间

LocalTime time = LocalTime.now();

// 当前日期和时间

LocalDateTime dateTime = LocalDateTime.now();

}

}

  • 输出标准为 ISO 8601,日期和时间之间的分割符是 T ,规定的标准格式如下:

  • 日期:yyyy-MM-dd

  • 时间:HH:mm:ss

  • 带毫秒的时间:HH:mm:ss.SSS

  • 日期和时间:yyyy-MM-dd T HH:mm:ss

  • 带毫秒的日期和时间:yyyy-MM-dd T HH:mm:ss.SSS

  • 对日期和时间进行调整:

  • 年:withYear()

  • 月:withMonth()

  • 日:withDayOfMonth()

  • 时:withHour()

  • 分:withMinute()

  • 秒:withSecond()

  • DurationPeriod

  • Duration:表示两个时刻间的时间间隔;

  • Period:表示两个日期之间的天数;

ZonedDateTime


用于表示带时区的日期和时间;

  • 时区转换及本地时间转换

import java.time.*;

public class Main{

public static void main(String[] args) throws Exception{

ZonedDateTime zoneTime = ZonedDateTime.now(ZoneId.of(“Asia/Shanghai”));

// 中国时区转换为纽约时间

ZonedDateTime newZoneTime = zoneTime.withZoneSameInstant(ZoneId.of(“America/New_York”));

// 转换为本地时间

LocalDateTime local = zoneTime.toLocalDateTime();

System.out.println(zoneTime);

System.out.println(newZoneTime);

}

}

DateTimeFormatter


相较于 SimpleDateFormatDateTimeFormatter 不仅是不变对象,还是线程安全的,有如下两种使用方式;

  • 传入格式化字符串

架构学习资料

准备两个月,面试五分钟,Java中高级岗面试为何越来越难?

准备两个月,面试五分钟,Java中高级岗面试为何越来越难?

准备两个月,面试五分钟,Java中高级岗面试为何越来越难?

准备两个月,面试五分钟,Java中高级岗面试为何越来越难?

准备两个月,面试五分钟,Java中高级岗面试为何越来越难?

由于篇幅限制小编,pdf文档的详解资料太全面,细节内容实在太多啦,所以只把部分知识点截图出来粗略的介绍,每个小节点里面都有更细化的内容!
是线程安全的,有如下两种使用方式;

  • 传入格式化字符串

架构学习资料

[外链图片转存中…(img-62lBiodw-1725143739754)]

[外链图片转存中…(img-RxGkxUKa-1725143739755)]

[外链图片转存中…(img-nk7QzpyV-1725143739755)]

[外链图片转存中…(img-zznvEU9Z-1725143739756)]

[外链图片转存中…(img-Czk7AOoV-1725143739756)]

由于篇幅限制小编,pdf文档的详解资料太全面,细节内容实在太多啦,所以只把部分知识点截图出来粗略的介绍,每个小节点里面都有更细化的内容!

  • 14
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值