diff options
author | Camil Staps | 2015-04-15 22:54:43 +0200 |
---|---|---|
committer | Camil Staps | 2015-04-15 22:54:43 +0200 |
commit | 7519f4d56e7b1bd7732f55e2643920302b436dff (patch) | |
tree | f92e7cf520fcf450664e18ac2796a5b85748d724 /app/src | |
parent | Finishing first raw version (diff) |
javadoc Date
Diffstat (limited to 'app/src')
-rw-r--r-- | app/src/main/java/com/camilstaps/common/Date.java | 125 |
1 files changed, 111 insertions, 14 deletions
diff --git a/app/src/main/java/com/camilstaps/common/Date.java b/app/src/main/java/com/camilstaps/common/Date.java index 59e7315..455c4ac 100644 --- a/app/src/main/java/com/camilstaps/common/Date.java +++ b/app/src/main/java/com/camilstaps/common/Date.java @@ -1,9 +1,6 @@ package com.camilstaps.common; import android.content.Context; -import android.util.Log; - -import com.camilstaps.taize.R; import java.text.ParseException; import java.text.SimpleDateFormat; @@ -13,12 +10,17 @@ import java.util.regex.Matcher; import java.util.regex.Pattern; /** - * Created by camilstaps on 7-4-15. + * Date + * + * @author Camil Staps */ public class Date implements Comparable<Date> { private int year, month, day; + /** + * Constructor without parameters creates a Date for the current date + */ public Date() { Calendar cal = Calendar.getInstance(); year = cal.get(Calendar.YEAR); @@ -26,12 +28,23 @@ public class Date implements Comparable<Date> { day = cal.get(Calendar.DAY_OF_MONTH); } + /** + * Constructor to specify year, month and day + * @param year + * @param month + * @param day + */ public Date(int year, int month, int day) { this.year = year; this.month = month; this.day = day; } + /** + * Constructor to parse a Date from a String + * @param format the String in YYYY-mm-dd format + * @throws ParseException if the format was incorrect + */ public Date(String format) throws ParseException { Pattern pat = Pattern.compile("(\\d*)-(\\d*)-(\\d*)"); Matcher match = pat.matcher(format); @@ -44,6 +57,10 @@ public class Date implements Comparable<Date> { } } + /** + * Copy-constructor + * @param copy + */ public Date(Date copy) { year = copy.year; month = copy.month; @@ -62,14 +79,27 @@ public class Date implements Comparable<Date> { return day; } + /** + * Add some amount of years + * @param n + */ public void addYears(int n) { year += n; } + /** + * Subtract some amount of years + * @param n + */ public void subtractYears(int n) { year -= n; } + /** + * Add some amount of months + * @param n + * @todo This could be optimized by removing the for loop + */ public void addMonths(int n) { for (int i = 0; i < n; i++) { month++; @@ -80,6 +110,11 @@ public class Date implements Comparable<Date> { } } + /** + * Subtract some amount of months + * @param n + * @todo This could be optimized by removing the for loop + */ public void subtractMonths(int n) { for (int i = 0; i < n; i++) { month--; @@ -90,6 +125,11 @@ public class Date implements Comparable<Date> { } } + /** + * Add some amount of days + * @param n + * @todo This could be optimized by removing the for loop + */ public void addDays(int n) { for (int i = 0; i < n; i++) { day++; @@ -100,6 +140,11 @@ public class Date implements Comparable<Date> { } } + /** + * Subtract some amount of days + * @param n + * @todo This could be optimized by removing the for loop + */ public void subtractDays(int n) { for (int i = 0; i < n; i++) { day--; @@ -110,6 +155,10 @@ public class Date implements Comparable<Date> { } } + /** + * Add (if n>0) or subtract (if n<0) some amount of days + * @param n + */ public void addOrSubtractDays(int n) { if (n < 0) { subtractDays(-n); @@ -118,15 +167,11 @@ public class Date implements Comparable<Date> { } } - public boolean equals(Date date) { - return year == date.year && month == date.month && day == date.day; - } - - @Override - public String toString() { - return Integer.toString(year) + "-" + Integer.toString(month) + "-" + Integer.toString(day); - } - + /** + * Check if a year is a leap year + * @param year + * @return + */ public static boolean isLeapYear(int year) { if (year % 4 != 0) return false; @@ -137,18 +182,37 @@ public class Date implements Comparable<Date> { return true; } + /** + * Check if this date is in a leap year + * @return + */ public boolean isLeapYear() { return isLeapYear(year); } + /** + * Give the number of days in a year + * @param year + * @return + */ public static int daysOfYear(int year) { return isLeapYear(year) ? 366 : 365; } + /** + * Give the number of days in this year + * @return + */ public int daysOfYear() { return daysOfYear(year); } + /** + * Give the number of days in a month + * @param year + * @param month + * @return + */ public static int daysOfMonth(int year, int month) { switch (month) { case 2: return isLeapYear(year) ? 29 : 28; @@ -164,10 +228,19 @@ public class Date implements Comparable<Date> { } } + /** + * Give the number of days in this month + * @return + */ public int daysOfMonth() { return daysOfMonth(year, month); } + /** + * Give the (absolute value of the) difference in days with another Date + * @param withDate + * @return + */ public int diffDays(Date withDate) { Date smaller, bigger; if (compareTo(withDate) > 0) { @@ -199,14 +272,38 @@ public class Date implements Comparable<Date> { return 0; } + /** + * Integer.compare was added in API level 19, so this is for compatibility + * @param x + * @param y + * @return + */ private int compareIntegers(int x, int y) { if (x == y) return 0; return (x < y) ? -1 : 1; } + /** + * Check if two objects represent the same date + * @param date + * @return + */ + public boolean equals(Date date) { + return year == date.year && month == date.month && day == date.day; + } + + /** + * Present the date in a 'nice' String, according to the user's localization + * @param context + * @return + */ public String toNiceString(Context context) { SimpleDateFormat sdf = new SimpleDateFormat("EEEE d MMMM y"); return sdf.format(new GregorianCalendar(year, month - 1, day).getTime()); - //return android.text.format.DateFormat.getDateFormat(context).format(new GregorianCalendar(year, month - 1, day).getTime()); + } + + @Override + public String toString() { + return Integer.toString(year) + "-" + Integer.toString(month) + "-" + Integer.toString(day); } } |