“获取当前系统时间”
不需要传递任何东西。
代码如下:
public java.util.Date getCurrentDate()
{
return new java.util.Date(System.currentTimeMillis());
}
不需要传递任何东西。
代码如下:
public java.util.Date getCurrentDate()
{
return new java.util.Date(System.currentTimeMillis());
}
像你的要求传进去的format可能是各种各样的,如果返回值是String的话倒还差不多,
但如果返回Date,format就没有用了。
SimpleDateFormat formatter=new SimpleDateFormat("yyyy-MM-dd");
Date currentTime_1 = new Date();
String dateString = formatter.format(currentTime_1);
-------------- -------
"yyyy.MM.dd G 'at' hh:mm:ss z" ->> 1996.07.10 AD at 15:08:56 PDT
"EEE, MMM d, ''yy" ->> Wed, July 10, '96
"h:mm a" ->> 12:08 PM
"hh 'o''clock' a, zzzz" ->> 12 o'clock PM, Pacific Daylight Time
"K:mm a, z" ->> 0:00 PM, PST
"yyyyy.MMMMM.dd GGG hh:mm aaa" ->> 1996.July.10 AD 12:08 PM
Code Sample: SimpleTimeZone pdt = new SimpleTimeZone(-8 * 60 * 60 * 1000, "PST");
pdt.setStartRule(DateFields.APRIL, 1, DateFields.SUNDAY, 2*60*60*1000);
pdt.setEndRule(DateFields.OCTOBER, -1, DateFields.SUNDAY, 2*60*60*1000);
// Format the current time.
SimpleDateFormat formatter
= new SimpleDateFormat ("yyyy.MM.dd G 'at' hh:mm:ss a zzz");
Date currentTime_1 = new Date();
String dateString = formatter.format(currentTime_1);
// Parse the previous string back into a Date.
ParsePosition pos = new ParsePosition(0);
Date currentTime_2 = formatter.parse(dateString, pos);
import java.text.*; public Date getCurrentDate(String formatter){
String myString = DateFormat.getDateInstance().format(formatter);
return java.sql.Date.valueOf(myString );
}
不能是Date拉。还是给你个输出String的吧?
public static String getCurrentDate(String pattern) { Date date = new Date();
SimpleDateFormat df = (SimpleDateFormat)DateFormat.getDateInstance(); synchronized(df) { df.applyPattern(pattern); String strDate = df.format(date); return strDate != null ? strDate : "";
}
} System.out.println(getCurrentDate("MM/dd/yyyy"));
System.out.println(getCurrentDate("yyyy/MM/dd"));
System.out.println(getCurrentDate("yyyy.MM.dd G 'at' HH:mm:ss z"));
System.out.println(getCurrentDate("EEE, MMM d, ''yy" ));
System.out.println(getCurrentDate("EEE, d MMM yyyy HH:mm:ss Z"));
输出
08/13/2003
2003/08/13
2003.08.13 西暦 at 16:38:21 GMT+08:00
水, 8 13, '03
水, 13 8 2003 16:38:21 +0800