在Java 8中使用replaceAll函数作为日期和时间戳

时间:2021-11-28 15:31:04

I would like to set two different inputs from date and timestamp into a date in UTC at the end. I am not sure if using replaceAll function would work.

我想在日期和时间戳中将两个不同的输入设置为最后的UTC日期。我不确定使用replaceAll函数是否可行。

Inputs:

  • string date = "06/04/2019" which the format is "MM/dd/yyyy"
  • string date =“06/04/2019”格式为“MM / dd / yyyy”

  • string timestamp = "15/03/2018 15:46:59.000386 PM -05:00" which the format is "dd/MM/yyyy hh:mm:ss.SSSSSS a XXX"
  • string timestamp =“15/03/2018 15:46:59.000386 PM -05:00”格式为“dd / MM / yyyy hh:mm:ss.SSSSSS a XXX”

Output: "06/04/2019 15:46:59.000386 PM -05:00" then transform it into UTC...

输出:“06/04/2019 15:46:59.000386 PM -05:00”然后将其转换为UTC ...

2 个解决方案

#1


0  

Executive answer: No, String.replaceAll will not work for converting your datetime nor your date to UTC.

执行回答:不,String.replaceAll不能用于将日期时间和日期转换为UTC。

Java (and Java 8 and later in particular) has very good support for parsing, handling, converting and formatting dates and times. For example:

Java(以及特别是Java 8及更高版本)非常支持解析,处理,转换和格式化日期和时间。例如:

    DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("dd/MM/uuuu");
    DateTimeFormatter timestampFormatter = DateTimeFormatter
            .ofPattern("dd/MM/yyyy hh:mm:ss.SSSSSS a XXX", Locale.ENGLISH);

    String dateString = "06/04/2019";
    String timestampString = "15/03/2018 03:46:59.000386 PM -05:00";
    OffsetDateTime timestampOnDate = OffsetDateTime
            .parse(timestampString, timestampFormatter)
            .with(LocalDate.parse(dateString, dateFormatter));
    System.out.println("Output:        " + timestampOnDate.format(timestampFormatter));
    System.out.println("Output in UTC: "
            + timestampOnDate.withOffsetSameInstant(ZoneOffset.UTC)
                    .format(timestampFormatter));

This prints:

Output:        06/04/2019 03:46:59.000386 PM -05:00
Output in UTC: 06/04/2019 08:46:59.000386 PM Z

Your strings and formats didn’t quite make sense, so I have changed them a bit: MM/dd to dd/MM, 15 PM to 03 PM and therefore also HH (for hour of day) to hh (for hour within AM or PM). If these changes were not the correct ones, just make the ones you want instead.

你的字符串和格式没有多大意义,所以我稍微改了一下:MM / dd到dd / MM,下午15点到03点,因此也是HH(一天中的小时)到hh(在AM或小时内)下午)。如果这些变化不正确,那就改成你想要的变化吧。

Edit: Of course, if you insist, you may do

编辑:当然,如果你坚持,你可能会这样做

    timestampString = timestampString.replaceFirst("^\\d{2}/\\d{2}/\\d{4}", dateString);

— to obtain 06/04/2019 03:46:59.000386 PM -05:00 and then parse as before. I consider this code less readable, and it also won’t give you the validation of the date that was in the timestamp string at first. I recommend using the date-time classes for manipulating your date and timestamp as I do in the code example.

- 获取06/04/2019 03:46:59.000386 PM -05:00然后像以前一样解析。我认为这段代码的可读性较差,并且它也不会让您首先验证时间戳字符串中的日期。我建议使用日期时间类来操作日期和时间戳,就像我在代码示例中那样。

#2


1  

Just to complement the other answer above, you can also parse the LocalDate first, and then join it with OffsetTime to create the OffsetDateTime:

只是为了补充上面的其他答案,您还可以先解析LocalDate,然后将其与OffsetTime连接以创建OffsetDateTime:

OffsetDateTime timestampOnDate = LocalDate
    // parse date (dd/MM/uuuu)
    .parse(dateString, dateFormatter)
    // join with time and offset (ignores 15/03/2018) 
    .atTime(OffsetTime.parse(timestampString, timestampFormatter));

