]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/utils/SimpleDate.java
rework transaction date handling
[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
26 public class SimpleDate {
27     public int year;
28     public int month;
29     public 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         if (year != date.year)
60             return false;
61         if (month != date.month)
62             return false;
63         if (day != date.day)
64             return false;
65
66         return true;
67     }
68     public boolean earlierThan(@NonNull SimpleDate date) {
69         if (year < date.year)
70             return true;
71         if (year > date.year)
72             return false;
73         if (month < date.month)
74             return true;
75         if (month > date.month)
76             return false;
77         return (day < date.day);
78     }
79     public boolean laterThan(@NonNull SimpleDate date) {
80         if (year > date.year)
81             return true;
82         if (year < date.year)
83             return false;
84         if (month > date.month)
85             return true;
86         if (month < date.month)
87             return false;
88         return (day > date.day);
89     }
90 }