Dateformat和SimpleDateformat的使用——时间和字符串互相转换
package zuoye2;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* Dateformat和SimpleDateformat的使用——时间和字符串互相转换
* @author 110
* Dateformat类的作用
* 把时间对象转化成指定格式的字符串。反之,把指定格式的字符串转化成时间对象。
* Dateformat是一个抽象类,一般使用它的子类SimpleDateFormat类来实现。
*/
public class study06 {
public static void main(String[] args) throws ParseException {
//把时间对象按照“格式字符串指定的格式”转成相应的字符串
DateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
String str = df.format(new Date());
System.out.println(str);
//把字符串按照“格式字符串指定的格式”转成相应的时间对象
DateFormat df2 = new SimpleDateFormat("yyyy年MM月dd日 hh时mm分ss秒");
Date date = df2.parse("1983年05月10日 10时45分59秒");
System.out.println(date);
//测试其他的格式字符。比如:利用D,获得本时间对象是所处年份的第几天.
DateFormat df3 = new SimpleDateFormat("D");
String str3 = df3.format(new Date());
System.out.println(str3);
}
}
格式化字符的含义