1:
import java.util.*;
int y,m,d,h,mm;
Calendar c = Calendar.getInstance();
y = c.get(Calendar.YEAR); //年
m = c.get(Calendar.MONTH) + 1; //月
d = c.get(Calendar.DAY_OF_MONTH); //日
h = c.get(Calendar.HOUR); //時(HOUR——十二小時制;HOUR_OF_DAY——二十四小時制)
mm = c.get(Calendar.MINUTE); //分
2:
java.util.Date d = new java.util.Date();
java.text.SimpleDateFormat dformat = new java.text.SimpleDateFormat("yyyy-MM-dd");
String datetime = dformat.format(d);
System.out.println(datetime);
3:
import java.util.*;
import java.text.*;
Date now = new Date();
DateFormat defaultFormat = DateFormat.getDateInstance();
DateFormat shortFormat = DateFormat.getDateInstance(DateFormat.SHORT);
DateFormat mediumFormat = DateFormat.getDateInstance(DateFormat.MEDIUM);
DateFormat longFormat = DateFormat.getDateInstance(DateFormat.LONG);
DateFormat fullFormat = DateFormat.getDateInstance(DateFormat.FULL);
String defaultDate = defaultFormat.format(now);
String shortDate = shortFormat.format(now);
String mediumDate = mediumFormat.format(now);
String longDate = longFormat.format(now);
String fullDate = fullFormat.format(now);
System.out.println("(Default) Today :" + defaultDate);
System.out.println("(SHORT) Today : " + shortDate);
System.out.println("(MEDIUM) Today :" + mediumDate);
System.out.println("(LONG) Today : " + longDate);
System.out.println("(FULL) Today : " + fullDate);
4.
<%
java.util.Date now=new java.util.Date();
out.println(now);
%>
如果只要年月日,可以用subString()截取.
或者用這個 String time=new SimpleDateFormat("yyyy-MM-dd").format(Calendar.getInstance().getTime);
5.
Date currentTime = new Date();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
String dateString = formatter.format(currentTime);
dataString 的格式就是2007-03-08這類型