From 077595f73825dfb75f17faa8c10c4122931b17be Mon Sep 17 00:00:00 2001 From: Camil Staps Date: Tue, 7 Apr 2015 23:01:37 +0200 Subject: General improvement & continuation of development --- app/src/main/java/com/camilstaps/common/Date.java | 184 ++++++++++++++++++++++ 1 file changed, 184 insertions(+) create mode 100644 app/src/main/java/com/camilstaps/common/Date.java (limited to 'app/src/main/java/com/camilstaps/common') diff --git a/app/src/main/java/com/camilstaps/common/Date.java b/app/src/main/java/com/camilstaps/common/Date.java new file mode 100644 index 0000000..2823e35 --- /dev/null +++ b/app/src/main/java/com/camilstaps/common/Date.java @@ -0,0 +1,184 @@ +package com.camilstaps.common; + +import android.util.Log; + +import java.text.ParseException; +import java.util.Calendar; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +/** + * Created by camilstaps on 7-4-15. + */ +public class Date implements Comparable { + + private int year, month, day; + + public Date() { + Calendar cal = Calendar.getInstance(); + year = cal.get(Calendar.YEAR); + month = cal.get(Calendar.MONTH) + 1; + day = cal.get(Calendar.DAY_OF_MONTH); + } + + public Date(int year, int month, int day) { + this.year = year; + this.month = month; + this.day = day; + } + + public Date(String format) throws ParseException { + Pattern pat = Pattern.compile("(\\d*)-(\\d*)-(\\d*)"); + Matcher match = pat.matcher(format); + if (match.find()) { + year = Integer.parseInt(match.group(1)); + month = Integer.parseInt(match.group(2)); + day = Integer.parseInt(match.group(3)); + } else { + throw new ParseException("Wrong date format", 0); + } + } + + public Date(Date copy) { + year = copy.year; + month = copy.month; + day = copy.day; + } + + public int getYear() { + return year; + } + + public int getMonth() { + return month; + } + + public int getDay() { + return day; + } + + public void addYears(int n) { + year += n; + } + + public void addMonths(int n) { + for (int i = 0; i < n; i++) { + month++; + if (month >= 13) { + month = 1; + addYears(1); + } + } + } + + public void addDays(int n) { + for (int i = 0; i < n; i++) { + day++; + if (day >= daysOfMonth()) { + day = 1; + addMonths(1); + } + } + } + + 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); + } + + public static boolean isLeapYear(int year) { + if (year % 4 != 0) + return false; + if (year % 100 != 0) + return true; + if (year % 400 != 0) + return false; + return true; + } + + public boolean isLeapYear() { + return isLeapYear(year); + } + + public static int daysOfYear(int year) { + return isLeapYear(year) ? 366 : 365; + } + + public int daysOfYear() { + return daysOfYear(year); + } + + public static int daysOfMonth(int year, int month) { + switch (month) { + case 2: return isLeapYear(year) ? 29 : 28; + case 1: + case 3: + case 5: + case 8: + case 10: + case 12: + return 31; + default: + return 30; + } + } + + public int daysOfMonth() { + return daysOfMonth(year, month); + } + + public int diffDays(Date withDate) { + Date smaller, bigger; + if (compareTo(withDate) > 0) { + smaller = new Date(this); + bigger = new Date(withDate); + } else { + smaller = new Date(withDate); + bigger = new Date(this); + } + if (smaller.equals(bigger)) { + return 0; + } + int i = 0; + while (!smaller.equals(bigger)) { + smaller.addDays(1); + i++; + } + return i; + } + + @Override + public int compareTo(Date another) { + if (another.year != year) + return Integer.compare(another.year, year); + if (another.month != month) + return Integer.compare(another.month, month); + if (another.day != day) + return Integer.compare(another.day, day); + return 0; + } + + public int getDayOfWeek() { + // See: http://en.wikipedia.org/wiki/Determination_of_the_day_of_the_week#A_tabular_method_to_calculate_the_day_of_the_week + int months[] = {isLeapYear() ? 6 : 0, isLeapYear() ? 2 : 3, 3, 6, 1, 4, 6, 2, 5, 0, 3, 5}; + return (day + months[month - 1] + (year % 100) + (int) Math.floor((year % 100) / 4) + 2 * (3 - ((int) Math.floor(year / 100) % 4))) % 7; + } + + public String getDayOfWeekName() { + String days[] = {"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"}; + return days[getDayOfWeek()]; + } + + public String getMonthName() { + String months[] = {"January","February","March","April","May","June","July","August","September","October","November","December"}; + return months[month - 1]; + } + + public String toNiceString() { + return getDayOfWeekName() + ", " + day + " " + getMonthName() + " " + year; + } +} -- cgit v1.2.3