java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > java时间类型转换

JAVA时间类型转换处理方式

作者:丿BAIKAL巛

本文详细介绍了Java中LocalDate与Date类型之间的转换方法,以及字符串格式化与解析技巧,同时,还涵盖了LocalDateTime和LocalTime与Date类型的转换,感兴趣的朋友跟随小编一起看看吧

LocalDateDate

Java时间类型详解:LocalDate与Date互转及字符串格式化

在Java中,时间处理是一个常见的需求。随着Java 8引入了新的日期时间API(java.time包),开发者可以更方便地处理日期和时间。本文将详细介绍 LocalDateDate 类型的互转、字符串格式化以及其他常见时间类型的转换。

一、LocalDate与Date类型互转

1.Date转LocalDate

Date 是Java早期的日期时间类,而 LocalDate 是Java 8引入的日期类。我们可以通过以下方式将 Date 转换为 LocalDate

import java.time.LocalDate;
import java.time.ZoneId;
import java.util.Date;
public class DateToLocalDate {
    public static void main(String[] args) {
        // 获取当前Date对象
        Date nowDate = new Date();
        // 将Date转换为LocalDate
        LocalDate localDate = nowDate.toInstant()          // 将Date转换为Instant
                                    .atZone(ZoneId.systemDefault())  // 指定时区
                                    .toLocalDate();        // 转换为LocalDate
        System.out.println("Date: " + nowDate);
        System.out.println("LocalDate: " + localDate);
    }
}

输出示例:

Date: Mon Oct 09 12:34:56 CST 2023
LocalDate: 2023-10-09

2.LocalDate转Date

LocalDate 转换为 Date 需要借助 ZonedDateTimeInstant

import java.time.LocalDate;
import java.time.ZoneId;
import java.util.Date;
public class LocalDateToDate {
    public static void main(String[] args) {
        // 获取当前LocalDate对象
        LocalDate localDate = LocalDate.now();
        // 将LocalDate转换为Date
        Date date = Date.from(localDate.atStartOfDay(ZoneId.systemDefault()).toInstant());
        System.out.println("LocalDate: " + localDate);
        System.out.println("Date: " + date);
    }
}

输出示例:

LocalDate: 2023-10-09
Date: Mon Oct 09 00:00:00 CST 2023

二、字符串格式化与解析

1.LocalDate与字符串互转

(1)LocalDate转字符串

使用 DateTimeFormatter 可以将 LocalDate 格式化为字符串:

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
public class LocalDateToString {
    public static void main(String[] args) {
        LocalDate localDate = LocalDate.now();
        // 定义格式化器
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        // 格式化LocalDate为字符串
        String dateString = localDate.format(formatter);
        System.out.println("LocalDate: " + localDate);
        System.out.println("Formatted String: " + dateString);
    }
}

输出示例:

LocalDate: 2023-10-09
Formatted String: 2023-10-09

(2)字符串转LocalDate

将字符串解析为 LocalDate

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
public class StringToLocalDate {
    public static void main(String[] args) {
        String dateString = "2023-10-09";
        // 定义格式化器
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        // 解析字符串为LocalDate
        LocalDate localDate = LocalDate.parse(dateString, formatter);
        System.out.println("String: " + dateString);
        System.out.println("LocalDate: " + localDate);
    }
}

输出示例:

String: 2023-10-09
LocalDate: 2023-10-09

2.Date与字符串互转

(1)Date转字符串

使用 SimpleDateFormat 可以将 Date 格式化为字符串:

import java.text.SimpleDateFormat;
import java.util.Date;
public class DateToString {
    public static void main(String[] args) {
        Date nowDate = new Date();
        // 定义格式化器
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        // 格式化Date为字符串
        String dateString = formatter.format(nowDate);
        System.out.println("Date: " + nowDate);
        System.out.println("Formatted String: " + dateString);
    }
}

输出示例:

Date: Mon Oct 09 12:34:56 CST 2023
Formatted String: 2023-10-09 12:34:56

(2)字符串转Date

将字符串解析为 Date

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class StringToDate {
    public static void main(String[] args) throws ParseException {
        String dateString = "2023-10-09 12:34:56";
        // 定义格式化器
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        // 解析字符串为Date
        Date date = formatter.parse(dateString);
        System.out.println("String: " + dateString);
        System.out.println("Date: " + date);
    }
}

输出示例:

String: 2023-10-09 12:34:56
Date: Mon Oct 09 12:34:56 CST 2023

三、其他常见时间类型转换

1.LocalDateTime与Date互转

(1)Date转LocalDateTime

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;
public class DateToLocalDateTime {
    public static void main(String[] args) {
        Date nowDate = new Date();
        // 将Date转换为LocalDateTime
        LocalDateTime localDateTime = nowDate.toInstant()
                                             .atZone(ZoneId.systemDefault())
                                             .toLocalDateTime();
        System.out.println("Date: " + nowDate);
        System.out.println("LocalDateTime: " + localDateTime);
    }
}

(2)LocalDateTime转Date

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;
public class LocalDateTimeToDate {
    public static void main(String[] args) {
        LocalDateTime localDateTime = LocalDateTime.now();
        // 将LocalDateTime转换为Date
        Date date = Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
        System.out.println("LocalDateTime: " + localDateTime);
        System.out.println("Date: " + date);
    }
}

2.LocalTime与Date互转

(1)Date转LocalTime

import java.time.LocalTime;
import java.time.ZoneId;
import java.util.Date;
public class DateToLocalTime {
    public static void main(String[] args) {
        Date nowDate = new Date();
        // 将Date转换为LocalTime
        LocalTime localTime = nowDate.toInstant()
                                     .atZone(ZoneId.systemDefault())
                                     .toLocalTime();
        System.out.println("Date: " + nowDate);
        System.out.println("LocalTime: " + localTime);
    }
}

(2)LocalTime转Date

由于 LocalTime 只包含时间信息,转换为 Date 时需要结合日期:

import java.time.LocalDate;
import java.time.LocalTime;
import java.time.ZoneId;
import java.util.Date;
public class LocalTimeToDate {
    public static void main(String[] args) {
        LocalTime localTime = LocalTime.now();
        // 将LocalTime转换为Date(需要结合日期)
        Date date = Date.from(LocalDate.now()
                                       .atTime(localTime)
                                       .atZone(ZoneId.systemDefault())
                                       .toInstant());
        System.out.println("LocalTime: " + localTime);
        System.out.println("Date: " + date);
    }
}

到此这篇关于JAVA时间类型转换处理方式的文章就介绍到这了,更多相关java时间类型转换内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文