Home>Article>Java> How to get the current date in java

How to get the current date in java

王林
王林 Original
2020-10-23 14:38:05 62753browse

How to get the current date in java: directly instantiate the Date class located in the Java package java.util, such as [Date date = new Date();].

How to get the current date in java

System.currentTimeMillis()

Obtaining the standard time can be obtained through the System.currentTimeMillis() method. This method Not affected by time zone, the results are in timestamp format. For example:

(Recommended tutorial:java course)

1543105352845

We can convert the timestamp into a format that is easy for us to understand

SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd 'at' HH:mm:ss z"); Date date = new Date(System.currentTimeMillis()); System.out.println(formatter.format(date));

Then the time The time corresponding to the stamp is:

2018-11-25 at 01:22:12 CET

It is worth noting that this method will return the current value based on our system time, because time zones around the world are different.

java.util.Date

In Java, one of the easiest ways to get the current date is to directly instantiate the Date class located in the Java package java.util.

Date date = new Date(); // this object contains the current date value

The date obtained above can also be formatted into the format we need, for example:

SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss"); System.out.println(formatter.format(date));

Related recommendations:Getting Started with Java

The above is the detailed content of How to get the current date in java. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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