java常用类-------Date类和DateFormat类

package cn.zxg.PackgeUse;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
* 测试时间对象和字符串之间的转换
* DateFormat抽象类和SimpleDateFormat实现类的使用
*/

public class TestFormat {
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("1990年01月18日 12时30分35秒");
System.out.println(date);
//测试其他格式字符,比如用D测试今天处于当年第几天
DateFormat df3=new SimpleDateFormat("D");
String str3=df3.format(new Date());
System.out.println(str3);



}
}

猜你喜欢

转载自www.cnblogs.com/zzzao/p/10903645.html