Skip to content

Instantly share code, notes, and snippets.

@josejuan
Last active December 23, 2021 13:44
Show Gist options
  • Save josejuan/63fb623bd77eb623b5eca36e9d3afff8 to your computer and use it in GitHub Desktop.
Save josejuan/63fb623bd77eb623b5eca36e9d3afff8 to your computer and use it in GitHub Desktop.
/*
PHP <8.1 does not support
https://unicode-org.github.io/icu/userguide/format_parse/datetime/index#datetimepatterngenerator
*/
package com.computermind.sandbox.icu;
import com.ibm.icu.text.DateTimePatternGenerator;
import com.ibm.icu.text.SimpleDateFormat;
import com.ibm.icu.util.TimeZone;
import com.ibm.icu.util.ULocale;
import java.util.Date;
import java.util.List;
import static java.util.Arrays.asList;
public class Test {
public static void main(String... args) {
// TODO config here locales and generators
final List<String> locales = asList("es", "en-US");
final List<String> icuGenerators = asList("MMMMdjmm", "yMMMM", "M/d", "EEEEMMMMdy");
final Date sample = new Date(2021 - 1900, 7, 25, 9, 31, 7);
System.out.printf(" const ICU_GENERATOR_2_PATTERN = [%n");
for (final String generator : icuGenerators) {
System.out.printf(" \"%s\" => [%n", generator);
for (final String locale : locales) {
final ULocale ulocale = ULocale.forLanguageTag(locale);
final DateTimePatternGenerator dtpg = DateTimePatternGenerator.getInstance(ulocale);
final String pattern = dtpg.getBestPattern(generator);
SimpleDateFormat format = new SimpleDateFormat(pattern, ulocale);
format.setTimeZone(TimeZone.getDefault());
System.out.printf(" \"%s\" => \"%s\", // %s%n", locale, pattern, format.format(sample));
}
System.out.printf(" ],%n");
}
System.out.printf(" ];%n");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment