1990年1月1日星期一00

1990年1月1日星期一00

本文介绍了如何转换此字符串"1990年1月1日星期一00:00:00 AEDT"变成"1990101"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个字符串"Mon Jan 01 00:00:00 AEDT 1990",我需要将其转换为"yyyyMMdd"格式,因此在这种情况下应为"19900101".

I have a string "Mon Jan 01 00:00:00 AEDT 1990" and I need to convert it into the format "yyyyMMdd" so in this case it would be "19900101".

我认为可以使用正则表达式来做到这一点,这样我就可以从字符串中提取年份,月份(但需要将Jan转换为01等)和日期,但是我并不精通正则表达式.有人有什么想法吗?

I think it's possible to do this with Regular Expressions so that I could pull out the year, month(but would need to convert Jan to 01 and etc) and day from the string but I am not well versed in Regular Expressions. Anyone have any ideas?

推荐答案

tl; dr

正则表达式过于矫kill过正.

tl;dr

Regex is overkill.

这里是使用Java中内置的 java.time 类的单线解决方案.

Here is a one-liner solution using java.time classes built into Java.

ZonedDateTime                            // Represent a moment as seen through the wall-clock time used by the people of a certain region (a time zone).
.parse(                                  // Parse the input text.
    "Mon Jan 01 00:00:00 AEDT 1990" ,
    DateTimeFormatter.ofPattern(
        "EEE MMM dd HH:mm:ss z uuuu" ,   // Specify a custom formatting pattern to match our input.
        Locale.US                        // Specify a `Locale` for the human language to use in translating the name of month& day-of-week.
    )                                    // Returns a `DateTimeFormatter` object.
)                                        // Returns a `ZonedDateTime` object.
.toLocalDate()                           // Extract the date, without time-of-day and without time zone.
.format(                                 // Generate text to represent the value of our `LocalDate` object.
    DateTimeFormatter.BASIC_ISO_DATE     // Use the predefined formatting pattern YYYYMMDD.
)                                        // Returns a String.

java.time

正则表达式对此过于矫kill.

java.time

Regex is overkill for this.

现代方法使用 java.time 类.

指定自定义格式设置模式以适合您的输入.

Specify a custom formatting pattern to fit your input.

指定语言环境以方便翻译星期几的名称和月份的名称.

Specify a locale to facilitate translating the name of day-of-week and name of month.

将其解析为ZonedDateTime,即通过特定地区(时区)的人们所使用的挂钟时间看到的时刻.

Parse as a ZonedDateTime, a moment as seen through the wall-clock time used by the people of a specific region (a time zone).

String input = "Mon Jan 01 00:00:00 AEDT 1990";
Locale locale = Locale.US;
DateTimeFormatter f = DateTimeFormatter.ofPattern( "EEE MMM dd HH:mm:ss z uuuu" , locale );
ZonedDateTime zdt = ZonedDateTime.parse( input , f );

System.out.println( "zdt: " + zdt );

顺便说一句,您的输入字符串采用糟糕格式.它使用2-4个字符的伪时区,这些伪时区不是实际的时区,不是标准化的,也不是唯一的!另一个问题取决于英语.而且很难解析.教育人们发布有关 ISO 8601 标准(用于交换日期-时间值作为文本.

By the way, your input string is in a terrible format. It uses the 2-4 character pseudo-zones that are not actual time zones, not standardized, and are not unique! Another problem is depending on English. And it is difficult to parse. Educate the people publishing your data about the beauty of the ISO 8601 standard, created for exchanging date-time values as text.

您只想要日期.因此,提取一个LocalDate.

You want only the date. So extract a LocalDate.

LocalDate ld = zdt.toLocalDate() ;  // Extract only the date, leaving behind the time-of-day and the time zone.

您已经在DateTimeFormatter类中定义了所需的输出格式.日期的标准 ISO 8601 格式为YYYY-MM-DD.的一种变体称为基本",表示它最大程度地减少了分隔符YYYYMMDD的使用.

Your desired output format has already been defined in the DateTimeFormatter class. The standard ISO 8601 format for a date is YYYY-MM-DD. A variation of that is known as "Basic" meaning it minimizes the use of delimiters: YYYYMMDD.

String output = ld.format( DateTimeFormatter.BASIC_ISO_DATE ) ;

这篇关于如何转换此字符串"1990年1月1日星期一00:00:00 AEDT"变成"1990101"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 11:24