?? 在Java中關于時間的處理有很多種方法,在API中也有好幾個關于時間處理的類,如 Date、Canlendar、GregorianCalendar、SimpleDateFormat、timestamp等...貌似很雜,現在一一整理~
?
?? 各種時間類的關系如下:
? ? ??

??
一、是獲取時間:
????? System.currentTimeMillis()??? //1970年到現在的毫米數
????? Date date = new Date()?????????????????
????? Canlendar c = new Calendar.getInstance()
??
??
二、格式化時間:????? Date d = new Date();
????? SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
????? System.out.println(sdf.format(d));
?
??
三、獲取月份等值????? Canlendar c = new Calendar.getInstance()
??? ? System.out.println(c.get(Calendar.MONTH)+1)
?
??
四、在數據庫中獲取時間點????? Timestamp ts = new rs.getTimestamp("date");
????? SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
????? System.out.println(sdf.format(ts));
??
五、將設定的時間字符串轉換為時間????? String s = "1970-12-30 08:21:14.0";
????? Timestamp ts = Timestamp.valueOf(s);
????? System.out.println(ts);
????? String s1 = "08:21:14";
??? ? Time t = Time.valueOf(s1);
??? ? System.out.println(t);
?
??
六、時區時間的獲取????? Cadendar cJanpan = new GregorianCadendar(TimeZone.getTimeZone("Japan"));
?????

System.out.println(cJapan.get(Calendar.HOUR_OF_DAY);
?????