如何格式化数据库中的以下日期时间值?

时间:2019-07-03 10:48:04

标签: java date date-formatting localdate

我正在使用Java 8 LocalDateTime,并将其转换为Timestamp格式。 timeStampOfTodayMidNight的输出为= 2019-07-03 00:00:00.0。但是我想要这种格式= 03-JUL-19 15:05:31。我尝试过很多格式化,但是没有。有人能帮我一下吗? 谢谢。

LocalTime midnight = LocalTime.MIDNIGHT;
LocalDate today = LocalDate.now(ZoneId.of(environment.getProperty("axipay.time.zone")));
LocalDateTime todayMidnight = LocalDateTime.of(today, midnight);
LocalDateTime tomorrowMidnight = todayMidnight.plusDays(1);
Date timeStampOfTodayMidNight = java.sql.Timestamp.valueOf(todayMidnight);

2 个答案:

答案 0 :(得分:0)

使用SimpleDateFormat

尝试一下:

LocalTime midnight = LocalTime.MIDNIGHT;
LocalDate today = LocalDate.now(ZoneId.of("America/New_York"));
LocalDateTime todayMidnight = LocalDateTime.of(today, midnight);
LocalDateTime tomorrowMidnight = todayMidnight.plusDays(1);
Date timeStampOfTodayMidNight = java.sql.Timestamp.valueOf(todayMidnight);
SimpleDateFormat format = new SimpleDateFormat("dd-MMM-yy hh:mm:ss", Locale.US);
String formatDate = format.format(timeStampOfTodayMidNight); 
System.out.println(formatDate.toUpperCase());

输出:

03-JUL-19 12:00:00

答案 1 :(得分:0)

我实际上意识到的事实是,如果您的数据库列采用时间戳格式,并且POJO类相关字段被DATE数据类型包围,那么您实际上不希望传递格式化日期。只需传递DATE类型即可。