这个问题在这里已有答案:
我有两个类型String
的日期,第一个格式为"yyyy.MM.dd"
,第二个"HH: mm"
他们使用Date
将它们转换为SimpleDateFormat
类型;
所以,我需要把它们放在一起,也就是说,结果应该是"yyyy.MM.dd HH: mm"
除了如何添加它们还有其他选择,除了
date.setHours();
date.setMinutes();
您可以使用“java.time.LocalDateTime”。这也允许您使用java.time.format.DateTimeFormatter进行格式化。
示例程序:
public static void main(String[] args) {
LocalDate date = LocalDate.of(2013, 1, 2);
LocalTime time = LocalTime.of(4, 5, 6);
LocalDateTime localDateTime = LocalDateTime.of(date, time);
DateTimeFormatter format = DateTimeFormatter.ofPattern("MMM d yyyy hh:mm a");
System.out.println(localDateTime.format(format));
}