|
| 1 | +package com.baeldung.internationalization; |
| 2 | + |
| 3 | +import org.junit.Assert; |
| 4 | +import org.junit.Test; |
| 5 | + |
| 6 | +import java.text.DateFormat; |
| 7 | +import java.text.DateFormatSymbols; |
| 8 | +import java.text.SimpleDateFormat; |
| 9 | +import java.util.Date; |
| 10 | +import java.util.GregorianCalendar; |
| 11 | +import java.util.Locale; |
| 12 | +import java.util.TimeZone; |
| 13 | + |
| 14 | +public class DateFormatUnitTest { |
| 15 | + |
| 16 | + @Test |
| 17 | + public void givenGregorianCalendar_whenLocaleSpecificDateInstance_givenLanguageSpecificMonths() { |
| 18 | + GregorianCalendar gregorianCalendar = new GregorianCalendar(2018, 1, 1, 10, 15, 20); |
| 19 | + Date date = gregorianCalendar.getTime(); |
| 20 | + |
| 21 | + DateFormat itInstance = DateFormat.getDateInstance(DateFormat.FULL, Locale.ITALY); |
| 22 | + DateFormat usInstance = DateFormat.getDateInstance(DateFormat.FULL, Locale.US); |
| 23 | + |
| 24 | + Assert.assertEquals("giovedì 1 febbraio 2018", itInstance.format(date)); |
| 25 | + Assert.assertEquals("Thursday, February 1, 2018", usInstance.format(date)); |
| 26 | + } |
| 27 | + |
| 28 | + @Test |
| 29 | + public void givenGregorianCalendar_whenDateInstanceWithDifferentFormats_givenSpecificDateFormatting() { |
| 30 | + GregorianCalendar gregorianCalendar = new GregorianCalendar(2018, 1, 1, 10, 15, 20); |
| 31 | + Date date = gregorianCalendar.getTime(); |
| 32 | + |
| 33 | + DateFormat fullInstance = DateFormat.getDateInstance(DateFormat.FULL, Locale.ITALY); |
| 34 | + DateFormat mediumInstance = DateFormat.getDateInstance(DateFormat.MEDIUM, Locale.ITALY); |
| 35 | + |
| 36 | + Assert.assertEquals("giovedì 1 febbraio 2018", fullInstance.format(date)); |
| 37 | + Assert.assertEquals("1-feb-2018", mediumInstance.format(date)); |
| 38 | + } |
| 39 | + |
| 40 | + @Test |
| 41 | + public void givenGregorianCalendar_whenTimeInstanceWithDifferentFormats_givenSpecificTimeFormatting() { |
| 42 | + GregorianCalendar gregorianCalendar = new GregorianCalendar(2018, 1, 1, 10, 15, 20); |
| 43 | + gregorianCalendar.setTimeZone(TimeZone.getTimeZone("CET")); |
| 44 | + TimeZone.setDefault(TimeZone.getTimeZone("CET")); |
| 45 | + Date date = gregorianCalendar.getTime(); |
| 46 | + |
| 47 | + DateFormat fullInstance = DateFormat.getTimeInstance(DateFormat.FULL, Locale.ITALY); |
| 48 | + DateFormat mediumInstance = DateFormat.getTimeInstance(DateFormat.MEDIUM, Locale.ITALY); |
| 49 | + |
| 50 | + Assert.assertEquals("10.15.20 CET", fullInstance.format(date)); |
| 51 | + Assert.assertEquals("10.15.20" , mediumInstance.format(date)); |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + public void givenGregorianCalendar_whenDateTimeInstanceWithDifferentFormats_givenSpecificDateTimeFormatting() { |
| 56 | + GregorianCalendar gregorianCalendar = new GregorianCalendar(2018, 1, 1, 10, 15, 20); |
| 57 | + gregorianCalendar.setTimeZone(TimeZone.getTimeZone("CET")); |
| 58 | + TimeZone.setDefault(TimeZone.getTimeZone("CET")); |
| 59 | + Date date = gregorianCalendar.getTime(); |
| 60 | + |
| 61 | + DateFormat ffInstance = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL, Locale.ITALY); |
| 62 | + DateFormat smInstance = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.MEDIUM, Locale.ITALY); |
| 63 | + |
| 64 | + Assert.assertEquals("giovedì 1 febbraio 2018 10.15.20 CET", ffInstance.format(date)); |
| 65 | + Assert.assertEquals("01/02/18 10.15.20", smInstance.format(date)); |
| 66 | + } |
| 67 | + |
| 68 | + @Test |
| 69 | + public void givenGregorianCalendar_whenLocaleSpecificDateTimeInstance_givenLocaleSpecificFormatting() { |
| 70 | + GregorianCalendar gregorianCalendar = new GregorianCalendar(2018, 1, 1, 10, 15, 20); |
| 71 | + gregorianCalendar.setTimeZone(TimeZone.getTimeZone("CET")); |
| 72 | + TimeZone.setDefault(TimeZone.getTimeZone("CET")); |
| 73 | + Date date = gregorianCalendar.getTime(); |
| 74 | + |
| 75 | + DateFormat itInstance = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL, Locale.ITALY); |
| 76 | + DateFormat jpInstance = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL, Locale.JAPAN); |
| 77 | + |
| 78 | + Assert.assertEquals("giovedì 1 febbraio 2018 10.15.20 CET", itInstance.format(date)); |
| 79 | + Assert.assertEquals("2018年2月1日 10時15分20秒 CET", jpInstance.format(date)); |
| 80 | + } |
| 81 | + |
| 82 | + @Test |
| 83 | + public void givenGregorianCalendar_whenCustomizedSimpleDateFormat_thenSpecificMonthRepresentations() { |
| 84 | + GregorianCalendar gregorianCalendar = new GregorianCalendar(2018, 1, 1, 10, 15, 20); |
| 85 | + Date date = gregorianCalendar.getTime(); |
| 86 | + Locale.setDefault(new Locale("pl", "PL")); |
| 87 | + |
| 88 | + SimpleDateFormat fullMonthDateFormat = new SimpleDateFormat("dd-MMMM-yyyy HH:mm:ss:SSS"); |
| 89 | + SimpleDateFormat shortMonthsimpleDateFormat = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss:SSS"); |
| 90 | + |
| 91 | + Assert.assertEquals("01-lutego-2018 10:15:20:000", fullMonthDateFormat.format(date)); |
| 92 | + Assert.assertEquals("01-02-2018 10:15:20:000" , shortMonthsimpleDateFormat.format(date)); |
| 93 | + } |
| 94 | + |
| 95 | + @Test |
| 96 | + public void givenGregorianCalendar_whenCustomizedDateFormatSymbols_thenChangedDayNames() { |
| 97 | + GregorianCalendar gregorianCalendar = new GregorianCalendar(2018, 1, 1, 10, 15, 20); |
| 98 | + Date date = gregorianCalendar.getTime(); |
| 99 | + Locale.setDefault(new Locale("pl", "PL")); |
| 100 | + |
| 101 | + DateFormatSymbols dateFormatSymbols = new DateFormatSymbols(); |
| 102 | + dateFormatSymbols.setWeekdays(new String[]{"A", "B", "C", "D", "E", "F", "G", "H"}); |
| 103 | + SimpleDateFormat standardDateFormat = new SimpleDateFormat("EEEE-MMMM-yyyy HH:mm:ss:SSS"); |
| 104 | + SimpleDateFormat newDaysDateFormat = new SimpleDateFormat("EEEE-MMMM-yyyy HH:mm:ss:SSS", dateFormatSymbols); |
| 105 | + |
| 106 | + Assert.assertEquals("czwartek-lutego-2018 10:15:20:000", standardDateFormat.format(date)); |
| 107 | + Assert.assertEquals("F-lutego-2018 10:15:20:000", newDaysDateFormat.format(date)); |
| 108 | + } |
| 109 | + |
| 110 | +} |
0 commit comments