我正在使用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);
最佳答案
使用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