日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > java >内容正文

java

java8 日期转换_Java8日期时间——LocalDateTime的使用以及相互转换

發(fā)布時間:2025/3/20 java 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java8 日期转换_Java8日期时间——LocalDateTime的使用以及相互转换 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

日期時間處理

Java8內(nèi)每個類含義

在 Java8 之前操作時間,用的都是 Date 和 Calendar 類,但這兩個類,操作起來及其繁瑣,且在時間轉(zhuǎn)換、時區(qū)轉(zhuǎn)換的時候也很麻煩,因此 JDK 官方在 8 之后,引入了 LocalDateTime 以及相關(guān)類,通過新的類來定義和操作時間,也十分的簡單清晰,下面我們就來我看一下如何操作。

Instant: 時間戳

Duration: 持續(xù)時間, 時間差

LocalDate: 只包含?期, ?如: 2016-10-20

LocalTime: 只包含時間, ?如: 231210

LocalDateTime: 包含?期和時間, ?如: 2016-10-20 231421

Period: 時間段

ZoneOffset: 時區(qū)偏移量, ?如: +8:00

ZonedDateTime: 帶時區(qū)的時間

Clock: 時鐘, ?如獲取?前美國紐約的時間

代碼實現(xiàn)

localdatetime -> 其他類型

// =================================================================================

public static Date localDateTimeToDate(LocalDateTime localDateTime) {

Date date = Date.from(localDateTime.toInstant(ZoneOffset.ofHours(8)));

return date;

}

public static Date localDateTimeToDate(LocalDateTime localDateTime, ZoneOffset zoneOffset) {

Date date = Date.from(localDateTime.toInstant(zoneOffset));

return date;

}

public static long localDateTimeToTimestamp(LocalDateTime localDateTime) {

long timestamp = localDateTime.toInstant(ZoneOffset.ofHours(8)).getEpochSecond();

return timestamp;

}

public static long localDateTimeToTimestamp(LocalDateTime localDateTime, ZoneOffset zoneOffset) {

long timestamp = localDateTime.toInstant(zoneOffset).getEpochSecond();

return timestamp;

}

public static String localDateTimeToString(LocalDateTime localDateTime) {

DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

String string = dateTimeFormatter.format(localDateTime);

return string;

}

public static String localDateTimeToString(LocalDateTime localDateTime, DateTimeFormatter dateTimeFormatter) {

String string = dateTimeFormatter.format(localDateTime);

return string;

}

public static String localDateTimeToString(LocalDateTime localDateTime, String pattern) {

DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(pattern);

String string = dateTimeFormatter.format(localDateTime);

return string;

}

date -> 其他類型

// =================================================================================

public static long dateToTimeStamp(Date date) {

return date.getTime();

}

public synchronized static String dateToString(Date date, SimpleDateFormat simpleDateFormat) {

return simpleDateFormat.format(date);

}

public static String dateToString(Date date) {

SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

return simpleDateFormat.format(date);

}

public static LocalDateTime dateToLocalDateTime(Date date) {

LocalDateTime localDateTime = LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault());

return localDateTime;

}

public static LocalDateTime dateToLocalDateTime(Date date, ZoneId zoneId) {

LocalDateTime localDateTime = LocalDateTime.ofInstant(date.toInstant(), zoneId);

return localDateTime;

}

timestamp -> 其他類型

// =================================================================================

public static Date timestampToDate(long timestamp) {

return new Date(timestamp);

}

public static LocalDateTime timestampToLocalDateTime(long timestamp) {

LocalDateTime localDateTime = Instant.ofEpochSecond(timestamp).atZone(ZoneOffset.ofHours(8)).toLocalDateTime();

return localDateTime;

}

string -> 其他類型

// =================================================================================

public static Date stringToDate(String string, SimpleDateFormat simpleDateFormat) throws ParseException {

return simpleDateFormat.parse(string);

}

public static Date stringToDate(String string) throws ParseException {

SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

return simpleDateFormat.parse(string);

}

public static LocalDateTime stringToLocalDateTime(String string) {

DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

LocalDateTime localDateTime = LocalDateTime.parse(string, dateTimeFormatter);

return localDateTime;

}

public static LocalDateTime stringToLocalDateTime(String string, DateTimeFormatter dateTimeFormatter) {

LocalDateTime localDateTime = LocalDateTime.parse(string, dateTimeFormatter);

return localDateTime;

}

public static long stringToTimestamp(String string) {

DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

LocalDateTime localDateTime = LocalDateTime.parse(string, dateTimeFormatter);

long timestamp = localDateTime.toInstant(ZoneOffset.ofHours(8)).getEpochSecond();

return timestamp;

}

公眾號截圖

文章在公眾號「iceWang」第一手更新,有興趣的朋友可以關(guān)注公眾號,第一時間看到筆者分享的各項知識點,謝謝!筆芯!

總結(jié)

以上是生活随笔為你收集整理的java8 日期转换_Java8日期时间——LocalDateTime的使用以及相互转换的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。