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;
25 import java.util.Locale;
27 public class SimpleDate implements Comparable<SimpleDate> {
28 public final int year;
29 public final int month;
31 public SimpleDate(int y, int m, int d) {
36 public static SimpleDate fromDate(Date date) {
37 Calendar calendar = Calendar.getInstance();
38 calendar.setTime(date);
39 return fromCalendar(calendar);
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));
45 public static SimpleDate today() {
46 return fromCalendar(Calendar.getInstance());
48 public Calendar toCalendar() {
49 Calendar result = Calendar.getInstance();
50 result.set(year, month - 1, day);
53 public Date toDate() {
54 return toCalendar().getTime();
56 public boolean equals(@Nullable SimpleDate date) {
60 return ((year == date.year) && (month == date.month) && (day == date.day));
62 public boolean earlierThan(@NonNull SimpleDate date) {
67 if (month < date.month)
69 if (month > date.month)
71 return (day < date.day);
73 public boolean laterThan(@NonNull SimpleDate date) {
78 if (month > date.month)
80 if (month < date.month)
82 return (day > date.day);
84 public int compareTo(SimpleDate date) {
85 int res = Integer.compare(year, date.year);
89 res = Integer.compare(month, date.month);
93 return Integer.compare(day, date.day);
95 public Calendar asCalendar() {
96 final Calendar calendar = Calendar.getInstance();
97 calendar.set(year, month, day);
100 public String toString() {
101 return String.format(Locale.US, "%d-%02d-%02d", year, month, day);