增加两个日期[重复]

问题描述 投票:0回答:1

这个问题在这里已有答案:

我有两个类型String的日期,第一个格式为"yyyy.MM.dd",第二个"HH: mm"

他们使用Date将它们转换为SimpleDateFormat类型;

所以,我需要把它们放在一起,也就是说,结果应该是"yyyy.MM.dd HH: mm"除了如何添加它们还有其他选择,除了

date.setHours();
date.setMinutes();
java android
1个回答
0
投票

您可以使用“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));
    }
© www.soinside.com 2019 - 2024. All rights reserved.