[Models] Add basic error protection in Date, Time.

This commit is contained in:
Kuba Szczodrzyński 2020-02-19 23:04:28 +01:00
parent 164cfbfd0d
commit a6b91c3a14
2 changed files with 42 additions and 9 deletions

View File

@ -50,8 +50,13 @@ public class Date implements Comparable<Date> {
} }
public static Date fromYmd(String dateTime) { public static Date fromYmd(String dateTime) {
try {
return new Date(Integer.parseInt(dateTime.substring(0, 4)), Integer.parseInt(dateTime.substring(4, 6)), Integer.parseInt(dateTime.substring(6, 8))); return new Date(Integer.parseInt(dateTime.substring(0, 4)), Integer.parseInt(dateTime.substring(4, 6)), Integer.parseInt(dateTime.substring(6, 8)));
} }
catch (Exception e) {
return new Date(2019, 1, 1);
}
}
public static Date fromMillis(long millis) { public static Date fromMillis(long millis) {
Calendar c = Calendar.getInstance(); Calendar c = Calendar.getInstance();
@ -68,8 +73,13 @@ public class Date implements Comparable<Date> {
} }
public static Date fromY_m_d(String dateTime) { public static Date fromY_m_d(String dateTime) {
try {
return new Date(Integer.parseInt(dateTime.substring(0, 4)), Integer.parseInt(dateTime.substring(5, 7)), Integer.parseInt(dateTime.substring(8, 10))); return new Date(Integer.parseInt(dateTime.substring(0, 4)), Integer.parseInt(dateTime.substring(5, 7)), Integer.parseInt(dateTime.substring(8, 10)));
} }
catch (Exception e) {
return new Date(2019, 1, 1);
}
}
public long getInMillis() { public long getInMillis() {
Calendar c = Calendar.getInstance(); Calendar c = Calendar.getInstance();
@ -83,16 +93,31 @@ public class Date implements Comparable<Date> {
} }
public static Date fromd_m_Y(String dateTime) { public static Date fromd_m_Y(String dateTime) {
try {
return new Date(Integer.parseInt(dateTime.substring(6, 10)), Integer.parseInt(dateTime.substring(3, 5)), Integer.parseInt(dateTime.substring(0, 2))); return new Date(Integer.parseInt(dateTime.substring(6, 10)), Integer.parseInt(dateTime.substring(3, 5)), Integer.parseInt(dateTime.substring(0, 2)));
} }
catch (Exception e) {
return new Date(2019, 1, 1);
}
}
public static long fromIso(String dateTime) { public static long fromIso(String dateTime) {
try {
return Date.fromY_m_d(dateTime).combineWith(new Time(Integer.parseInt(dateTime.substring(11, 13)), Integer.parseInt(dateTime.substring(14, 16)), Integer.parseInt(dateTime.substring(17, 19)))); return Date.fromY_m_d(dateTime).combineWith(new Time(Integer.parseInt(dateTime.substring(11, 13)), Integer.parseInt(dateTime.substring(14, 16)), Integer.parseInt(dateTime.substring(17, 19))));
} }
catch (Exception e) {
return System.currentTimeMillis();
}
}
public static long fromIsoHm(String dateTime) { public static long fromIsoHm(String dateTime) {
try {
return Date.fromY_m_d(dateTime).combineWith(new Time(Integer.parseInt(dateTime.substring(11, 13)), Integer.parseInt(dateTime.substring(14, 16)), 0)); return Date.fromY_m_d(dateTime).combineWith(new Time(Integer.parseInt(dateTime.substring(11, 13)), Integer.parseInt(dateTime.substring(14, 16)), 0));
} }
catch (Exception e) {
return System.currentTimeMillis();
}
}
public static Date fromValue(int value) { public static Date fromValue(int value) {
int year = value / 10000; int year = value / 10000;

View File

@ -30,16 +30,24 @@ public class Time implements Comparable<Time> {
public Time parseFromYmdHm(String dateTime) public Time parseFromYmdHm(String dateTime)
{ {
try {
this.hour = Integer.parseInt(dateTime.substring(8, 10)); this.hour = Integer.parseInt(dateTime.substring(8, 10));
this.minute = Integer.parseInt(dateTime.substring(10, 12)); this.minute = Integer.parseInt(dateTime.substring(10, 12));
this.second = 0; this.second = 0;
}
catch (Exception ignore) {}
return this; return this;
} }
public static Time fromYmdHm(String dateTime) public static Time fromYmdHm(String dateTime)
{ {
try {
return new Time(Integer.parseInt(dateTime.substring(8, 10)), Integer.parseInt(dateTime.substring(10, 12)), 0); return new Time(Integer.parseInt(dateTime.substring(8, 10)), Integer.parseInt(dateTime.substring(10, 12)), 0);
} }
catch (Exception e) {
return new Time(0, 0, 0);
}
}
public long combineWith(Date date) { public long combineWith(Date date) {
if (date == null) { if (date == null) {