Skip to main content
We’ve updated our Terms of Service. A new AI Addendum clarifies how Stack Overflow utilizes AI interactions.
Code Golf

Return to Answer

Fixing "2nd" case, previously avoided because of "Monday" and "Sunday"
Source Link
Olivier Grégoire
  • 14.5k
  • 3
  • 33
  • 56

Java (OpenJDK 8), 193190 + 26 = 219216 bytes

import java.time.format.*;
s->DateTimeFormatter.ofPattern("uuuu/MM/dd").format(DateTimeFormatter.ofPattern("[EEEE, ][d ]MMMM [d, ]uuuu").withResolverStyle(ResolverStyle.STRICT).parse(s.replaceAll("st|rd|th|"(2\\d)nd"[a-z].","1ドル")))

Try it online! Try it online!

Important note: it was shorter to also validate the day of week instead of ditching it, so that validation is included!

I haven't tried with SimpleDateFormat beyond the obvious cases which all accepted dates like 30 February. So I had to ditch it and I used Java 8's DateTimeFormatter.

Explanation

"[EEEE, ][d ]MMMM [d, ]uuuu"

This format means :

  • optional day-of-week followed by comma and space [EEEE, ] (happens in Sunday, ...),
  • followed by optional day with space [d ],
  • followed by month in full letters MMMM and space,
  • followed by optional day with comma and space [d, ],
  • followed by year of era uuuu to let the parser know we're in Gregorian era.

Code:

import java.time.format.*; // Required for DateTimeFormatter, *and* ResolverStyle
s->DateTimeFormatter.ofPattern("uuuu/MM/dd") // Output format
 .format(
 DateTimeFormatter.ofPattern("[EEEE, ][d ]MMMM [d, ]uuuu") // Input format
 .withResolverStyle(ResolverStyle.STRICT) // Invalidates xxxx-02-30 instead of transforming it into xxxx-02-28
 .parse(
 s.replaceAll("st|rd|th|"(2\\d)nd"[a-z].","1ドル") // remove useless stuff around the date, special case for// "nd"remove st, nd, rd, th
 )
 )

Credits

  • 2 bytes in the regex thanks to Neil.

Java (OpenJDK 8), 193 + 26 = 219 bytes

import java.time.format.*;
s->DateTimeFormatter.ofPattern("uuuu/MM/dd").format(DateTimeFormatter.ofPattern("[EEEE, ][d ]MMMM [d, ]uuuu").withResolverStyle(ResolverStyle.STRICT).parse(s.replaceAll("st|rd|th|(2)nd","1ドル")))

Try it online!

Important note: it was shorter to also validate the day of week instead of ditching it, so that validation is included!

I haven't tried with SimpleDateFormat beyond the obvious cases which all accepted dates like 30 February. So I had to ditch it and I used Java 8's DateTimeFormatter.

Explanation

"[EEEE, ][d ]MMMM [d, ]uuuu"

This format means :

  • optional day-of-week followed by comma and space [EEEE, ] (happens in Sunday, ...),
  • followed by optional day with space [d ],
  • followed by month in full letters MMMM and space,
  • followed by optional day with comma and space [d, ],
  • followed by year of era uuuu to let the parser know we're in Gregorian era.

Code:

import java.time.format.*; // Required for DateTimeFormatter, *and* ResolverStyle
s->DateTimeFormatter.ofPattern("uuuu/MM/dd") // Output format
 .format(
 DateTimeFormatter.ofPattern("[EEEE, ][d ]MMMM [d, ]uuuu") // Input format
 .withResolverStyle(ResolverStyle.STRICT) // Invalidates xxxx-02-30 instead of transforming it into xxxx-02-28
 .parse(
 s.replaceAll("st|rd|th|(2)nd","1ドル") // remove useless stuff around the date, special case for "nd"
 )
 )

Credits

  • 2 bytes in the regex thanks to Neil.

Java (OpenJDK 8), 190 + 26 = 216 bytes

