迹忆客 专注技术分享

当前位置:主页 > 学无止境 > 编程语言 > Java >

在 Java 日期中获取当前时间戳

作者:迹忆客 最近更新:2023/08/10 浏览次数:

本文介绍如何获取 Java 中的当前时间戳,并列出一些示例代码以了解该主题。

有几种获取 Java 当前时间戳的方法,例如 Timspamp 类,Date 类,ZonedDateTime 类,LocalDateTime 类等。让我们来看一些示例。


使用 Java 中的 Timestamp 类获取当前时间戳

要获取 Java 中的当前时间戳,可以使用 Timestamp 类。由于此类没有默认的构造函数,因此我们以毫秒为单位传递时间。我们使用 System 类的 currentTimeMillis() 方法获取时间。请参见下面的示例。

import java.sql.Timestamp;

public class SimpleTesting{
    public static void main(String[] args) {
        Long datetime = System.currentTimeMillis();
        Timestamp timestamp = new Timestamp(datetime);
        System.out.println("Current Time Stamp: "+timestamp);
    }
}

输出:

Current Time Stamp: 2021-03-06 17:24:57.107

使用 Java 中的 Date 类获取当前时间戳

我们可以使用 util 包的 Date 类来获取当前时间戳。要以 yyyy.MM.dd 格式格式化时间戳,我们可以使用 SimpleDateFormat 类。请参见下面的示例。

import java.text.SimpleDateFormat;
import java.util.Date;

public class SimpleTesting{
    public static void main(String[] args) {
        SimpleDateFormat date = new SimpleDateFormat("yyyy.MM.dd.HH:mm:ss");
        String timeStamp = date.format(new Date());
        System.out.println("Current Time Stamp: "+timeStamp);
    }
}

输出:

Current Time Stamp: 2021.03.06.17:26:17

使用 Java 中的 ZonedDateTime 类获取当前时间戳

Java datetime 包的 ZonedDateTime 类创建带有区域信息的时间戳。我们使用 ZoneIdsystemDefault() 方法获取系统的默认区域,并使用 now() 方法获取具有指定 zoneId 的当前时间戳。

获取当前时间戳后,我们使用 DateTimeFormatter 类的 ofPattern() 方法来获取指定格式的时间戳。请参见下面的示例。

import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;

public class SimpleTesting{
    public static void main(String[] args) {
        ZoneId zid = ZoneId.systemDefault();
        ZonedDateTime datetime = ZonedDateTime.now(zid);
        System.out.println("Current Time Stamp: "+datetime);
        // if want to format into a specific format
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern( "uuuu.MM.dd.HH:mm:ss" );
        String timeStamp = datetime.format(formatter);
        System.out.println("Current Time Stamp: "+timeStamp);
    }
}

输出:

Current Time Stamp: 2021-03-06T17:35:52.722720362Z[Etc/UTC]
Current Time Stamp: 2021.03.06.17:35:52

使用 Java 中的 LocalDateTime 类获取当前时间戳

如果你使用的是 Java 8,请使用 LocalDateTime 类获取 Java 中的当前时间戳。请参见以下示例。

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class SimpleTesting{
    public static void main(String[] args) {
        LocalDateTime dateTime = LocalDateTime.now();
        System.out.println("Current Time Stamp Default Format: "+dateTime);
        String timeStamp = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").format(dateTime);
        System.out.println("Current Time Stamp: "+timeStamp);
    }
}

输出:

Current Time Stamp Default Format: 2021-02-27T14:15:46.196564
Current Time Stamp: 2021-02-27 14:15:46

使用 Java 中的 Instant 类获取当前时间戳

Instant 类可用于获取当前时间戳。我们使用 Instant 类的 now() 方法来即时获取当前时间戳。

import java.time.Instant;
public class SimpleTesting{
    public static void main(String[] args) {
        Instant timeStamp = Instant.now();
        System.out.println("Current Time Stamp Default Format: "+timeStamp);
    }
}

输出:

Current Time Stamp Default Format: 2021-02-27T08:48:28.001913Z

使用 Java 中的 Timestamp 类获取当前时间戳

这是通过 TimestampInstant 类获取 Java 中当前时间戳的另一种解决方案。请参见下面的示例。

import java.sql.Timestamp;
import java.time.Instant;
public class SimpleTesting{
    public static void main(String[] args) {
        long time = System.currentTimeMillis();
        Timestamp timestamp = new Timestamp(time);
        Instant instant = timestamp.toInstant();
        System.out.println("Current Time Stamp: "+instant);
    }
}

输出:

Current Time Stamp: 2021-02-27T08:50:05.125Z

使用 Java 中的 toInstant() 方法获取当前时间戳

如果我们有一个表示时间戳的日期对象,我们还可以使用 Date 类的 toInstant() 方法来获取当前的时间戳。

import java.time.Instant;
import java.util.Date;
public class SimpleTesting{
    public static void main(String[] args) {
        Date date = new Date();
        Instant instant = date.toInstant();
        System.out.println("Current Time Stamp: "+instant);
    }
}

输出:

Current Time Stamp: 2021-02-27T08:51:34.223Z

转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处

本文地址:

相关文章

Do you understand JavaScript closures?

发布时间:2025/02/21 浏览次数:108 分类:JavaScript

The function of a closure can be inferred from its name, suggesting that it is related to the concept of scope. A closure itself is a core concept in JavaScript, and being a core concept, it is naturally also a difficult one.

Do you know about the hidden traps in variables in JavaScript?

发布时间:2025/02/21 浏览次数:178 分类:JavaScript

Whether you're just starting to learn JavaScript or have been using it for a long time, I believe you'll encounter some traps related to JavaScript variable scope. The goal is to identify these traps before you fall into them, in order to av

How much do you know about the Prototype Chain?

发布时间:2025/02/21 浏览次数:150 分类:JavaScript

The prototype chain can be considered one of the core features of JavaScript, and certainly one of its more challenging aspects. If you've learned other object-oriented programming languages, you may find it somewhat confusing when you start

扫一扫阅读全部技术教程

社交账号
  • https://www.github.com/onmpw
  • qq:1244347461

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便