Home > Java > javaTutorial > How to get system time in Java

How to get system time in Java

醉折花枝作酒筹
Release: 2021-04-28 18:51:05
forward
4795 people have browsed it

This article will introduce to you four methods of obtaining system time in Java. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to everyone.

How to get system time in Java

1. Get the current time through the Date class

Date day=new Date();    
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
System.out.println(df.format(day));
Copy after login

2. Get the current time through the currentTimeMillis method in the System class

SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");   
System.out.println(df.format(System.currentTimeMillis()));
Copy after login

3. Get the current time through the Calendar class

Calendar c = Calendar.getInstance();//可以对每个时间域单独修改   
int year = c.get(Calendar.YEAR);  
 int month = c.get(Calendar.MONTH);   
int date = c.get(Calendar.DATE);    
int hour = c.get(Calendar.HOUR_OF_DAY);   
int minute = c.get(Calendar.MINUTE);   
int second = c.get(Calendar.SECOND);    
System.out.println(year + "/" + month + "/" + date + " " +hour + ":" +minute + ":" + second);
Copy after login

4. Get the current time through the Date class

Date date = new Date();    
String year = String.format("%tY", date);   
String month = String.format("%tB", date);   
String day = String.format("%te", date);    
System.out.println("今天是:"+year+"-"+month+"-"+day);
Copy after login

Recommended: "java video tutorial"

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

Related labels:
source:csdn.net
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