Date日期处理:
package cn.tedu.demo;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* 开始时间:2020年7月29日10:16:22
* @author cll
*
*/
public class Demo2 {
//Date类
public static void test1() throws ParseException{
System.out.println(new Date());
//日期和字符串的转换
SimpleDateFormat sdf =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
SimpleDateFormat sdf1 =new SimpleDateFormat("yyyy年MM/dd HH:mm^ss");
//把日期Date类型转换成想要的字符串
System.out.println(sdf.format(new Date()));
System.out.println(sdf1.format(new Date()));
//把字符串类型的日期转换成日期Date类型
String strDate ="2020-07-29 10:35:45";
System.out.println(sdf.parse(strDate));
//这里的格式必须和输出的年月日的格式相对应,否则可能会无法返回到原来得Date类型
String str ="2020年07/29 10:47^30";
System.out.println(sdf1.parse(str));
}
public static void main(String[] args) throws ParseException {
test1();
}
}
运行结果:
Wed Jul 29 10:48:12 CST 2020
2020-07-29 10:48:13
2020年07/29 10:48^13
Wed Jul 29 10:35:45 CST 2020
Wed Jul 29 10:47:30 CST 2020