]> git.ktnx.net Git - mobile-ledger-staging.git/blob - app/src/main/java/net/ktnx/mobileledger/utils/SimpleDate.java
fix many lint errors/warnings
[mobile-ledger-staging.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
26 public class SimpleDate implements Comparable<SimpleDate> {
27     public final int year;
28     public final int month;
29     public final int day;
30     public SimpleDate(int y, int m, int d) {
31         year = y;
32         month = m;
33         day = d;
34     }
35     public static SimpleDate fromDate(Date date) {
36         Calendar calendar = Calendar.getInstance();
37         calendar.setTime(date);
38         return fromCalendar(calendar);
39     }
40     public static SimpleDate fromCalendar(Calendar calendar) {
41         return new SimpleDate(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH) + 1,
42                 calendar.get(Calendar.DATE));
43     }
44     public static SimpleDate today() {
45         return fromCalendar(Calendar.getInstance());
46     }
47     public Calendar toCalendar() {
48         Calendar result = Calendar.getInstance();
49         result.set(year, month - 1, day);
50         return result;
51     }
52     public Date toDate() {
53         return toCalendar().getTime();
54     }
55     public boolean equals(@Nullable SimpleDate date) {
56         if (date == null)
57             return false;
58
59         return ((year == date.year) && (month == date.month) && (day == date.day));
60     }
61     public boolean earlierThan(@NonNull SimpleDate date) {
62         if (year < date.year)
63             return true;
64         if (year > date.year)
65             return false;
66         if (month < date.month)
67             return true;
68         if (month > date.month)
69             return false;
70         return (day < date.day);
71     }
72     public boolean laterThan(@NonNull SimpleDate date) {
73         if (year > date.year)
74             return true;
75         if (year < date.year)
76             return false;
77         if (month > date.month)
78             return true;
79         if (month < date.month)
80             return false;
81         return (day > date.day);
82     }
83     public int compareTo(SimpleDate date) {
84         int res = Integer.compare(year, date.year);
85         if (res != 0)
86             return res;
87
88         res = Integer.compare(month, date.month);
89         if (res != 0)
90             return res;
91
92         return Integer.compare(day, date.day);
93     }
94     public Calendar asCalendar() {
95         final Calendar calendar = Calendar.getInstance();
96         calendar.set(year, month, day);
97         return calendar;
98     }
99 }