项目开发过程中,有时候需要将时间的形式进行变换,时间的输入和输出不一致的转换,本文用于记录常用到的方法。
首先对sdf.format(a),sdf.parse(a)两个类型进行区分.
sdf.format(a):将String类型a转换为Date类型,输出结果为Date类型。
sdf.parse(a):将Date类型a转换为String类型,输出结果为String类型
【时间类型转换】
1.将String类型数据转为Date类型
String Time = "2019-05-22 08:10:12"
public static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date date = sdf.format(Time);
2.将Date类型数据转为String类型
Date date = new Date();
public static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String time = sdf.parse(date);
【时间格式转换】
将String类型的yyyy-MM-dd HH:mm:ss转换为String类型的yyyyMMddHHmmss
String Time = "2019-05-22 08:10:12"
public static SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
public static SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
String time = simpleDateFormat.format(sdf.parse(Time)); //首先将String类型变为Date类型,然后在将Date类型变为String类型