Bael 1600 internationalization and localization (#3807)
* BAEL-1600 Internationalization and Localization in Java 8 The essential code for the article. * BAEL-1600 Internationalization and Localization in Java 8 The essential code for the article. * BAEL-1600 Internationalization and Localization in Java 8 JUnit test names fixed. Default Timezones set. * BAEL-1600 Internationalization and Localization in Java 8 Default Timezones set.
This commit is contained in:
parent
c6c7ffbcc1
commit
489e3186bc
|
@ -0,0 +1,110 @@
|
|||
package com.baeldung.internationalization;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.text.DateFormat;
|
||||
import java.text.DateFormatSymbols;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
import java.util.GregorianCalendar;
|
||||
import java.util.Locale;
|
||||
import java.util.TimeZone;
|
||||
|
||||
public class DateFormatUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenGregorianCalendar_whenLocaleSpecificDateInstance_givenLanguageSpecificMonths() {
|
||||
GregorianCalendar gregorianCalendar = new GregorianCalendar(2018, 1, 1, 10, 15, 20);
|
||||
Date date = gregorianCalendar.getTime();
|
||||
|
||||
DateFormat itInstance = DateFormat.getDateInstance(DateFormat.FULL, Locale.ITALY);
|
||||
DateFormat usInstance = DateFormat.getDateInstance(DateFormat.FULL, Locale.US);
|
||||
|
||||
Assert.assertEquals("giovedì 1 febbraio 2018", itInstance.format(date));
|
||||
Assert.assertEquals("Thursday, February 1, 2018", usInstance.format(date));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenGregorianCalendar_whenDateInstanceWithDifferentFormats_givenSpecificDateFormatting() {
|
||||
GregorianCalendar gregorianCalendar = new GregorianCalendar(2018, 1, 1, 10, 15, 20);
|
||||
Date date = gregorianCalendar.getTime();
|
||||
|
||||
DateFormat fullInstance = DateFormat.getDateInstance(DateFormat.FULL, Locale.ITALY);
|
||||
DateFormat mediumInstance = DateFormat.getDateInstance(DateFormat.MEDIUM, Locale.ITALY);
|
||||
|
||||
Assert.assertEquals("giovedì 1 febbraio 2018", fullInstance.format(date));
|
||||
Assert.assertEquals("1-feb-2018", mediumInstance.format(date));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenGregorianCalendar_whenTimeInstanceWithDifferentFormats_givenSpecificTimeFormatting() {
|
||||
GregorianCalendar gregorianCalendar = new GregorianCalendar(2018, 1, 1, 10, 15, 20);
|
||||
gregorianCalendar.setTimeZone(TimeZone.getTimeZone("CET"));
|
||||
TimeZone.setDefault(TimeZone.getTimeZone("CET"));
|
||||
Date date = gregorianCalendar.getTime();
|
||||
|
||||
DateFormat fullInstance = DateFormat.getTimeInstance(DateFormat.FULL, Locale.ITALY);
|
||||
DateFormat mediumInstance = DateFormat.getTimeInstance(DateFormat.MEDIUM, Locale.ITALY);
|
||||
|
||||
Assert.assertEquals("10.15.20 CET", fullInstance.format(date));
|
||||
Assert.assertEquals("10.15.20" , mediumInstance.format(date));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenGregorianCalendar_whenDateTimeInstanceWithDifferentFormats_givenSpecificDateTimeFormatting() {
|
||||
GregorianCalendar gregorianCalendar = new GregorianCalendar(2018, 1, 1, 10, 15, 20);
|
||||
gregorianCalendar.setTimeZone(TimeZone.getTimeZone("CET"));
|
||||
TimeZone.setDefault(TimeZone.getTimeZone("CET"));
|
||||
Date date = gregorianCalendar.getTime();
|
||||
|
||||
DateFormat ffInstance = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL, Locale.ITALY);
|
||||
DateFormat smInstance = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.MEDIUM, Locale.ITALY);
|
||||
|
||||
Assert.assertEquals("giovedì 1 febbraio 2018 10.15.20 CET", ffInstance.format(date));
|
||||
Assert.assertEquals("01/02/18 10.15.20", smInstance.format(date));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenGregorianCalendar_whenLocaleSpecificDateTimeInstance_givenLocaleSpecificFormatting() {
|
||||
GregorianCalendar gregorianCalendar = new GregorianCalendar(2018, 1, 1, 10, 15, 20);
|
||||
gregorianCalendar.setTimeZone(TimeZone.getTimeZone("CET"));
|
||||
TimeZone.setDefault(TimeZone.getTimeZone("CET"));
|
||||
Date date = gregorianCalendar.getTime();
|
||||
|
||||
DateFormat itInstance = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL, Locale.ITALY);
|
||||
DateFormat jpInstance = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL, Locale.JAPAN);
|
||||
|
||||
Assert.assertEquals("giovedì 1 febbraio 2018 10.15.20 CET", itInstance.format(date));
|
||||
Assert.assertEquals("2018年2月1日 10時15分20秒 CET", jpInstance.format(date));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenGregorianCalendar_whenCustomizedSimpleDateFormat_thenSpecificMonthRepresentations() {
|
||||
GregorianCalendar gregorianCalendar = new GregorianCalendar(2018, 1, 1, 10, 15, 20);
|
||||
Date date = gregorianCalendar.getTime();
|
||||
Locale.setDefault(new Locale("pl", "PL"));
|
||||
|
||||
SimpleDateFormat fullMonthDateFormat = new SimpleDateFormat("dd-MMMM-yyyy HH:mm:ss:SSS");
|
||||
SimpleDateFormat shortMonthsimpleDateFormat = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss:SSS");
|
||||
|
||||
Assert.assertEquals("01-lutego-2018 10:15:20:000", fullMonthDateFormat.format(date));
|
||||
Assert.assertEquals("01-02-2018 10:15:20:000" , shortMonthsimpleDateFormat.format(date));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenGregorianCalendar_whenCustomizedDateFormatSymbols_thenChangedDayNames() {
|
||||
GregorianCalendar gregorianCalendar = new GregorianCalendar(2018, 1, 1, 10, 15, 20);
|
||||
Date date = gregorianCalendar.getTime();
|
||||
Locale.setDefault(new Locale("pl", "PL"));
|
||||
|
||||
DateFormatSymbols dateFormatSymbols = new DateFormatSymbols();
|
||||
dateFormatSymbols.setWeekdays(new String[]{"A", "B", "C", "D", "E", "F", "G", "H"});
|
||||
SimpleDateFormat standardDateFormat = new SimpleDateFormat("EEEE-MMMM-yyyy HH:mm:ss:SSS");
|
||||
SimpleDateFormat newDaysDateFormat = new SimpleDateFormat("EEEE-MMMM-yyyy HH:mm:ss:SSS", dateFormatSymbols);
|
||||
|
||||
Assert.assertEquals("czwartek-lutego-2018 10:15:20:000", standardDateFormat.format(date));
|
||||
Assert.assertEquals("F-lutego-2018 10:15:20:000", newDaysDateFormat.format(date));
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,46 @@
|
|||
package com.baeldung.internationalization;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.ZoneId;
|
||||
import java.time.ZonedDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.time.format.FormatStyle;
|
||||
import java.util.Locale;
|
||||
import java.util.TimeZone;
|
||||
|
||||
public class DateTimeFormatterUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenDefaultUsLocaleAndDateTimeAndPattern_whenFormatWithDifferentLocales_thenGettingLocalizedDateTimes() {
|
||||
Locale.setDefault(Locale.US);
|
||||
LocalDateTime localDateTime = LocalDateTime.of(2018, 1, 1, 10, 15, 50, 500);
|
||||
String pattern = "dd-MMMM-yyyy HH:mm:ss.SSS";
|
||||
|
||||
DateTimeFormatter defaultTimeFormatter = DateTimeFormatter.ofPattern(pattern);
|
||||
DateTimeFormatter plTimeFormatter = DateTimeFormatter.ofPattern(pattern, new Locale("pl", "PL"));
|
||||
DateTimeFormatter deTimeFormatter = DateTimeFormatter.ofPattern(pattern).withLocale(Locale.GERMANY);
|
||||
|
||||
Assert.assertEquals("01-January-2018 10:15:50.000", defaultTimeFormatter.format(localDateTime));
|
||||
Assert.assertEquals("01-stycznia-2018 10:15:50.000", plTimeFormatter.format(localDateTime));
|
||||
Assert.assertEquals("01-Januar-2018 10:15:50.000", deTimeFormatter.format(localDateTime));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenDateTimeAndTimeZone_whenFormatWithDifferentLocales_thenGettingLocalizedZonedDateTimes() {
|
||||
Locale.setDefault(Locale.US);
|
||||
LocalDateTime localDateTime = LocalDateTime.of(2018, 1, 1, 10, 15, 50, 500);
|
||||
ZoneId losAngelesTimeZone = TimeZone.getTimeZone("America/Los_Angeles").toZoneId();
|
||||
|
||||
DateTimeFormatter localizedFormatter = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.FULL);
|
||||
DateTimeFormatter frLocalizedFormatter =
|
||||
DateTimeFormatter.ofLocalizedDateTime(FormatStyle.FULL).withLocale(Locale.FRANCE);
|
||||
String formattedDateTime = localizedFormatter.format(ZonedDateTime.of(localDateTime, losAngelesTimeZone));
|
||||
String frFormattedDateTime = frLocalizedFormatter.format(ZonedDateTime.of(localDateTime, losAngelesTimeZone));
|
||||
|
||||
Assert.assertEquals("Monday, January 1, 2018 10:15:50 AM PST", formattedDateTime);
|
||||
Assert.assertEquals("lundi 1 janvier 2018 10 h 15 PST", frFormattedDateTime);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,75 @@
|
|||
package com.baeldung.internationalization;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.text.DecimalFormat;
|
||||
import java.text.DecimalFormatSymbols;
|
||||
import java.text.NumberFormat;
|
||||
import java.util.Currency;
|
||||
import java.util.Locale;
|
||||
|
||||
public class NumbersCurrenciesFormattingUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenDifferentLocalesAndDoubleNumber_whenNumberInstance_thenDifferentOutput() {
|
||||
Locale usLocale = Locale.US;
|
||||
Locale plLocale = new Locale("pl", "PL");
|
||||
Locale deLocale = Locale.GERMANY;
|
||||
double number = 102_300.456d;
|
||||
|
||||
NumberFormat usNumberFormat = NumberFormat.getInstance(usLocale);
|
||||
NumberFormat plNumberFormat = NumberFormat.getInstance(plLocale);
|
||||
NumberFormat deNumberFormat = NumberFormat.getInstance(deLocale);
|
||||
|
||||
Assert.assertEquals(usNumberFormat.format(number), "102,300.456");
|
||||
Assert.assertEquals(plNumberFormat.format(number), "102 300,456");
|
||||
Assert.assertEquals(deNumberFormat.format(number), "102.300,456");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenDifferentLocalesAndDoubleAmount_whenCurrencyInstance_thenDifferentOutput() {
|
||||
Locale usLocale = Locale.US;
|
||||
Locale plLocale = new Locale("pl", "PL");
|
||||
Locale deLocale = Locale.GERMANY;
|
||||
double number = 102_300.456d;
|
||||
|
||||
NumberFormat usNumberFormat = NumberFormat.getCurrencyInstance(usLocale);
|
||||
NumberFormat plNumberFormat = NumberFormat.getCurrencyInstance(plLocale);
|
||||
NumberFormat deNumberFormat = NumberFormat.getCurrencyInstance(deLocale);
|
||||
|
||||
Assert.assertEquals(usNumberFormat.format(number), "$102,300.46");
|
||||
Assert.assertEquals(plNumberFormat.format(number), "102 300,46 zł");
|
||||
Assert.assertEquals(deNumberFormat.format(number), "102.300,46 €");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenLocaleAndNumber_whenSpecificDecimalFormat_thenSpecificOutput() {
|
||||
Locale.setDefault(Locale.FRANCE);
|
||||
double number = 102_300.456d;
|
||||
|
||||
DecimalFormat zeroDecimalFormat = new DecimalFormat("000000000.0000");
|
||||
DecimalFormat hashDecimalFormat = new DecimalFormat("###,###.#");
|
||||
DecimalFormat dollarDecimalFormat = new DecimalFormat("$###,###.##");
|
||||
|
||||
Assert.assertEquals(zeroDecimalFormat.format(number), "000102300,4560");
|
||||
Assert.assertEquals(hashDecimalFormat.format(number), "102 300,5");
|
||||
Assert.assertEquals(dollarDecimalFormat.format(number), "$102 300,46");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenLocaleAndNumber_whenSpecificDecimalFormatSymbols_thenSpecificOutput() {
|
||||
Locale.setDefault(Locale.FRANCE);
|
||||
double number = 102_300.456d;
|
||||
|
||||
DecimalFormatSymbols decimalFormatSymbols = DecimalFormatSymbols.getInstance();
|
||||
decimalFormatSymbols.setGroupingSeparator('^');
|
||||
decimalFormatSymbols.setDecimalSeparator('@');
|
||||
DecimalFormat separatorsDecimalFormat = new DecimalFormat("$###,###.##");
|
||||
separatorsDecimalFormat.setGroupingSize(4);
|
||||
separatorsDecimalFormat.setCurrency(Currency.getInstance(Locale.JAPAN));
|
||||
separatorsDecimalFormat.setDecimalFormatSymbols(decimalFormatSymbols);
|
||||
|
||||
Assert.assertEquals(separatorsDecimalFormat.format(number), "$10^2300@46");
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue