关于Date类型数据处理

String转换成date
DateFormat format=new SimpleDateFormat("yyyy-mm-dd");
		DateFormat format2=new SimpleDateFormat("yyyy年mm月dd日hh时mm分ss秒");
		Date date =null;
		String str=null;
		str="2007-9-8";
	
		date=format.parse(str);
		date=format2.parse(str);   

取得当前时间
java.text.SimpleDateFormat formater = new java.text.SimpleDateFormat("yyyy-MM-dd  HH:mm:ss");
	String date = formater.format(new java.util.Date());

如何在JSP页面获得系统当前时间
2009-05-20 11:44
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); //时
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.

//如果只要年月日,可以用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这类型

你可能感兴趣的:(java,Date)