Date
current time
ex
Date currentTime = new Date();
System.out.println("currentTime: " + currentTime);
UNIX TimeStamp已考慮時區問題
ex
Date currentUnix = new Date();
Long unix =currentUnix.getTime();
apache.commons.lang3 DateFormatUtils
public static String format(Date date,
String pattern,
TimeZone timeZone)
Formats a date/time into a specific pattern in a time zone.
Parameters:
date - the date to format, not null
pattern - the pattern to use to format the date, not null
timeZone - the time zone to use, may be null
Returns:
the formatted date
Fomatter用法
import java.time.format.DateTimeFormatter; DateTimeFormatter format = DateTimeFormatter.ofPattern("MMM d yyyy hh:mm a"); DateTimeFormatter format1 = DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss"); DateTimeFormatter format2 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm"); String dateString = "2022-12-19 19:30"; localDateTime.format(format); LocalDateTime formatDateTime = LocalDateTime.parse(dateString, format2);
-------------------------------------------------------------;
LocalDateTime
LocalDateTime
LocalDateTime currentTime = LocalDateTime.now();
ex: 2022-02-14T09:52:14.406 (本機本地時間)
--------------------------------------------------------;
ZonedDateTime
取得有時區的時間
ZonedDateTime date1 = ZonedDateTime.parse("2007-12-03T10:15:30+05:30[Asia/Taipei]");
ex: 2022-12-03T10:15:30+08:00[Asia/Taipei]
ZoneId id = ZoneId.of("Asia/Taipei");
//取得ZoneId
-------------------------------------------------------------; Date 轉其他型別
Date currcurrentDateentUnix = new Date();
Instant current = currentDate.toInstant();
ZoneId myZone = ZoneId.of("Asia/Taipei");
轉LocalDateTime和ZonedDateTime類別
LocalDateTime localDateTime = LocalDateTime.ofInstant(current, myZone);
ZonedDateTime zonedDateTime = ZonedDateTime.ofInstant(current, myZone);