114 lines
3.5 KiB
Java
114 lines
3.5 KiB
Java
package de.arminwolf.financeanalyzer.util;
|
|
|
|
import java.text.DateFormat;
|
|
import java.text.ParseException;
|
|
import java.text.SimpleDateFormat;
|
|
import java.time.Instant;
|
|
import java.time.LocalDate;
|
|
import java.time.YearMonth;
|
|
import java.time.ZoneId;
|
|
import java.time.format.DateTimeFormatter;
|
|
import java.util.Calendar;
|
|
import java.util.Date;
|
|
import java.util.Locale;
|
|
|
|
public class DateUtil {
|
|
|
|
private final static DateFormat df = new SimpleDateFormat("dd-MMM-yyyy", Locale.GERMAN);
|
|
private final static DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd.MM.yyyy");
|
|
private final static DateTimeFormatter originalFormatter = DateTimeFormatter.ofPattern("dd-MMM-yyyy");
|
|
private final static Calendar startCalendar = Calendar.getInstance();
|
|
|
|
|
|
public static String format(final LocalDate start) {
|
|
return start.format(formatter);
|
|
}
|
|
|
|
|
|
public static LocalDate getFirstDayOfMonth(final LocalDate localDate) {
|
|
return LocalDate.of(localDate.getYear(), localDate.getMonth(), 1);
|
|
}
|
|
|
|
|
|
public static LocalDate getLastDayOfMonth(final LocalDate localDate) {
|
|
return YearMonth.of(localDate.getYear(), localDate.getMonth()).atEndOfMonth();
|
|
}
|
|
|
|
|
|
public static boolean isBefore(String date, LocalDate end) {
|
|
try {
|
|
Date parse = df.parse(date);
|
|
Instant instant = parse.toInstant();
|
|
LocalDate localDate = instant.atZone(ZoneId.systemDefault()).toLocalDate();
|
|
|
|
return localDate.isBefore(end);
|
|
} catch (ParseException e) {
|
|
System.out.println("ERROR: " + date);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
|
|
public static boolean isAfter(String date, LocalDate start) {
|
|
try {
|
|
Date parse = df.parse(date);
|
|
Instant instant = parse.toInstant();
|
|
LocalDate localDate = instant.atZone(ZoneId.systemDefault()).toLocalDate();
|
|
|
|
return localDate.isAfter(start);
|
|
} catch (ParseException e) {
|
|
System.out.println("ERROR: " + date);
|
|
e.printStackTrace(System.err);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
|
|
public static boolean isBetween(String date, LocalDate start, LocalDate end) {
|
|
try {
|
|
Date parse = df.parse(date);
|
|
Instant instant = parse.toInstant();
|
|
LocalDate localDate = instant.atZone(ZoneId.systemDefault()).toLocalDate();
|
|
|
|
return (localDate.isEqual(end) || localDate.isBefore(end)) && (localDate.isEqual(start) || localDate.isAfter(start));
|
|
} catch (ParseException e) {
|
|
System.out.println("ERROR: " + date);
|
|
e.printStackTrace(System.err);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
|
|
public static LocalDate parse(final String buchungstag) {
|
|
return LocalDate.parse(buchungstag, originalFormatter);
|
|
}
|
|
|
|
|
|
public static LocalDate parseMonth(final String month) {
|
|
String[] split = month.split("-");
|
|
return LocalDate.of(Integer.parseInt(split[0]), Integer.parseInt(split[1]), 1);
|
|
}
|
|
|
|
|
|
public static String prettyPrint(final LocalDate date) {
|
|
return date.format(formatter);
|
|
}
|
|
|
|
public static int monthsBetween(LocalDate startDate, LocalDate endDate) {
|
|
if (startDate == null || endDate == null) {
|
|
throw new IllegalArgumentException("Both startDate and endDate must be provided");
|
|
}
|
|
|
|
startCalendar.setTime(Date.from(startDate.atStartOfDay(ZoneId.systemDefault()).toInstant()));
|
|
int startDateTotalMonths = 12 * startCalendar.get(Calendar.YEAR) + startCalendar.get(Calendar.MONTH);
|
|
|
|
Calendar endCalendar = Calendar.getInstance();
|
|
endCalendar.setTime(Date.from(endDate.atStartOfDay(ZoneId.systemDefault()).toInstant()));
|
|
|
|
int endDateTotalMonths = 12 * endCalendar.get(Calendar.YEAR)
|
|
+ endCalendar.get(Calendar.MONTH);
|
|
|
|
return endDateTotalMonths - startDateTotalMonths;
|
|
}
|
|
}
|