[size=medium][b][color=brown]DateFormat和SimpleDateFormat类,是操作Date的工具类
作用:完成字符串和时间对象的相互转化[/color][/b][/size]
作用:完成字符串和时间对象的相互转化[/color][/b][/size]
package com.out.test;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import com.out.util.DateUtil;
public class Test {
public static void main(String[] args) throws ParseException {
//DateFormat是抽象类,不能直接new,要new子类
//DateFormat只有一个子类SimpleDateFormat
//"yyyy年MM月dd日大风水电工" 叫格式化字符串,用来格式化Date类型时间,返回字符串
//格式化字符串如:
//yyyy-MM-dd hh:mm:ss只要符合这个想应的字符,
//如MM等,就可以自动匹配,去格式化,这个可以查看API,里面好多介绍
DateFormat fmt = new SimpleDateFormat("yyyy年MM月dd日大风水电工");
Date date = new Date();
//1.按照格式化字符串,将时间对象转化成字符串
String time = fmt.format(date);
System.out.println(time);
DateFormat fmt2 = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
String time2 = fmt2.format(date);
System.out.println(time2);
//2.按照格式化字符串,将字符串转化成时间对象
String time3 = "2014-03-04";
DateFormat fmt3 = new SimpleDateFormat("yyyy-MM-dd");
Date date2 = fmt3.parse(time3);
System.out.println(date2);
}
}