import java.util.*;
import java.text.*;
public class Main {
public static void main(String[] args) throws Exception {
DateFormat df=DateFormat.getDateInstance();
DateFormat dft=DateFormat.getDateTimeInstance();
DateFormat dft1=DateFormat.getDateTimeInstance(DateFormat.FULL,DateFormat.FULL);//也可以自定义格式
Date date1=dft.parse("2011-02-10 20:6:57.98");//此处漏掉了02中的0 只是日期的
System.out.println(df.format(date1));
System.out.println(dft.format(date1));
System.out.println(dft1.format(date1));
//DateFormat 是一个抽象类,以下是他的子类
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");//定义格式
//年:4位(少写一位的话,将变成2位)
SimpleDateFormat sdf1=new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss.SSS");
Date date2=sdf.parse("2011-2-10 20:06:57.098");//把一个字符串转换为Date对象
System.out.println(sdf.format(date2));
System.out.println(sdf1.format(date2));
}
}
所以常用的还是SimplDateFormat
2011-2-10
2011-2-10 20:06:57
2011年2月10日 星期四 下午08时06分57秒 CST
2011-02-10 20:06:57.098
2011年02月10日 20:06:57.098
本文介绍使用Java进行日期和时间的格式化与解析操作,重点展示了DateFormat类及其子类SimpleDateFormat的功能,包括日期时间的自定义格式化及字符串与日期对象之间的相互转换。
1952

被折叠的 条评论
为什么被折叠?



