java中日期转换Date、DateTime、TimeStamp、String之间相互转换

1.Date转String

1.1Date->String

        //date->String
        Date date = new Date();
        String format = dateFormat.format(date);
        System.out.println("format = " + format);

1.2String->Date

        //yyyy-MM-dd HH:mm:ss
        //SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time = "2023-04-03";
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        //1.string->date
        Date parse = dateFormat.parse(time);
        System.out.println("parse = " + parse);

2.Date转TimeStamp

2.1Date->TimeStamp

        //Date->TimeStamp
        Date date = new Date();
        long time = date.getTime();
        Timestamp createTime = new Timestamp(time);
        System.out.println("createTime = " + createTime);

2.2TimeStamp->Date

        //TimeStamp->Date
        Timestamp timestamp = new Timestamp(System.currentTimeMillis());
        Date timestampToDate = new Date(timestamp.getTime());
        System.out.println("timestampToDate = " + timestampToDate);

3.Date转DateTime

DateTime使用依赖

        <dependency>
            <groupId>joda-time</groupId>
            <artifactId>joda-time</artifactId>
            <version>2.9.1</version>
        </dependency>

3.1Date->DateTime

方法1:

        //method1
        Date date = new Date();
        DateTime dateTime1 = new DateTime(date);

方法2:

         //method2
        Date date = new Date();
        String dateTimeString = new DateTime(date).toString("yyyy-MM-dd");
        DateTimeFormatter dateTimeFormatter = DateTimeFormat.forPattern("yyyy-MM-dd");
        DateTime time = dateTimeFormatter.parseDateTime(dateTimeString);
        System.out.println("Date->DateTime: " + time);

3.2DateTime->Date

        //DateTime->Date
        DateTime dateTime = new DateTime();
        Date dateToDateTime = dateTime.toDate();
        System.out.println("DateTime->Date" + dateToDateTime);

4.String转DateTime

        //String->DateTime
        String dateTimeString = "2023-04-08";
        DateTimeFormatter dateTimeFormatter = DateTimeFormat.forPattern("yyyy-MM-dd");
        DateTime time = dateTimeFormatter.parseDateTime(dateTimeString);
        System.out.println("String->DateTime: " + time);
        //DateTime->String
        DateTime dt=new DateTime();
        String format="YYYY-MM-dd HH-mm-ss";
        String str= dt.toString(format);
        System.out.println("DateTime->String = " + str);

5.String与TimeStamp互转

         String timeStr = "2023-04-06 10:30:40";
        //String -> Timestamp
        Timestamp time = Timestamp.valueOf(timeStr);
        //Timestamp -> String
        String strn = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(time);
        System.out.println("Timestamp time = " + time);
        System.out.println("strn = " + strn);

  • 2
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
Java提供了几种处理日期和时间的类:`Date`、`LocalDate`、`LocalTime`、`LocalDateTime`、`ZonedDateTime`等等。其,`Date`类是旧版本的日期时间类,而其他类是Java 8引入的新日期时间API。 `Date`类表示特定的日期和时间,但它在处理日期和时间时存在一些问题,因此在新的Java日期时间API被废弃。推荐使用`LocalDate`、`LocalTime`和`LocalDateTime`类来处理日期和时间。 `LocalDate`类表示一个没有时区信息的日期,包含年、月、日信息。 `LocalTime`类表示一个没有日期信息的时间,包含时、分、秒和毫秒信息。 `LocalDateTime`类表示一个包含日期和时间的对象。 另外,如果你需要处理带有时区的日期和时间,可以使用`ZonedDateTime`类。 关于时间戳(timestamp),在Java可以使用`Instant`类来表示。`Instant`类代表从1970年1月1日开始计数的秒数或纳秒数。你可以使用该类来表示一个特定的时间点。 以下是一个示例代码,演示了如何在Java处理日期和时间: ```java import java.time.LocalDate; import java.time.LocalTime; import java.time.LocalDateTime; import java.time.ZonedDateTime; import java.time.Instant; public class DateTimeExample { public static void main(String[] args) { // 获取当前日期 LocalDate currentDate = LocalDate.now(); System.out.println("Current Date: " + currentDate); // 获取当前时间 LocalTime currentTime = LocalTime.now(); System.out.println("Current Time: " + currentTime); // 获取当前日期和时间 LocalDateTime currentDateTime = LocalDateTime.now(); System.out.println("Current Date and Time: " + currentDateTime); // 获取带时区的日期和时间 ZonedDateTime zonedDateTime = ZonedDateTime.now(); System.out.println("Zoned Date and Time: " + zonedDateTime); // 获取当前时间戳 Instant timestamp = Instant.now(); System.out.println("Timestamp: " + timestamp);
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值