import java.time.format.*;
s->DateTimeFormatter.ofPattern("uuuu/MM/dd").format(DateTimeFormatter.ofPattern("[EEEE, ][d ]MMMM [d, ]uuuu").withResolverStyle(ResolverStyle.STRICT).parse(s.replaceAll("(\\d)[a-z].","1ドル")))

Try it online!

Important note: it was shorter to also validate the day of week instead of ditching it, so that validation is included!

I haven't tried with SimpleDateFormat beyond the obvious cases which all accepted dates like 30 February. So I had to ditch it and I used Java 8's DateTimeFormatter.

Explanation

"[EEEE, ][d ]MMMM [d, ]uuuu"

This format means :

  • optional day-of-week followed by comma and space [EEEE, ] (happens in Sunday, ...),
  • followed by optional day with space [d ],
  • followed by month in full letters MMMM and space,
  • followed by optional day with comma and space [d, ],
  • followed by year of era uuuu to let the parser know we're in Gregorian era.

Code:

import java.time.format.*; // Required for DateTimeFormatter, *and* ResolverStyle
s->DateTimeFormatter.ofPattern("uuuu/MM/dd") // Output format
 .format(
 DateTimeFormatter.ofPattern("[EEEE, ][d ]MMMM [d, ]uuuu") // Input format
 .withResolverStyle(ResolverStyle.STRICT) // Invalidates xxxx-02-30 instead of transforming it into xxxx-02-28
 .parse(
 s.replaceAll("(\\d)[a-z].","1ドル") // remove st, nd, rd, th
 )
 )

Credits

  • 2 bytes in the regex thanks to Neil.
Fixing "2nd" case, previously avoided because of "Monday" and "Sunday"
Source Link
Olivier Grégoire
  • 14.5k
  • 3
  • 33
  • 56

Java (OpenJDK 8), 185193 + 26 = 211219 bytes

