JAVA时间类型转换处理


以下是关于Java时间类型的内容,涵盖了 LocalDateDate 类型的互转、字符串格式化以及其他常见时间类型的转换。


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

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


一、LocalDateDate 类型互转

1. DateLocalDate

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. LocalDateDate

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. LocalDateTimeDate 互转

(1)DateLocalDateTime
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)LocalDateTimeDate
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. LocalTimeDate 互转

(1)DateLocalTime
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)LocalTimeDate

由于 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);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

丿BAIKAL巛

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值