我数据库中的默认日期是1900-01-01 00:00:00, 但是我需要在页面显示时格式化输出 这是我的代码,但是结果不是我想要的。
package com.niocoder;
import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;
/**
* Created on 2018/12/13.
*
* @author zlf
* @email i@merryyou.cn
* @since 1.0
*/
public class DateTest {
private static final String DEFALUT_TIME = "1900-01-01 00:00:00";
public static void main(String[] args) {
String dateStr = "1900-01-01 00:00:00";
Timestamp timestamp = Timestamp.valueOf(dateStr);
System.out.println(ZoneId.systemDefault());
String resultDate = getDateTimerStrByDate(new java.util.Date(timestamp.getTime()));
resultDate = DEFALUT_TIME.equals(resultDate) ? "" : resultDate;
System.out.println(resultDate);
}
public static String getDateTimerStrByDate(Date date) {
return dateToLocalDateTime(date).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
}
public static LocalDateTime dateToLocalDateTime(Date date) {
return date.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
}
}
输出:亚洲/上海1900-01-01 00:05:43