Home > Java > javaTutorial > What are the three ways to get the current time in java

What are the three ways to get the current time in java

王林
Release: 2020-05-14 17:20:42
Original
6546 people have browsed it

What are the three ways to get the current time in java

1. Obtain

Date date = new Date();
SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd :hh:mm:ss");
System.out.println(dateFormat.format(date));
Copy after login

through Date in the Util package (video tutorial recommendation: java video tutorial)

2 , Get the time through the Calendar of the Util package

Calendar calendar= Calendar.getInstance();
SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd :hh:mm:ss");
System.out.println(dateFormat.format(calendar.getTime()));
Copy after login

3. Get the time through the Calendar of the Util package, and get the year, month, day, hour, minute and second respectively

Calendar cal=Calendar.getInstance();      
int y=cal.get(Calendar.YEAR);      
int m=cal.get(Calendar.MONTH);      
int d=cal.get(Calendar.DATE);      
int h=cal.get(Calendar.HOUR_OF_DAY);      
int mi=cal.get(Calendar.MINUTE);      
int s=cal.get(Calendar.SECOND);      
System.out.println("现在时刻是"+y+"年"+m+"月"+d+"日"+h+"时"+mi+"分"+s+"秒");
Copy after login

Recommended related tutorials: Getting started with java

The above is the detailed content of What are the three ways to get the current time 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