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

How to get system time in Java

醉折花枝作酒筹
醉折花枝作酒筹forward
2021-04-28 09:36:534742browse

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));

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()));

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);

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);

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!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete