1、獲取當前系統時間和日期並格式化輸出:java
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//設置日期格式 System.out.println(df.format(new Date()));// new Date()爲獲取當前系統時間
2、Date轉換爲Datetimesql
Date date = new Date(); Timestamp timestamp = new Timestamp(date.getTime()); //2013-01-14 22:45:36.484
3、String轉換爲Datecode
//String轉換爲java.util.Date String str = "2013-01-14"; SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); Date date = null; //初始化date try { date = sdf.parse(str); //Mon Jan 14 00:00:00 CST 2013 } catch (ParseException e) { e.printStackTrace(); }
4、Date轉換爲Stringorm
//java.util.Date轉換爲String Date date = new Date(); DateFormat format = new SimpleDateFormat("yyyy-MM-dd"); String str = format.format(date); //2013-01-14
5、String轉化爲java.sql.Dateget
//String轉換爲java.sql.Date String str = "2013-01-14"; SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); java.sql.Date sdate = null; //初始化 try { java.util.Date udate = sdf.parse(str); sdate = new java.sql.Date(udate.getTime()); //2013-01-14 } catch (ParseException e) { e.printStackTrace(); }