Home > Java > javaTutorial > How to get the millisecond representation of a date using the getTime() method of the Date class

How to get the millisecond representation of a date using the getTime() method of the Date class

WBOY
Release: 2023-07-24 11:42:19
Original
4356 people have browsed it

How to get the millisecond representation of a date using the getTime() method of the Date class

In Java, the Date class is a class used to represent dates and times. It provides many useful methods to manipulate and obtain information about date objects. Among them, the getTime() method is an important method in the Date class, which can return the millisecond representation of the date object. Next, we will detail how to use this method to obtain the millisecond representation of a date, and provide corresponding code examples.

Using the getTime() method of the Date class is very simple, just call this method. It returns the number of milliseconds since 00:00:00 on January 1, 1970. The specific usage is as follows:

import java.util.Date;

public class DateExample {
    public static void main(String[] args) {
        // 创建一个Date对象
        Date date = new Date();

        // 使用getTime()方法获取毫秒表示形式
        long milliseconds = date.getTime();

        // 打印毫秒数
        System.out.println("Date的毫秒表示形式:" + milliseconds);
    }
}
Copy after login

In the above example, we first created a Date object date. Then, use the getTime() method to obtain the millisecond representation of the date object and assign it to the variable milliseconds. Finally, print the output to display the milliseconds of the date.

The output of this example will be a result similar to "Date in milliseconds: 1578420949000". Note that this number represents the number of milliseconds that have elapsed since 00:00:00 on January 1, 1970.

In addition to the current time, we can also get the millisecond representation of a specific date by setting the date object. For example, if we want to get the number of milliseconds on January 1, 2020, we can do it as follows:

import java.util.Date;

public class DateExample {
    public static void main(String[] args) {
        // 创建一个特定日期的Date对象
        Date date = new Date(120, 0, 1);

        // 使用getTime()方法获取毫秒表示形式
        long milliseconds = date.getTime();

        // 打印毫秒数
        System.out.println("特定日期的毫秒表示形式:" + milliseconds);
    }
}
Copy after login

In the above example, we have created a specific date (January 1, 2020 ) Date object date. Next, we use the getTime() method to obtain the millisecond representation of the object and assign it to the variable milliseconds. Finally, we print the output to show the milliseconds of the date.

The output of this example will be "The millisecond representation of a specific date: 1577836800000". The meaning of this number is also the number of milliseconds that have passed since 00:00:00 on January 1, 1970.

To summarize, it is very simple to use the getTime() method of the Date class to get the millisecond representation of a date. Just create a Date object and call the getTime() method. This method returns the number of milliseconds since 00:00:00 on January 1, 1970. By using this method, we can easily handle date objects and perform related operations.

Hope the code examples and explanations in this article can help you understand how to get the millisecond representation of a date using the getTime() method of the Date class. Happy programming!

The above is the detailed content of How to get the millisecond representation of a date using the getTime() method of the Date class. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template