5. Begin of this Month?
private Date beginOfThisMonth(){
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.DAY_OF_MONTH, 1);
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
return calendar.getTime();
}
6. Other Date use cases
Boolean isSameday(Date date1, Date date2)
Date firstDayOfWeek()
Date getBeginOfDay(int year, int month, int day)
int dayDiff(Date date1, Date date2)
7. Other Date use cases
Boolean isSameday(Date date1, Date date2)
Date firstDayOfWeek()
Date getBeginOfDay(int year, int month, int day)
int dayDiff(Date date1, Date date2)
int dayDiff(Calendar date1, Calendar date2) ?????
23. 連續上12天班
Period workDay = Period.ofDays(6)
Period holiday = Period.ofDays(1)
Period firstWeek = holiday.plus(workDay)
Period secondWeek = workDay.plus(holiday)
Period continuesWorkDay = workDay.plus(workDay)
30. Test with date-related code
//should climb mountain at Wednesday
boolean shouldClimbMountain()
31. Test with date-related code
//** this test only passed at Wednesday
void testShouldClimbMountain(){
...
}
32. Test with date-related code
//** this test only passed at Wednesday
@Ignore
void testShouldClimbMountain(){
...
}
33. Test with date-related code
//** this test only passed at Wednesday
/*Unit test sucks
void testShouldClimbMountain(){
...
}*/
34. Solution: Set System Time
private var clock = Clock.systemDefaultZone()
@VisibleForTesting
@JvmStatic
fun setSystemClock(clock: Clock){
this.clock = clock
}
@JvmStatic
fun getNow(): LocalDateTime = LocalDateTime.now(clock)
37. Plus, Minus
public final class LocalDate extends ChronoLocalDate implements Temporal{
...
Public LocalDate plus(TemporalAmount var1);
Public LocalDate minus(TemporalAmount var1);
...
}
38. Plus(+), Minus(-)
val today = LocalDate.now()
val tomorrow = today + Period.ofDays(1)
val lastWeek = today - Period.ofWeeks(1)
39. Kotlin Method Extensions
fun Int.Days(): Period = Period.ofDays(this)
fun Int.Weeks(): Period = Period.ofWeeks(this)
40. Method Extensions
fun Int.Days(): Period = Period.ofDays(this)
fun Int.Weeks(): Period = Period.ofWeeks(this)
val now = LocalDateTime.now()
val tomorrow = now + 1.Days()
val lastWeek = now - 1.Weeks()
41. CompareTo (<, >, =, >=, <=)
fun isFutureTime(dateTime: LocalDateTime): Boolean {
val now = LocalDateTime.now()
return dateTime > now
}
42. CompareTo (<, >, =, >=, <=)
fun isFutureTime(dateTime: LocalDateTime): Boolean {
val now = LocalDateTime.now()
return dateTime > now
}
46. After this talk...
1. 怎麼設計一個好用的 Api ?
2. Date 跟 Calendar 有著什麼樣的職責 ? 他們的命名跟職責符合
嗎?
3. 想一想你寫的專案裡有沒有類似的情形
47. Related work
JodaTime has design flaw :
http://www.infoq.com/cn/news/2010/05/jsr-310
Date 跟 Calendar 怎麼了?(CodeData)
http://www.codedata.com.tw/java/jodatime-jsr310-1-date-calendar/
50. Operator overloading and Conventions
class Point(val x: Int, val y: Int){
}
fun AddPoints(){
val p1 = Point(10, 15)
val p2 = Point(5, -5)
val p3 = p1 + p2
print(p3)
//Point(5, 10)
}
51. Operator overloading and Conventions
class Point(val x: Int, val y: Int){
operator fun plus(other: Point): Point{
return Point(x + other.x, y + other.y)
}
}
fun AddPoints(){
val p1 = Point(10, 15)
val p2 = Point(5, -5)
val p3 = p1 + p2
print(p3)
//Point(5, 10)
}
52. Operator overloading and Conventions
class Point(val x: Int, val y: Int){
operator fun plus(other: Point): Point{
return Point(x + other.x, y + other.y)
}
}
fun AddPoints(){
val p1 = Point(10, 15)
val p2 = Point(5, -5)
val p3 = p1 + p2
print(p3)
//Point(5, 10)
}
53. Operator overloading and Conventions(java)
public class Point {
int x;
int y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
public Point plus(Point other){
return new Point(x + other.x, y + other.y);
}
}
54. Operator overloading and Conventions(java)
public class Point {
int x;
int y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
public Point plus(Point other){
return new Point(x + other.x, y + other.y);
}
}