怎么把这个字符串“ Mon Jan 01 00:00:00 AEDT 1990”转换成“ 1990101”?

Joh*_*ael 0 java regex string parsing date

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

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

Bas*_*que 6

tl;dr

Regex is overkill.

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.
Run Code Online (Sandbox Code Playgroud)

19900101

java.time

Regex is overkill for this.

The modern approach uses java.time classes.

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 );
Run Code Online (Sandbox Code Playgroud)

zdt: 1990-01-01T00:00+11:00[Australia/Sydney]

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.
Run Code Online (Sandbox Code Playgroud)

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 ) ;
Run Code Online (Sandbox Code Playgroud)

19900101