Use java.time.* instead of Calendar

This commit is contained in:
Reinhard Pointner 2016-02-10 03:17:31 +00:00
parent 9be55cbf54
commit 058940e847
1 changed files with 13 additions and 16 deletions

View File

@ -1,13 +1,12 @@
package net.filebot.web; package net.filebot.web;
import static java.util.Calendar.*;
import java.io.Serializable; import java.io.Serializable;
import java.text.ParseException; import java.text.ParseException;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Arrays; import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar; import java.util.GregorianCalendar;
import java.util.Locale; import java.util.Locale;
@ -28,11 +27,10 @@ public class SimpleDate implements Serializable, Comparable<Object> {
} }
public SimpleDate(long t) { public SimpleDate(long t) {
GregorianCalendar c = new GregorianCalendar(); LocalDateTime date = LocalDateTime.ofInstant(Instant.ofEpochMilli(t), ZoneId.systemDefault());
c.setTime(new Date(t)); year = date.getYear();
year = c.get(Calendar.YEAR); month = date.getMonthValue();
month = c.get(Calendar.MONTH) + 1; day = date.getDayOfMonth();
day = c.get(Calendar.DAY_OF_MONTH);
} }
public int getYear() { public int getYear() {
@ -109,16 +107,15 @@ public class SimpleDate implements Serializable, Comparable<Object> {
} }
public static SimpleDate parse(String string, String pattern) { public static SimpleDate parse(String string, String pattern) {
if (string == null || string.isEmpty()) if (string == null || string.isEmpty()) {
return null; return null;
}
try {
SimpleDateFormat formatter = new SimpleDateFormat(pattern, Locale.ROOT); SimpleDateFormat formatter = new SimpleDateFormat(pattern, Locale.ROOT);
formatter.setLenient(false); // enable strict mode (e.g. fail on invalid dates like 0000-00-00) formatter.setLenient(false); // enable strict mode (e.g. fail on invalid dates like 0000-00-00)
try { return new SimpleDate(formatter.parse(string).getTime());
Calendar date = new GregorianCalendar(Locale.ROOT);
date.setTime(formatter.parse(string));
return new SimpleDate(date.get(YEAR), date.get(MONTH) + 1, date.get(DAY_OF_MONTH)); // Calendar months start at 0
} catch (ParseException e) { } catch (ParseException e) {
// date is invalid // date is invalid
return null; return null;