Home > Java > Javagetting Started > How to get the current date in java

How to get the current date in java

王林
Release: 2020-10-23 14:38:05
Original
62948 people have browsed it

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
Copy after login

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));
Copy after login

Then the time The time corresponding to the stamp is:

2018-11-25 at 01:22:12 CET
Copy after login

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
Copy after login

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));
Copy after login

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!

Related labels:
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