]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/utils/SimpleDate.java
more pronounced day/month delimiters in the transaction list
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / utils / SimpleDate.java
1 /*
2  * Copyright © 2020 Damyan Ivanov.
3  * This file is part of MoLe.
4  * MoLe is free software: you can distribute it and/or modify it
5  * under the term of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your opinion), any later version.
8  *
9  * MoLe is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License terms for details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with MoLe. If not, see <https://www.gnu.org/licenses/>.
16  */
17
18 package net.ktnx.mobileledger.utils;
19
20 import androidx.annotation.NonNull;
21 import androidx.annotation.Nullable;
22
23 import java.util.Calendar;
24 import java.util.Date;
25 import java.util.Locale;
26
27 public class SimpleDate implements Comparable<SimpleDate> {
28     public final int year;
29     public final int month;
30     public final int day;
31     public SimpleDate(int y, int m, int d) {
32         year = y;
33         month = m;
34         day = d;
35     }
36     public static SimpleDate fromDate(Date date) {
37         Calendar calendar = Calendar.getInstance();
38         calendar.setTime(date);
39         return fromCalendar(calendar);
40     }
41     public static SimpleDate fromCalendar(Calendar calendar) {
42         return new SimpleDate(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH) + 1,
43                 calendar.get(Calendar.DATE));
44     }
45     public static SimpleDate today() {
46         return fromCalendar(Calendar.getInstance());
47     }
48     public Calendar toCalendar() {
49         Calendar result = Calendar.getInstance();
50         result.set(year, month - 1, day);
51         return result;
52     }
53     public Date toDate() {
54         return toCalendar().getTime();
55     }
56     public boolean equals(@Nullable SimpleDate date) {
57         if (date == null)
58             return false;
59
60         return ((year == date.year) && (month == date.month) && (day == date.day));
61     }
62     public boolean earlierThan(@NonNull SimpleDate date) {
63         if (year < date.year)
64             return true;
65         if (year > date.year)
66             return false;
67         if (month < date.month)
68             return true;
69         if (month > date.month)
70             return false;
71         return (day < date.day);
72     }
73     public boolean laterThan(@NonNull SimpleDate date) {
74         if (year > date.year)
75             return true;
76         if (year < date.year)
77             return false;
78         if (month > date.month)
79             return true;
80         if (month < date.month)
81             return false;
82         return (day > date.day);
83     }
84     public int compareTo(SimpleDate date) {
85         int res = Integer.compare(year, date.year);
86         if (res != 0)
87             return res;
88
89         res = Integer.compare(month, date.month);
90         if (res != 0)
91             return res;
92
93         return Integer.compare(day, date.day);
94     }
95     public Calendar asCalendar() {
96         final Calendar calendar = Calendar.getInstance();
97         calendar.set(year, month, day);
98         return calendar;
99     }
100     public String toString() {
101         return String.format(Locale.US, "%d-%02d-%02d", year, month, day);
102     }
103 }