1. import java.text.SimpleDateFormat;   
  2. import java.util.Calendar;   
  3. import java.util.Date;   
  4. import java.util.GregorianCalendar;   
  5. /**  
  6.  * 读取当前系统时间  
  7.  */  
  8. public class ReadSystemTime {   
  9.   
  10.     public static void main(String[] args) {   
  11.         getTime_1();   
  12.         getTime_2();   
  13.         getTime_3();   
  14.     }   
  15.   
  16.     /**  
  17.      * 方法1  
  18.      */  
  19.     public static void getTime_1(){   
  20.         //此处字符串的格式可以修改   
  21.         SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");   
  22.         Date currentTime = new Date();//得到当前系统时间   
  23.         String timeStr = formatter.format(currentTime); //将日期时间格式化    
  24.         System.out.println("[1] "   timeStr);      
  25.         //输出:[1]    2009-08-18 20:06:13   
  26.     }   
  27.     /**  
  28.      * 方法2  
  29.      */  
  30.     public static void getTime_2(){   
  31.         SimpleDateFormat formatter = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");   
  32.         System.out.println("[2] "   formatter.format(System.currentTimeMillis()));   
  33.         //输出:[2]    2009/08/18 20:18:48   
  34.     }   
  35.     /**  
  36.      * 方法3  
  37.      */  
  38.     public static void getTime_3(){   
  39.         Calendar todaysDate = new GregorianCalendar();    
  40.         int year = todaysDate.get(Calendar.YEAR);   
  41.         int month = todaysDate.get(Calendar.MONTH)       1;     
  42.         int day = todaysDate.get(Calendar.DAY_OF_MONTH);     
  43.         int hourOfDay = todaysDate.get(Calendar.HOUR_OF_DAY);//24小时制     
  44.         //int hour = todaysDate.get(Calendar.HOUR);          //12小时制     
  45.         int minute = todaysDate.get(Calendar.MINUTE);     
  46.         int second = todaysDate.get(Calendar.SECOND);            
  47.         System.out.println("[3] "   year   "."   month   "."   day    
  48.                   " "   hourOfDay   ":"   minute   ":"   second);    
  49.         //输出:[3]    2009.8.18 20:23:51         
  50.     }   
  51. }  

本文转载:CSDN博客