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.
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.
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/>.
18 package net.ktnx.mobileledger.utils;
20 import androidx.annotation.NonNull;
21 import androidx.annotation.Nullable;
23 import java.util.Calendar;
24 import java.util.Date;
26 public class SimpleDate implements Comparable<SimpleDate> {
30 public SimpleDate(int y, int m, int d) {
35 public static SimpleDate fromDate(Date date) {
36 Calendar calendar = Calendar.getInstance();
37 calendar.setTime(date);
38 return fromCalendar(calendar);
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));
44 public static SimpleDate today() {
45 return fromCalendar(Calendar.getInstance());
47 public Calendar toCalendar() {
48 Calendar result = Calendar.getInstance();
49 result.set(year, month - 1, day);
52 public Date toDate() {
53 return toCalendar().getTime();
55 public boolean equals(@Nullable SimpleDate date) {
59 return ((year == date.year) && (month == date.month) && (day == date.day));
61 public boolean earlierThan(@NonNull SimpleDate date) {
66 if (month < date.month)
68 if (month > date.month)
70 return (day < date.day);
72 public boolean laterThan(@NonNull SimpleDate date) {
77 if (month > date.month)
79 if (month < date.month)
81 return (day > date.day);
83 public int compareTo(SimpleDate date) {
84 int res = Integer.compare(year, date.year);
88 res = Integer.compare(month, date.month);
92 return Integer.compare(day, date.day);