import java.time.format.*;
s->DateTimeFormatter.ofPattern("uuuu/MM/dd").format(DateTimeFormatter.ofPattern("[EEEE, ][d ]MMMM [d, ]uuuu").withResolverStyle(ResolverStyle.STRICT).parse(s.replaceAll("st|rd|th""st|rd|th|(2)nd","""1ドル")))

Try it online! Try it online!

Important note: it was shorter to also validate the day of week instead of ditching it, so that validation is included!

I haven't tried with SimpleDateFormat beyond the obvious cases which all accepted dates like 30 February. So I had to ditch it and I used Java 8's DateTimeFormatter.

Explanation

"[EEEE, ][d ]MMMM [d, ]uuuu"

This format means :

  • optional day-of-week followed by comma and space [EEEE, ] (happens in Sunday, ...),
  • followed by optional day with space [d ],
  • followed by month in full letters MMMM and space,
  • followed by optional day with comma and space [d, ],
  • followed by year of era uuuu to let the parser know we're in Gregorian era.

Code:

import java.time.format.*; // Required for DateTimeFormatter, *and* ResolverStyle
s->DateTimeFormatter.ofPattern("uuuu/MM/dd") // Output format
 .format(
 DateTimeFormatter.ofPattern("[EEEE, ][d ]MMMM [d, ]uuuu") // Input format
 .withResolverStyle(ResolverStyle.STRICT) // Invalidates xxxx-02-30 instead of transforming it into xxxx-02-28
 .parse(
 s.replaceAll("st|rd|th""st|rd|th|(2)nd","""1ドル") // remove useless stuff around the date, special case for "nd"
 )
 )

Credits

  • 2 bytes in the regex thanks to Neil.

Java (OpenJDK 8), 185 + 26 = 211 bytes

import java.time.format.*;
s->DateTimeFormatter.ofPattern("uuuu/MM/dd").format(DateTimeFormatter.ofPattern("[EEEE, ][d ]MMMM [d, ]uuuu").withResolverStyle(ResolverStyle.STRICT).parse(s.replaceAll("st|rd|th","")))

Try it online!

Important note: it was shorter to also validate the day of week instead of ditching it, so that validation is included!

I haven't tried with SimpleDateFormat beyond the obvious cases which all accepted dates like 30 February. So I had to ditch it and I used Java 8's DateTimeFormatter.

Explanation

"[EEEE, ][d ]MMMM [d, ]uuuu"

This format means :

  • optional day-of-week followed by comma and space [EEEE, ] (happens in Sunday, ...),
  • followed by optional day with space [d ],
  • followed by month in full letters MMMM and space,
  • followed by optional day with comma and space [d, ],
  • followed by year of era uuuu to let the parser know we're in Gregorian era.

Code:

import java.time.format.*; // Required for DateTimeFormatter, *and* ResolverStyle
s->DateTimeFormatter.ofPattern("uuuu/MM/dd") // Output format
 .format(
 DateTimeFormatter.ofPattern("[EEEE, ][d ]MMMM [d, ]uuuu") // Input format
 .withResolverStyle(ResolverStyle.STRICT) // Invalidates xxxx-02-30 instead of transforming it into xxxx-02-28
 .parse(
 s.replaceAll("st|rd|th","") // remove useless stuff around the date
 )
 )

Credits

  • 2 bytes in the regex thanks to Neil.

Java (OpenJDK 8), 193 + 26 = 219 bytes

import java.time.format.*;
s->DateTimeFormatter.ofPattern("uuuu/MM/dd").format(DateTimeFormatter.ofPattern("[EEEE, ][d ]MMMM [d, ]uuuu").withResolverStyle(ResolverStyle.STRICT).parse(s.replaceAll("st|rd|th|(2)nd","1ドル")))

Try it online!

Important note: it was shorter to also validate the day of week instead of ditching it, so that validation is included!

I haven't tried with SimpleDateFormat beyond the obvious cases which all accepted dates like 30 February. So I had to ditch it and I used Java 8's DateTimeFormatter.

Explanation

"[EEEE, ][d ]MMMM [d, ]uuuu"

This format means :

  • optional day-of-week followed by comma and space [EEEE, ] (happens in Sunday, ...),
  • followed by optional day with space [d ],
  • followed by month in full letters MMMM and space,
  • followed by optional day with comma and space [d, ],
  • followed by year of era uuuu to let the parser know we're in Gregorian era.

Code:

import java.time.format.*; // Required for DateTimeFormatter, *and* ResolverStyle
s->DateTimeFormatter.ofPattern("uuuu/MM/dd") // Output format
 .format(
 DateTimeFormatter.ofPattern("[EEEE, ][d ]MMMM [d, ]uuuu") // Input format
 .withResolverStyle(ResolverStyle.STRICT) // Invalidates xxxx-02-30 instead of transforming it into xxxx-02-28
 .parse(
 s.replaceAll("st|rd|th|(2)nd","1ドル") // remove useless stuff around the date, special case for "nd"
 )
 )

Credits

  • 2 bytes in the regex thanks to Neil.
added 49 characters in body
Source Link
Olivier Grégoire
  • 14.5k
  • 3
  • 33
  • 56

Java (OpenJDK 8), 187185 + 26 = 213211 bytes

import java.time.format.*;
s->DateTimeFormatter.ofPattern("uuuu/MM/dd").format(DateTimeFormatter.ofPattern("[EEEE, ][d ]MMMM [d, ]uuuu").withResolverStyle(ResolverStyle.STRICT).parse(s.replaceAll("(st|rd|th)""st|rd|th","")))

Try it online! Try it online!

Important note: it was shorter to also validate the day of week instead of ditching it, so that validation is included!

I haven't tried with SimpleDateFormat beyond the obvious cases which all accepted dates like 30 February. So I had to ditch it and I used Java 8's DateTimeFormatter.

Explanation

"[EEEE, ][d ]MMMM [d, ]uuuu"

This format means :

  • optional day-of-week followed by comma and space [EEEE, ] (happens in Sunday, ...),
  • followed by optional day with space [d ],
  • followed by month in full letters MMMM and space,
  • followed by optional day with comma and space [d, ],
  • followed by year of era uuuu to let the parser know we're in Gregorian era.

Code:

import java.time.format.*; // Required for DateTimeFormatter, *and* ResolverStyle
s->DateTimeFormatter.ofPattern("uuuu/MM/dd") // Output format
 .format(
 DateTimeFormatter.ofPattern("[EEEE, ][d ]MMMM [d, ]uuuu") // Input format
 .withResolverStyle(ResolverStyle.STRICT) // Invalidates xxxx-02-30 instead of transforming it into xxxx-02-28
 .parse(
 s.replaceAll("(st|rd|th)""st|rd|th","") // remove useless stuff around the date
 )
 )

Credits

  • 2 bytes in the regex thanks to Neil.

Java (OpenJDK 8), 187 + 26 = 213 bytes

import java.time.format.*;
s->DateTimeFormatter.ofPattern("uuuu/MM/dd").format(DateTimeFormatter.ofPattern("[EEEE, ][d ]MMMM [d, ]uuuu").withResolverStyle(ResolverStyle.STRICT).parse(s.replaceAll("(st|rd|th)","")))

Try it online!

Important note: it was shorter to also validate the day of week instead of ditching it, so that validation is included!

I haven't tried with SimpleDateFormat beyond the obvious cases which all accepted dates like 30 February. So I had to ditch it and I used Java 8's DateTimeFormatter.

Explanation

"[EEEE, ][d ]MMMM [d, ]uuuu"

This format means :

  • optional day-of-week followed by comma and space [EEEE, ] (happens in Sunday, ...),
  • followed by optional day with space [d ],
  • followed by month in full letters MMMM and space,
  • followed by optional day with comma and space [d, ],
  • followed by year of era uuuu to let the parser know we're in Gregorian era.

Code:

import java.time.format.*; // Required for DateTimeFormatter, *and* ResolverStyle
s->DateTimeFormatter.ofPattern("uuuu/MM/dd") // Output format
 .format(
 DateTimeFormatter.ofPattern("[EEEE, ][d ]MMMM [d, ]uuuu") // Input format
 .withResolverStyle(ResolverStyle.STRICT) // Invalidates xxxx-02-30 instead of transforming it into xxxx-02-28
 .parse(
 s.replaceAll("(st|rd|th)","") // remove useless stuff around the date
 )
 )

Java (OpenJDK 8), 185 + 26 = 211 bytes

import java.time.format.*;
s->DateTimeFormatter.ofPattern("uuuu/MM/dd").format(DateTimeFormatter.ofPattern("[EEEE, ][d ]MMMM [d, ]uuuu").withResolverStyle(ResolverStyle.STRICT).parse(s.replaceAll("st|rd|th","")))

Try it online!

Important note: it was shorter to also validate the day of week instead of ditching it, so that validation is included!

I haven't tried with SimpleDateFormat beyond the obvious cases which all accepted dates like 30 February. So I had to ditch it and I used Java 8's DateTimeFormatter.

Explanation

"[EEEE, ][d ]MMMM [d, ]uuuu"

This format means :

  • optional day-of-week followed by comma and space [EEEE, ] (happens in Sunday, ...),
  • followed by optional day with space [d ],
  • followed by month in full letters MMMM and space,
  • followed by optional day with comma and space [d, ],
  • followed by year of era uuuu to let the parser know we're in Gregorian era.

Code:

import java.time.format.*; // Required for DateTimeFormatter, *and* ResolverStyle
s->DateTimeFormatter.ofPattern("uuuu/MM/dd") // Output format
 .format(
 DateTimeFormatter.ofPattern("[EEEE, ][d ]MMMM [d, ]uuuu") // Input format
 .withResolverStyle(ResolverStyle.STRICT) // Invalidates xxxx-02-30 instead of transforming it into xxxx-02-28
 .parse(
 s.replaceAll("st|rd|th","") // remove useless stuff around the date
 )
 )

Credits

  • 2 bytes in the regex thanks to Neil.
added 136 characters in body
Source Link
Olivier Grégoire
  • 14.5k
  • 3
  • 33
  • 56
Loading
Source Link
Olivier Grégoire
  • 14.5k
  • 3
  • 33
  • 56
Loading

AltStyle によって変換されたページ (->オリジナル) /