|
| 1 | +package intermediate; |
| 2 | + |
| 3 | +import static com.sandwich.koan.constant.KoanConstants.__; |
| 4 | +import static com.sandwich.util.Assert.assertEquals; |
| 5 | + |
| 6 | +import java.text.DateFormat; |
| 7 | +import java.text.ParseException; |
| 8 | +import java.util.Calendar; |
| 9 | +import java.util.Date; |
| 10 | + |
| 11 | +import com.sandwich.koan.Koan; |
| 12 | + |
| 13 | + |
| 14 | +public class AboutDates { |
| 15 | + |
| 16 | + private Date date = new Date(100010001000L); |
| 17 | + |
| 18 | + @Koan |
| 19 | + public void dateToString() { |
| 20 | + assertEquals(date.toString(), __); |
| 21 | + } |
| 22 | + |
| 23 | + @Koan |
| 24 | + public void changingDateValue() { |
| 25 | + int oneHourInMiliseconds = 360000; |
| 26 | + date.setTime(date.getTime() + oneHourInMiliseconds); |
| 27 | + assertEquals(date.toString(), __); |
| 28 | + } |
| 29 | + |
| 30 | + @Koan |
| 31 | + public void usingCalendarToChangeDates() { |
| 32 | + Calendar cal = Calendar.getInstance(); |
| 33 | + cal.setTime(date); |
| 34 | + cal.add(Calendar.MONTH, 1); |
| 35 | + assertEquals(cal.getTime().toString(), __); |
| 36 | + } |
| 37 | + |
| 38 | + @Koan |
| 39 | + public void usingRollToChangeDatesDoesntWrapOtherFields() { |
| 40 | + Calendar cal = Calendar.getInstance(); |
| 41 | + cal.setTime(date); |
| 42 | + cal.roll(Calendar.MONTH, 12); |
| 43 | + assertEquals(cal.getTime().toString(), __); |
| 44 | + } |
| 45 | + |
| 46 | + @Koan |
| 47 | + public void usingDateFormatToFormatDate() { |
| 48 | + String formattedDate = DateFormat.getDateInstance().format(date); |
| 49 | + assertEquals(formattedDate, __); |
| 50 | + } |
| 51 | + |
| 52 | + @Koan |
| 53 | + public void usingDateFormatToFormatDateShort() { |
| 54 | + String formattedDate = DateFormat.getDateInstance(DateFormat.SHORT).format(date); |
| 55 | + assertEquals(formattedDate, __); |
| 56 | + } |
| 57 | + |
| 58 | + @Koan |
| 59 | + public void usingDateFormatToFormatDateFull() { |
| 60 | + String formattedDate = DateFormat.getDateInstance(DateFormat.FULL).format(date); |
| 61 | + // There is also DateFormat.MEDIUM and DateFormat.LONG... you get the idea ;-) |
| 62 | + assertEquals(formattedDate, __); |
| 63 | + } |
| 64 | + |
| 65 | + @Koan |
| 66 | + public void usingDateFormatToParseDates() throws ParseException { |
| 67 | + DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.SHORT); |
| 68 | + Date date2 = dateFormat.parse("Sat Sep 01 12:34:46 CET 2001"); |
| 69 | + assertEquals(date2.toString(), __); |
| 70 | + // What happened to the time? What do you need to change to keep the time as well? |
| 71 | + } |
| 72 | +} |
0 commit comments