在程序开发中,经常用到 java 日期 Date 类型与字符串 String ,之间的转换
package nice;
import java.text.SimpleDateFormat;
import java.util.Date;
public class DemoT {
public static void main(String[] args) {
//获取时间
Date date = new Date();
//设置转换后的日期格式 yyyy-MM-dd HH:mm:ss
SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println(sdf1.format(date));
//设置转换后的日期格式 yyyy-MM-dd HH:mm
SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm");
System.out.println(sdf2.format(date));
//设置转换后的日期格式 yyyy-MM-dd
SimpleDateFormat sdf3 = new SimpleDateFormat("yyyy-MM-dd");
System.out.println(sdf3.format(date));
}
}