|
| 1 | + |
| 2 | + |
| 3 | +import org.junit.Test; |
| 4 | + |
| 5 | +import java.text.SimpleDateFormat; |
| 6 | +import java.time.*; |
| 7 | +import java.time.format.DateTimeFormatter; |
| 8 | +import java.util.Calendar; |
| 9 | +import java.util.Date; |
| 10 | +import java.util.GregorianCalendar; |
| 11 | +import java.util.Locale; |
| 12 | + |
| 13 | +import static java.time.format.DateTimeFormatter.*; |
| 14 | +import static org.junit.Assert.assertEquals; |
| 15 | + |
| 16 | + |
| 17 | +public class Java8FormatDate { |
| 18 | + |
| 19 | + @Test |
| 20 | + public void testFormatDate() { |
| 21 | + |
| 22 | + //create a date object by using Calendar |
| 23 | + Calendar calendar = new GregorianCalendar(2020, Calendar.AUGUST, 9); |
| 24 | + Date date = calendar.getTime(); |
| 25 | + |
| 26 | + //create a pattern |
| 27 | + String pattern = "dd/MM/yyyy"; |
| 28 | + SimpleDateFormat dateFormatter = new SimpleDateFormat(pattern); |
| 29 | + // format the date |
| 30 | + String olympicDate = dateFormatter.format(date); |
| 31 | + //Validate |
| 32 | + assertEquals("09/08/2020", olympicDate); |
| 33 | + |
| 34 | + } |
| 35 | + |
| 36 | + @Test |
| 37 | + public void testFormatDateLongPattern() { |
| 38 | + //create a date |
| 39 | + int year = 2020; |
| 40 | + int month = Calendar.AUGUST; |
| 41 | + int day = 9; |
| 42 | + int hourOfDay = 23; |
| 43 | + int minute = 59; |
| 44 | + int second = 59; |
| 45 | + Calendar calendar = new GregorianCalendar(year, month, day, hourOfDay, |
| 46 | + minute, second); |
| 47 | + |
| 48 | + Date date = calendar.getTime(); |
| 49 | + |
| 50 | + //create a long pattern with en_US locale |
| 51 | + String pattern = "EEEEE MMMMM yyyy HH:mm:ss.SSS"; |
| 52 | + SimpleDateFormat dateFormatter = |
| 53 | + new SimpleDateFormat(pattern, new Locale("en", "US")); |
| 54 | + |
| 55 | + //format the date |
| 56 | + String olympicDate = dateFormatter.format(date); |
| 57 | + |
| 58 | + //validate |
| 59 | + assertEquals("Sunday August 2020 23:59:59.000", olympicDate); |
| 60 | + |
| 61 | + } |
| 62 | + @Test |
| 63 | + public void testFormatLocalDate() { |
| 64 | + |
| 65 | + //create a LocalDate object |
| 66 | + LocalDate date = LocalDate.of(2020, Month.AUGUST, 9); |
| 67 | + |
| 68 | + //create a pattern |
| 69 | + String pattern = "dd/MM/yyyy"; |
| 70 | + DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern); |
| 71 | + |
| 72 | + // format the date |
| 73 | + String olympicDate = formatter.format(date); |
| 74 | + |
| 75 | + //Validate |
| 76 | + assertEquals("09/08/2020", olympicDate); |
| 77 | + |
| 78 | + } |
| 79 | + @Test |
| 80 | + public void testFormatLocalDateUsingPreDefinedFormatter() { |
| 81 | + |
| 82 | + //create a LocalDate object |
| 83 | + LocalDate date = LocalDate.of(2020, Month.AUGUST, 9); |
| 84 | + |
| 85 | + // format the date |
| 86 | + String olympicDate = ISO_DATE.format(date); |
| 87 | + assertEquals("2020-08-09", olympicDate); |
| 88 | + |
| 89 | + //or use as a pattern |
| 90 | + olympicDate = date.format(ISO_DATE); |
| 91 | + assertEquals("2020-08-09", olympicDate); |
| 92 | + |
| 93 | + } |
| 94 | + @Test |
| 95 | + public void testFormatLocalDateTime() { |
| 96 | + |
| 97 | + //create a LocalDate object |
| 98 | + LocalDateTime dt = LocalDateTime.of(2020, Month.AUGUST, 9, 23, 59, 59); |
| 99 | + |
| 100 | + //create a pattern |
| 101 | + String pattern = "yyyy/MM/dd HH:mm:ss"; |
| 102 | + DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern); |
| 103 | + |
| 104 | + // format the date |
| 105 | + String olympicDate = formatter.format(dt); |
| 106 | + assertEquals("2020/08/09 23:59:59", olympicDate); |
| 107 | + |
| 108 | + } |
| 109 | + @Test |
| 110 | + public void testFormatLocalDateTimeUsingPreDefinedFormatters() { |
| 111 | + |
| 112 | + //create a LocalDate object |
| 113 | + LocalDateTime dt = LocalDateTime.of(2020, Month.AUGUST, 9, 23, 59, 59); |
| 114 | + |
| 115 | + // format the date |
| 116 | + String olympicDate = ISO_LOCAL_DATE_TIME.format(dt); |
| 117 | + assertEquals("2020-08-09T23:59:59", olympicDate); |
| 118 | + |
| 119 | + // or using as a pattern |
| 120 | + olympicDate = dt.format(ISO_LOCAL_DATE_TIME); |
| 121 | + assertEquals("2020-08-09T23:59:59", olympicDate); |
| 122 | + |
| 123 | + } |
| 124 | + |
| 125 | + @Test |
| 126 | + public void testFormatZonedDateTime() { |
| 127 | + |
| 128 | + //create a LocalDate object |
| 129 | + LocalDateTime dt = LocalDateTime.of(2020, Month.AUGUST, 9, 23, 59, 59); |
| 130 | + ZonedDateTime japanZonedDT = ZonedDateTime.of(dt, ZoneId.of("Asia/Tokyo")); |
| 131 | + // format the date |
| 132 | + String pattern = "yyyy/MM/dd HH:mm:ssXXX"; |
| 133 | + DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern); |
| 134 | + |
| 135 | + String olympicDate = formatter.format(japanZonedDT); |
| 136 | + assertEquals("2020/08/09 23:59:59+09:00", olympicDate); |
| 137 | + |
| 138 | + } |
| 139 | + @Test |
| 140 | + public void testFormatZonedDateTimeUsingPreDefinedFormatters() { |
| 141 | + |
| 142 | + //create a LocalDate object |
| 143 | + LocalDateTime dt = LocalDateTime.of(2020, Month.AUGUST, 9, 23, 59, 59); |
| 144 | + ZonedDateTime japanZonedDT = ZonedDateTime.of(dt, ZoneId.of("Asia/Tokyo")); |
| 145 | + // format the date |
| 146 | + String olympicDate = ISO_OFFSET_DATE_TIME.format(japanZonedDT); |
| 147 | + assertEquals("2020-08-09T23:59:59+09:00", olympicDate); |
| 148 | + |
| 149 | + // or using as a pattern |
| 150 | + olympicDate = japanZonedDT.format(ISO_OFFSET_DATE_TIME); |
| 151 | + assertEquals("2020-08-09T23:59:59+09:00", olympicDate); |
| 152 | + |
| 153 | + } |
| 154 | +} |
0 commit comments