由于项目已经定好了一些类中变量的属性,用于接收时间参数的变量是Integer,20210624,但是前端需要返回的是2021-06-24,我试了format,parse等方法,始终不行,请问各位大佬怎么解决
package T1;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Test {
public static void main(String[] args) throws ParseException {
// 使用指定的格式化程序将格式解析为日期对象
Integer value = 20151006;
SimpleDateFormat originalFormat = new SimpleDateFormat("yyyyMMdd");
Date date = originalFormat.parse(value.toString());
// 日期没有格式。它仅表示从1970-01-01开始的时间(以毫秒为单位)的特定实例。将日期格式化为期望的格式,则可以使用其他格式化程序
System.out.println(date);
SimpleDateFormat newFormat = new SimpleDateFormat("yyyy-MM-dd");
String formatedDate = newFormat.format(date);
System.out.println(formatedDate);
}
}
运行结果
用string传递。