Java Examples - Get current time
The following example demonstrates how to use format Date class and SimpleDateFormat class (date) method to output the current time:
/* author by w3cschool.cc 文件名:Main.java */ import java.text.SimpleDateFormat; import java.util.Date; public class Main{ public static void main(String[] args){ SimpleDateFormat sdf = new SimpleDateFormat();// 格式化时间 sdf.applyPattern("yyyy-MM-dd HH:mm:ss a");// a为am/pm的标记 Date date = new Date();// 获取当前时间 System.out.println("现在时间:" + sdf.format(date)); // 输出已经格式化的现在时间(24小时制) } }
The above code is run output is:
现在时间:2015-03-27 21:27:28 下午