Java Examples - Get Current Time
The following example demonstrates how to use the Date class and the format(date) method of the SimpleDateFormat class to output the current time:
Main.java file
import java.text.SimpleDateFormat;
import java.util.Date;
public class Main{
public static void main(String[] args){
SimpleDateFormat sdf = new SimpleDateFormat();//Format time
sdf.applyPattern("yyyy-MM-dd HH:mm:ss a");//a is the am/pm marker
Date date = new Date();//Get current time
System.out.println("Current time:" + sdf.format(date)); //Output the formatted current time (24-hour format)
}
}
The above code produces the following output:
现在时间:2015-03-27 21:27:28 下午Other Extensions
Java Examples