Date default in my database is 1900-01-01 00:00:00, but i need to format the output when the page is displayed this is my code,But the result wasn't what I wanted.
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();
}
}
output : Asia/Shanghai 1900-01-01 00:05:43