SimpleDateFormat is a concrete class for formatting and parsing dates in a locale-sensitive manner. It allows for formatting (date -> text), parsing (text -> date), and normalization.
SimpleDateFormat allows you to start by choosing any user-defined patterns for date-time formatting. However, you are encouraged to create a date-time formatter with either getTimeInstance, getDateInstance, or getDateTimeInstance in DateFormat. Each of these class methods can return a date/time formatter initialized with a default format pattern. You may modify the format pattern using the applyPattern methods as desired. For more information on using these methods, see DateFormat.
package c1;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Test1 {
public static void main(String[] args) {
SimpleDateFormat sdf = new SimpleDateFormat ("MM/dd/yyyy HH:mm:ss");
SimpleDateFormat sdf1 = new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss");
//日期转换成字符串
String str=sdf.format(new Date());
System.out.println(str);
// 字符串转换成日期
try {
Date d1=sdf.parse(str);
String str1=sdf1.format(d1);
System.out.println(str1);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Output:
06/08/2010 15:08:22
2010-06-08 15:08:22
阅读(1075) | 评论(0) | 转发(0) |