OffsetTime.parse will create an object that has the time and offset part (ignoring the date you don't want, in this case, "15/03/2018"), and joining it with a LocalDate produces the desired OffsetDateTime.

OffsetTime.parse将创建一个具有时间和偏移部分的对象(忽略您不想要的日期,在本例中为“15/03/2018”),并将其与LocalDate连接会生成所需的OffsetDateTime。

Not sure which code is better, though. I guess both are equivalent.

但不确定哪个代码更好。我猜两者都是等价的。


replaceAll might work, but only if the dateString is a valid date - which is checked when parsing. If the date string contains an invalid date, such as 35/99/0000, or a valid date in another format, or even some nonsense text, replaceAll won't complain and the result will be an incorrect string.

replaceAll可能有效,但前提是dateString是一个有效的日期 - 在解析时会检查。如果日期字符串包含无效日期(如35/99/0000)或其他格式的有效日期,甚至包含一些无意义文本,则replaceAll不会报告,结果将是不正确的字符串。

But parsing those invalid values as a date will throw an exception, so it's better to handle your data as the types they really represent.

但是将这些无效值解析为日期会抛出异常,因此最好将数据作为它们真正代表的类型来处理。

#1


0  

Executive answer: No, String.replaceAll will not work for converting your datetime nor your date to UTC.

执行回答:不,String.replaceAll不能用于将日期时间和日期转换为UTC。

Java (and Java 8 and later in particular) has very good support for parsing, handling, converting and formatting dates and times. For example:

Java(以及特别是Java 8及更高版本)非常支持解析,处理,转换和格式化日期和时间。例如:

    DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("dd/MM/uuuu");
    DateTimeFormatter timestampFormatter = DateTimeFormatter
            .ofPattern("dd/MM/yyyy hh:mm:ss.SSSSSS a XXX", Locale.ENGLISH);

    String dateString = "06/04/2019";
    String timestampString = "15/03/2018 03:46:59.000386 PM -05:00";
    OffsetDateTime timestampOnDate = OffsetDateTime
            .parse(timestampString, timestampFormatter)
            .with(LocalDate.parse(dateString, dateFormatter));
    System.out.println("Output:        " + timestampOnDate.format(timestampFormatter));
    System.out.println("Output in UTC: "
            + timestampOnDate.withOffsetSameInstant(ZoneOffset.UTC)
                    .format(timestampFormatter));

This prints:

Output:        06/04/2019 03:46:59.000386 PM -05:00
Output in UTC: 06/04/2019 08:46:59.000386 PM Z

Your strings and formats didn’t quite make sense, so I have changed them a bit: MM/dd to dd/MM, 15 PM to 03 PM and therefore also HH (for hour of day) to hh (for hour within AM or PM). If these changes were not the correct ones, just make the ones you want instead.

你的字符串和格式没有多大意义,所以我稍微改了一下:MM / dd到dd / MM,下午15点到03点,因此也是HH(一天中的小时)到hh(在AM或小时内)下午)。如果这些变化不正确,那就改成你想要的变化吧。

Edit: Of course, if you insist, you may do

编辑:当然,如果你坚持,你可能会这样做

    timestampString = timestampString.replaceFirst("^\\d{2}/\\d{2}/\\d{4}", dateString);

— to obtain 06/04/2019 03:46:59.000386 PM -05:00 and then parse as before. I consider this code less readable, and it also won’t give you the validation of the date that was in the timestamp string at first. I recommend using the date-time classes for manipulating your date and timestamp as I do in the code example.

- 获取06/04/2019 03:46:59.000386 PM -05:00然后像以前一样解析。我认为这段代码的可读性较差,并且它也不会让您首先验证时间戳字符串中的日期。我建议使用日期时间类来操作日期和时间戳,就像我在代码示例中那样。

#2


1  

Just to complement the other answer above, you can also parse the LocalDate first, and then join it with OffsetTime to create the OffsetDateTime:

只是为了补充上面的其他答案,您还可以先解析LocalDate,然后将其与OffsetTime连接以创建OffsetDateTime:

OffsetDateTime timestampOnDate = LocalDate
    // parse date (dd/MM/uuuu)
    .parse(dateString, dateFormatter)
    // join with time and offset (ignores 15/03/2018) 
    .atTime(OffsetTime.parse(timestampString, timestampFormatter));

OffsetTime.parse will create an object that has the time and offset part (ignoring the date you don't want, in this case, "15/03/2018"), and joining it with a LocalDate produces the desired OffsetDateTime.

OffsetTime.parse将创建一个具有时间和偏移部分的对象(忽略您不想要的日期,在本例中为“15/03/2018”),并将其与LocalDate连接会生成所需的OffsetDateTime。

Not sure which code is better, though. I guess both are equivalent.

但不确定哪个代码更好。我猜两者都是等价的。


replaceAll might work, but only if the dateString is a valid date - which is checked when parsing. If the date string contains an invalid date, such as 35/99/0000, or a valid date in another format, or even some nonsense text, replaceAll won't complain and the result will be an incorrect string.

replaceAll可能有效,但前提是dateString是一个有效的日期 - 在解析时会检查。如果日期字符串包含无效日期(如35/99/0000)或其他格式的有效日期,甚至包含一些无意义文本,则replaceAll不会报告,结果将是不正确的字符串。

But parsing those invalid values as a date will throw an exception, so it's better to handle your data as the types they really represent.

但是将这些无效值解析为日期会抛出异常,因此最好将数据作为它们真正代表的类型来处理。