001package intermediate;
002
003import com.sandwich.koan.Koan;
004
005import java.text.DateFormat;
006import java.text.ParseException;
007import java.text.SimpleDateFormat;
008import java.util.Calendar;
009import java.util.Date;
010
011import static com.sandwich.koan.constant.KoanConstants.__;
012import static com.sandwich.util.Assert.assertEquals;
013
014
015public class AboutDates {
016
017    private Date date = new Date(100010001000L);
018
019    @Koan
020    public void dateToString() {
021        assertEquals(date.toString(), __);
022    }
023
024    @Koan
025    public void changingDateValue() {
026        int oneHourInMiliseconds = 3600000;
027        date.setTime(date.getTime() + oneHourInMiliseconds);
028        assertEquals(date.toString(), __);
029    }
030
031    @Koan
032    public void usingCalendarToChangeDates() {
033        Calendar cal = Calendar.getInstance();
034        cal.setTime(date);
035        cal.add(Calendar.MONTH, 1);
036        assertEquals(cal.getTime().toString(), __);
037    }
038
039    @Koan
040    public void usingRollToChangeDatesDoesntWrapOtherFields() {
041        Calendar cal = Calendar.getInstance();
042        cal.setTime(date);
043        cal.roll(Calendar.MONTH, 12);
044        assertEquals(cal.getTime().toString(), __);
045    }
046
047    @Koan
048    public void usingDateFormatToFormatDate() {
049        String formattedDate = DateFormat.getDateInstance().format(date);
050        assertEquals(formattedDate, __);
051    }
052
053    @Koan
054    public void usingDateFormatToFormatDateShort() {
055        String formattedDate = DateFormat.getDateInstance(DateFormat.SHORT).format(date);
056        assertEquals(formattedDate, __);
057    }
058
059    @Koan
060    public void usingDateFormatToFormatDateFull() {
061        String formattedDate = DateFormat.getDateInstance(DateFormat.FULL).format(date);
062        // There is also DateFormat.MEDIUM and DateFormat.LONG... you get the idea ;-)
063        assertEquals(formattedDate, __);
064    }
065
066    @Koan
067    public void usingDateFormatToParseDates() throws ParseException {
068        DateFormat dateFormat = new SimpleDateFormat("MM-dd-yyyy");
069        Date date2 = dateFormat.parse("01-01-2000");
070        assertEquals(date2.toString(), __);
071        // What happened to the time? What do you need to change to keep the time as well?
072    }
073}