【问题标题】:Java - Take two strings and combine into a single date objectJava - 取两个字符串并组合成一个日期对象
【发布时间】:2021-01-14 19:26:04
【问题描述】:

我正在尝试获取两个字符串并将其转换为 Date 对象。我无法确定需要使用哪些格式。

第一个字符串是日期,格式为:5th Jan

第二个字符串是时间,格式为:8:15

主要问题是 5th 的格式是什么

【问题讨论】:

  • 在我看来,链接的副本不正确。提问者希望将给定形式 5th Jan 8:15 的字符串解析为日期,而不是格式化要输出的具有后缀 th 的日期。
  • @Eritrean - 我同意你的看法。我还想提一下 OP 已经提到:The first string is a date and is in the format of : 5th Jan The second string is a time and is in the format of : 8:15 即有两个单独的字符串。
  • @Eritrean 哎呀,我看错了。
  • @LiveandLetLive 请重新关闭这些重复的地址解析:1234
  • @SotiriosDelimanolis - 这些链接中的每一个都部分满足要求,例如这不仅仅是解析;这也是关于结合日期和时间。我们还可以找到一些关于该要求的问题,但是如果我们开始基于两个或更多关于不同类型要求的问题开始将问题标记为重复,那么关于 SO 的大多数问题都可以标记为重复。您在 SO 方面的经验比我多近 9 年,因此,我并没有挑战您的意见;相反,我只是根据我对 SO 的有限经验提出我的观点。

标签: java dateformatter


【解决方案1】:

由于您的日期字符串 5th Jan 没有年份,因此您必须使用一些默认年份,例如当前年份,您可以从LocalDate.now() 获取。您可以使用DateTimeFormatterBuilder#parseDefaulting 设置默认值。此外,您还可以制作解析器 使用DateTimeFormatterBuilder#parseCaseInsensitive 不区分大小写。

为了解析日期字符串5th Jan,您可以使用模式d'th' MMM。但是,为了处理 3rd、1st 等其他后缀,您应该使用模式 d['th']['st']['rd']['nd'] MMM,其中方括号内的模式是可选的。

要解析像8:15 这样的时间字符串,您可以使用模式H:m

演示:

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.time.temporal.ChronoField;
import java.util.Locale;

public class Main {
    public static void main(String[] args) {
        LocalDate date = LocalDate.now();
        DateTimeFormatter dtfForDate = new DateTimeFormatterBuilder()
                                        .parseCaseInsensitive()
                                        .parseDefaulting(ChronoField.YEAR, date.getYear())
                                        .appendPattern("d['th']['st']['rd']['nd'] MMM")
                                        .toFormatter(Locale.ENGLISH);

        DateTimeFormatter dtfForTime = DateTimeFormatter.ofPattern("H:m", Locale.ENGLISH);

        String strDate = "5th Jan";
        String strTime = "8:15";

        LocalDateTime ldt = LocalDate.parse(strDate, dtfForDate)
                                        .atTime(LocalTime.parse(strTime, dtfForTime));

        // Print the default string value i.e. the value returned by ldt.toString()
        System.out.println(ldt);

        // The default format omits seconds and fraction of second if they are 0. In
        // order to retain them in the output string, you can use DateTimeFormatter
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("uuuu-MM-dd'T'HH:mm:ss");
        String formatted = dtf.format(ldt);
        System.out.println(formatted);
    }
}

输出:

2021-01-05T08:15
2021-01-05T08:15:00

Trail: Date Time 了解现代日期时间 API。

【讨论】:

  • 不错的答案。为了完整起见,我将添加第一个、第二个和第三个 .appendPattern("d['st']['nd']['rd']['th'] MMM") 的后缀
  • @Eritrean - 谢谢。在您编写所需更改的同时更新了答案?。
  • 有一个问题告诉我它找不到 parseCaseInsensitive
  • 我需要的总是英语
  • @CeriTurner - 我已经更新了答案以满足这一要求。我建议您更新问题并在问题中添加此要求。如有任何疑问/问题,请随时发表评论。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-25
  • 2017-02-04
  • 1970-01-01
  • 1970-01-01
  • 2015-02-11
  • 2022-01-04
相关资源
最近更新 更多