-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAboutDates.java
More file actions
73 lines (59 loc) · 2.29 KB
/
AboutDates.java
File metadata and controls
73 lines (59 loc) · 2.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package intermediate;
import com.sandwich.koan.Koan;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import static com.sandwich.koan.constant.KoanConstants.__;
import static com.sandwich.util.Assert.assertEquals;
public class AboutDates {
private Date date = new Date(100010001000L);
@Koan
public void dateToString() {
assertEquals(date.toString(), "Sat Mar 03 18:03:21 IST 1973");
}
@Koan
public void changingDateValue() {
int oneHourInMiliseconds = 3600000;
date.setTime(date.getTime() + oneHourInMiliseconds);
assertEquals(date.toString(), "Sat Mar 03 19:03:21 IST 1973");
}
@Koan
public void usingCalendarToChangeDates() {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.MONTH, 1);
assertEquals(cal.getTime().toString(), "Tue Apr 03 19:03:21 IST 1973");
}
@Koan
public void usingRollToChangeDatesDoesntWrapOtherFields() {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.roll(Calendar.MONTH, 12);
assertEquals(cal.getTime().toString(), "Sat Mar 03 19:03:21 IST 1973");
}
@Koan
public void usingDateFormatToFormatDate() {
String formattedDate = DateFormat.getDateInstance().format(date);
assertEquals(formattedDate, "Mar 3, 1973");
}
@Koan
public void usingDateFormatToFormatDateShort() {
String formattedDate = DateFormat.getDateInstance(DateFormat.SHORT).format(date);
assertEquals(formattedDate, "3/3/73");
}
@Koan
public void usingDateFormatToFormatDateFull() {
String formattedDate = DateFormat.getDateInstance(DateFormat.FULL).format(date);
// There is also DateFormat.MEDIUM and DateFormat.LONG... you get the idea ;-)
assertEquals(formattedDate,"Saturday, March 3, 1973");
}
@Koan
public void usingDateFormatToParseDates() throws ParseException {
DateFormat dateFormat = new SimpleDateFormat("MM-dd-yyyy");
Date date2 = dateFormat.parse("01-01-2000");
assertEquals(date2.toString(),"Sat Jan 01 00:00:00 IST 2000");
// What happened to the time? What do you need to change to keep the time as well?
}
}