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

?? 一、是獲取時(shí)間:
????? System.currentTimeMillis()??? //1970年到現(xiàn)在的毫米數(shù)
????? Date date = new Date()?????????????????
????? Canlendar c = new Calendar.getInstance()
??
?? 二、格式化時(shí)間:
????? 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)
?
?? 四、在數(shù)據(jù)庫中獲取時(shí)間點(diǎn)
????? Timestamp ts = new rs.getTimestamp("date");
????? SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
????? System.out.println(sdf.format(ts));
?? 五、將設(shè)定的時(shí)間字符串轉(zhuǎn)換為時(shí)間
????? 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);
?
?? 六、時(shí)區(qū)時(shí)間的獲取
????? Cadendar cJanpan = new GregorianCadendar(TimeZone.getTimeZone("Japan"));
?????

?????
posted on 2008-09-04 21:45 nonels 閱讀(220) 評論(0) 編輯 收藏 所屬分類: J2SE