1: java.sql.Timestamp a = new java.sql.Timestamp(System.currentTimeMillis()); 2: java.util.Date a = new java.util.Date(); theDate=new java.util.Date();//得到当前的日期和时间,如 2003-06-03 08:00:00 3: java.util.Date a; a.setTime(System.currentTimeMillis());
4:
currTime=new java.sql.Time(new java.util.Date().getTime());//得到当前时间。 如 08:00:00 currDate=new java.sql.Date(new java.util.Date().getTime());//得到当前日期。 如 2003-06-03
5:
Date date = Calendar.getInstance().getTime(); SimpleDateFormat sdf = new SimpleDateFormat( "yyyy/MM/dd HH:MM:SS "); String sDate = sdf.format(date);
6:
Calendar todaysDate = new GregorianCalendar(); year = todaysDate.get(Calendar.YEAR); month = todaysDate.get(Calendar.MONTH) + 1; day = todaysDate.get(Calendar.DAY_OF_MONTH); hourOfDay = todaysDate.get(Calendar.HOUR_OF_DAY); // 24小时制 hour = todaysDate.get(Calendar.HOUR); // 12小时制 minute = todaysDate.get(Calendar.MINUTE); second = todaysDate.get(Calendar.SECOND);
7: Date(int year, int month, int date) ... 使用api, 查看Date constructor.
Deprecated. As of JDK version 1.1, replaced by Calendar.set(year + 1900, month, date) or GregorianCalendar(year + 1900, month, date).
public static void main(String[] args) { System.out.println(new GregorianCalendar(19 + 1900, 1, 4).getTime()); }
Result: Tue Feb 04 00:00:00 CST 1919
得到指定时间:
System.out.println(new GregorianCalendar(19 + 1900, 1, 4,9,9,9).getTime());Result: Tue Feb 04 09:09:09 CST 1919
8:SimpleDateFormat constructor
DateFormat format1 = new SimpleDateFormat("yyyy-MM-dd"); DateFormat format 2= new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒"); Date date = null; String str = null; // String转Date str = "2007-1-18"; try { date = format1.parse(str); data = format2.parse(str); } catch (ParseException e) { e.printStackTrace(); } //Date转String date=new Date(); str=format1.format(date); str=format2.format(date);