Java 中的日期与时间处理!

时间戳

Epoch Time ,即时间戳,在不同编程语言中有如下几种存储方式:

  • 以秒为单位的整数,缺点是只能精确到秒;

  • 以毫秒为单位的整数,最后 3 位表示毫秒数;

  • 以秒为单位的浮点数,小数点后表示零点几秒;

标准库 API

主要提供了两套处理时间和日期的 API:

  • 定义在 java.util 中,主要包括 Date、Calendar、TimeZone 这几个类;

  • 定义在 java.time 中,主要包括 LocalDateTime、ZoneDateTime、ZoneId 等,自 Java 8 引入;

Date

  • 基本用法

import java.util.*;

public class Main{

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

// 获取当前时间

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 不仅是不变对象,还是线程安全的,有如下两种使用方式;

  • 传入格式化字符串

DateTimeFormatter formatter = DateTimeFormatter.ofPattern(“yyyy-MM-dd HH:mm:ss”)

  • 传入格式化字符串同时指定 Locale

DateTimeFormatter formatter = DateTimeFormatter.ofPattern(“E, yyyy-MM-dd HH:mm:ss”, Locale.US)

  • 对比效果

import java.time.*;

import java.time.format.*;

import java.util.Locale;

public class Main{

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

ZonedDateTime zdt = ZonedDateTime.new();

var formatter = DateTimeFormatter.ofPattern(“yyyy-MM-dd’T’HH:mm ZZZZ”);

System.out.println(formatter.format(zdt));

var zhFormatter = DateTimeFormatter.ofPattern(“yyyy-MM-dd EE HH:mm”, Locale.CHINA);

System.out.println(zhFormatter.format(zdt));

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值