diff --git a/src/main/java/de/jeyp91/tippliga/TLWMatchdaysCreator.java b/src/main/java/de/jeyp91/tippliga/TLWMatchdaysCreator.java index ed935e5..8583400 100644 --- a/src/main/java/de/jeyp91/tippliga/TLWMatchdaysCreator.java +++ b/src/main/java/de/jeyp91/tippliga/TLWMatchdaysCreator.java @@ -6,9 +6,11 @@ import org.json.simple.JSONObject; import org.json.simple.parser.JSONParser; import org.json.simple.parser.ParseException; +import java.text.DateFormat; import java.text.SimpleDateFormat; +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; import java.util.ArrayList; -import java.util.Date; public class TLWMatchdaysCreator { @@ -53,7 +55,7 @@ public class TLWMatchdaysCreator { numberOfMatches = Integer.parseInt(matchdayConfigJson.get("numberOfMatches").toString()); } - String deliveryDate1 = null; + String deliveryDate1; String deliveryDate2 = null; JSONArray matchesConfig = (JSONArray) matchdayConfigJson.get("matchesConfig"); @@ -63,23 +65,15 @@ public class TLWMatchdaysCreator { } else { ArrayList matchesOfMatchday = getMatchesForMatchday(matches, matchdayNumber); - Date firstMatchDate = null; - for(TLWMatch match : matchesOfMatchday) { - if(deliveryDate1 == null) { - deliveryDate1 = match.getMatchDateTime(); - } - Date matchdate = null; - try { - matchdate = new SimpleDateFormat("yyyy-MM-dd").parse(match.getMatchDateTime().substring(0, 10)); - } catch (java.text.ParseException e) { - e.printStackTrace(); - } - if(firstMatchDate == null) { - firstMatchDate = matchdate; - } - if(deliveryDate2 == null && getDaysDifference(firstMatchDate, matchdate) >= 1) { - deliveryDate2 = match.getMatchDateTime(); - } + + LocalDateTime firstMatchDate = TLWMatchesManagerBase.getFirstMatchtimeTLW(matchesOfMatchday); + DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); + deliveryDate1 = firstMatchDate.format(formatter); + + ArrayList remainingMatches = TLWMatchesManagerBase.getMatchesStartingFromSecondDay(matchesOfMatchday); + if(remainingMatches.size() > 0) { + firstMatchDate = TLWMatchesManagerBase.getFirstMatchtimeTLW(remainingMatches); + deliveryDate2 = firstMatchDate.format(formatter); } } @@ -116,14 +110,6 @@ public class TLWMatchdaysCreator { return matchdaySql; } - private int getDaysDifference(Date date1, Date date2) { - long startTime = date1.getTime(); - long endTime = date2.getTime(); - long diffTime = endTime - startTime; - long diffDays = diffTime / (1000 * 60 * 60 * 24); - return (int) diffDays; - } - private ArrayList getMatchesForMatchday(ArrayList matches, int matchday) { ArrayList matchesOfMatchday = new ArrayList<>(); for(TLWMatch match : matches) { diff --git a/src/main/java/de/jeyp91/tippliga/TLWMatchdaysUpdater.java b/src/main/java/de/jeyp91/tippliga/TLWMatchdaysUpdater.java index ae5e9d9..f3816e1 100644 --- a/src/main/java/de/jeyp91/tippliga/TLWMatchdaysUpdater.java +++ b/src/main/java/de/jeyp91/tippliga/TLWMatchdaysUpdater.java @@ -2,6 +2,7 @@ package de.jeyp91.tippliga; import org.json.simple.JSONObject; +import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Comparator; @@ -14,7 +15,7 @@ public class TLWMatchdaysUpdater { ArrayList matchdaysOriginal; ArrayList matchdaysUpdated; - public TLWMatchdaysUpdater(int season, int league, String configPath) { + public TLWMatchdaysUpdater(int season, int league, String configPath) throws ParseException { this.season = season; this.league = league; diff --git a/src/main/java/de/jeyp91/tippliga/TLWMatchesCreatorFootball.java b/src/main/java/de/jeyp91/tippliga/TLWMatchesCreatorFootball.java index 8bfd081..5d4c442 100644 --- a/src/main/java/de/jeyp91/tippliga/TLWMatchesCreatorFootball.java +++ b/src/main/java/de/jeyp91/tippliga/TLWMatchesCreatorFootball.java @@ -13,6 +13,7 @@ import java.io.IOException; import java.net.URL; import java.nio.charset.StandardCharsets; import java.text.SimpleDateFormat; +import java.time.LocalDateTime; import java.util.ArrayList; import java.util.Date; @@ -57,7 +58,7 @@ public class TLWMatchesCreatorFootball extends TLWMatchesCreatorBase { for(int i = 0; i < this.matchdayConfig.size(); i++) { int TLWMatchday = ((Long) ((JSONObject) this.matchdayConfig.get(i)).get("TLWMatchday")).intValue(); JSONArray matchesConfig = (JSONArray)((JSONObject) this.matchdayConfig.get(i)).get("matchesConfig"); - ArrayList APIFootballMatches = getAPIFootballMatchesForMatchday(matchesConfig); + ArrayList apiFootballMatchesForMatchday = getAPIFootballMatchesForMatchday(matchesConfig); int tempNumberOfMatchesBackup = this.matchesPerMatchday; if(((JSONObject) this.matchdayConfig.get(i)).containsKey("numberOfMatches")) { @@ -67,19 +68,14 @@ public class TLWMatchesCreatorFootball extends TLWMatchesCreatorBase { int matchdayMatchCounter = 0; // Use first matchtime because API Football always returns matches sorted by date - Date firstDate = null; + LocalDateTime firstDate = TLWMatchesManagerBase.getFirstMatchtimeAPIFootball(apiFootballMatchesForMatchday); - for(APIFootballMatch match : APIFootballMatches) { + for(APIFootballMatch match : apiFootballMatchesForMatchday) { int matchNo = this.nextMatchNo; this.nextMatchNo++; int status = 0; - Date matchDateTime = null; - try { - matchDateTime = new SimpleDateFormat("yyyy-MM-dd").parse(match.getMatchDateTime().substring(0, 10)); - } catch (java.text.ParseException e) { - e.printStackTrace(); - } - if(firstDate != null && getDaysDifference(firstDate, matchDateTime) >= 1) { + LocalDateTime matchDateTime = TLWMatchesManagerBase.getDate(match); + if(firstDate != null && TLWMatchesManagerBase.getDaysDifference(firstDate, matchDateTime) >= 1) { status = -1; } this.TLWMatches.add(new TLWMatch(match, this.season, this.league, TLWMatchday, matchNo, status, this.ko)); @@ -95,7 +91,7 @@ public class TLWMatchesCreatorFootball extends TLWMatchesCreatorBase { if(((JSONObject) matchesConfig.get(0)).get("type").toString().equals("ToBeDefined")) { matchDatetime = ((JSONObject) matchesConfig.get(0)).get("placeholderDatetime").toString(); } - else if(APIFootballMatches.size() > 0) { + else if(apiFootballMatchesForMatchday.size() > 0) { matchDatetime = this.TLWMatches.get(this.TLWMatches.size() - 1).getMatchDateTime(); } int matchNo = this.nextMatchNo; @@ -129,12 +125,4 @@ public class TLWMatchesCreatorFootball extends TLWMatchesCreatorBase { } return apiFootballMatches; } - - private int getDaysDifference(Date date1, Date date2) { - long startTime = date1.getTime(); - long endTime = date2.getTime(); - long diffTime = endTime - startTime; - long diffDays = diffTime / (1000 * 60 * 60 * 24); - return (int) diffDays; - } } diff --git a/src/main/java/de/jeyp91/tippliga/TLWMatchesManagerBase.java b/src/main/java/de/jeyp91/tippliga/TLWMatchesManagerBase.java index c4f4f7b..588bf35 100644 --- a/src/main/java/de/jeyp91/tippliga/TLWMatchesManagerBase.java +++ b/src/main/java/de/jeyp91/tippliga/TLWMatchesManagerBase.java @@ -1,5 +1,7 @@ package de.jeyp91.tippliga; +import com.google.api.client.util.DateTime; +import de.jeyp91.apifootball.APIFootballMatch; import de.jeyp91.gists.GistProvider; import org.json.simple.JSONArray; import org.json.simple.JSONObject; @@ -7,6 +9,10 @@ import org.json.simple.parser.JSONParser; import org.json.simple.parser.ParseException; import java.text.SimpleDateFormat; +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; +import java.time.temporal.ChronoUnit; +import java.util.ArrayList; import java.util.Date; public class TLWMatchesManagerBase { @@ -36,21 +42,63 @@ public class TLWMatchesManagerBase { } } - protected int getDaysDifference(Date date1, Date date2) { - long startTime = date1.getTime(); - long endTime = date2.getTime(); - long diffTime = endTime - startTime; - long diffDays = diffTime / (1000 * 60 * 60 * 24); - return (int) diffDays; + public static ArrayList getMatchesStartingFromSecondDay(ArrayList matches) { + LocalDateTime firstMatchtime = getFirstMatchtimeTLW(matches); + ArrayList filteredMatches = new ArrayList<>(); + for(TLWMatch match : matches) { + LocalDateTime matchDateTime = getDate(match); + if(getDaysDifference(matchDateTime, firstMatchtime) != 0) { + filteredMatches.add(match); + } + } + return filteredMatches; } - protected Date getDateObject(String matchdatetime) { - Date matchDateTime = null; - try { - matchDateTime = new SimpleDateFormat("yyyy-MM-dd").parse(matchdatetime.substring(0, 10)); - } catch (java.text.ParseException e) { - e.printStackTrace(); + public static LocalDateTime getFirstMatchtimeTLW(ArrayList matches) { + LocalDateTime matchtime = null; + for(TLWMatch match : matches) { + LocalDateTime matchtimeTemp = getDate(match); + if(matchtime == null) { + matchtime = matchtimeTemp; + } else { + if(matchtimeTemp.isBefore(matchtime)) { + matchtime = matchtimeTemp; + } + } } - return matchDateTime; + return matchtime; + } + + public static LocalDateTime getFirstMatchtimeAPIFootball(ArrayList matches) { + LocalDateTime matchtime = null; + for(APIFootballMatch match : matches) { + LocalDateTime matchtimeTemp = getDate(match); + if(matchtime == null) { + matchtime = matchtimeTemp; + } else { + if(matchtimeTemp.isBefore(matchtime)) { + matchtime = matchtimeTemp; + } + } + } + return matchtime; + } + + public static LocalDateTime getDate(TLWMatch match) { + DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); + LocalDateTime dateTime = LocalDateTime.parse(match.getMatchDateTime(), formatter); + return dateTime; + } + + public static LocalDateTime getDate(APIFootballMatch match) { + DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss"); + LocalDateTime dateTime = LocalDateTime.parse(match.getMatchDateTime().substring(0, 19), formatter); + return dateTime; + } + + public static int getDaysDifference(LocalDateTime date1, LocalDateTime date2) { + date1 = date1.toLocalDate().atTime(0, 0, 0); + date2 = date2.toLocalDate().atTime(0, 0, 0); + return (int) date1.until(date2, ChronoUnit.DAYS); } } diff --git a/src/main/java/de/jeyp91/tippliga/TLWMatchesUpdaterFootball.java b/src/main/java/de/jeyp91/tippliga/TLWMatchesUpdaterFootball.java index 285b081..84b32e5 100644 --- a/src/main/java/de/jeyp91/tippliga/TLWMatchesUpdaterFootball.java +++ b/src/main/java/de/jeyp91/tippliga/TLWMatchesUpdaterFootball.java @@ -141,11 +141,11 @@ public class TLWMatchesUpdaterFootball extends TLWMatchesManagerBase { } private void updateStatus(ArrayList matches) { - Date earliestDate = getEarliestMatchDateTime(matches); - Date now = new Date(System.currentTimeMillis()); - if(earliestDate.after(now)) { + LocalDateTime earliestDate = TLWMatchesManagerBase.getFirstMatchtimeTLW(matches); + LocalDateTime now = LocalDateTime.now(); + if(earliestDate.isAfter(now)) { for(TLWMatch match : matches) { - Date date = this.getDateObject(match.getMatchDateTime()); + LocalDateTime date = TLWMatchesManagerBase.getDate(match); if (this.getDaysDifference(earliestDate, date) == 0) { match.setStatus(0); } else { @@ -155,17 +155,6 @@ public class TLWMatchesUpdaterFootball extends TLWMatchesManagerBase { } } - private Date getEarliestMatchDateTime(ArrayList matches) { - Date earliestDate = this.getDateObject(matches.get(0).getMatchDateTime()); - for(TLWMatch match : matches) { - Date date = this.getDateObject(match.getMatchDateTime()); - if(date.before(earliestDate)) { - earliestDate = date; - } - } - return earliestDate; - } - private TLWMatch getMatchingMatch(APIFootballMatch apiFootballMatch, ArrayList tlwMatches) throws Exception { int foundMatches = 0; TLWMatch matchingMatch = null; diff --git a/src/test/java/de/jeyp91/tippliga/TLWMatchdaysCreatorTest.java b/src/test/java/de/jeyp91/tippliga/TLWMatchdaysCreatorTest.java index 6d44c32..cbad41d 100644 --- a/src/test/java/de/jeyp91/tippliga/TLWMatchdaysCreatorTest.java +++ b/src/test/java/de/jeyp91/tippliga/TLWMatchdaysCreatorTest.java @@ -2,6 +2,7 @@ package de.jeyp91.tippliga; import org.junit.Test; +import java.text.ParseException; import java.util.ArrayList; import static org.junit.Assert.assertEquals; @@ -9,7 +10,7 @@ import static org.junit.Assert.assertEquals; public class TLWMatchdaysCreatorTest { @Test - public void getMatchdaysTippligaTest() { + public void getMatchdaysTippligaTest() throws ParseException { TLWMatchdaysCreator matchdaysCreator = new TLWMatchdaysCreator(2021, 1, "Tippliga.json"); ArrayList matchdays = matchdaysCreator.getMatchdays(); @@ -18,7 +19,7 @@ public class TLWMatchdaysCreatorTest { } @Test - public void getMatchdaysWTLPokalTest() { + public void getMatchdaysWTLPokalTest() throws ParseException { TLWMatchdaysCreator matchdaysCreator = new TLWMatchdaysCreator(2021, 1, "WTL_Pokal.json"); ArrayList matchdays = matchdaysCreator.getMatchdays(); @@ -26,7 +27,7 @@ public class TLWMatchdaysCreatorTest { } @Test - public void getMatchdaysTippliga1SqlTest() { + public void getMatchdaysTippliga1SqlTest() throws ParseException { TLWMatchdaysCreator matchdaysCreator = new TLWMatchdaysCreator(2021, 1, "Tippliga.json"); String sql = matchdaysCreator.getMatchdaysSQL(); @@ -34,7 +35,7 @@ public class TLWMatchdaysCreatorTest { } @Test - public void getMatchdaysTippliga2SqlTest() { + public void getMatchdaysTippliga2SqlTest() throws ParseException { TLWMatchdaysCreator matchdaysCreator = new TLWMatchdaysCreator(2021, 2, "Tippliga.json"); String sql = matchdaysCreator.getMatchdaysSQL(); @@ -42,7 +43,7 @@ public class TLWMatchdaysCreatorTest { } @Test - public void getMatchdaysTippliga51SqlTest() { + public void getMatchdaysTippliga51SqlTest() throws ParseException { TLWMatchdaysCreator matchdaysCreator = new TLWMatchdaysCreator(2021, 51, "Tippliga.json"); String sql = matchdaysCreator.getMatchdaysSQL(); @@ -50,7 +51,7 @@ public class TLWMatchdaysCreatorTest { } @Test - public void getMatchdaysTippliga52SqlTest() { + public void getMatchdaysTippliga52SqlTest() throws ParseException { TLWMatchdaysCreator matchdaysCreator = new TLWMatchdaysCreator(2021, 52, "Tippliga.json"); String sql = matchdaysCreator.getMatchdaysSQL(); @@ -58,7 +59,7 @@ public class TLWMatchdaysCreatorTest { } @Test - public void getMatchdaysWTLPokal48SqlTest() { + public void getMatchdaysWTLPokal48SqlTest() throws ParseException { TLWMatchdaysCreator matchdaysCreator = new TLWMatchdaysCreator(2021, 48, "WTL_Pokal.json"); String sql = matchdaysCreator.getMatchdaysSQL(); @@ -66,7 +67,7 @@ public class TLWMatchdaysCreatorTest { } @Test - public void getMatchdaysWTLPokal98SqlTest() { + public void getMatchdaysWTLPokal98SqlTest() throws ParseException { TLWMatchdaysCreator matchdaysCreator = new TLWMatchdaysCreator(2021, 98, "WTL_Pokal.json"); String sql = matchdaysCreator.getMatchdaysSQL(); diff --git a/src/test/java/de/jeyp91/tippliga/TLWMatchesCreatorTipperPokalTest.java b/src/test/java/de/jeyp91/tippliga/TLWMatchesCreatorTipperPokalTest.java index d0f2357..2db0177 100644 --- a/src/test/java/de/jeyp91/tippliga/TLWMatchesCreatorTipperPokalTest.java +++ b/src/test/java/de/jeyp91/tippliga/TLWMatchesCreatorTipperPokalTest.java @@ -2,10 +2,12 @@ package de.jeyp91.tippliga; import org.junit.Test; +import java.text.ParseException; + public class TLWMatchesCreatorTipperPokalTest { @Test - public void getMatchesWTLPokalTest() { + public void getMatchesWTLPokalTest() throws ParseException { TLWMatchdaysCreator matchdaysCreator = new TLWMatchdaysCreator(2021, 48, "WTL_Pokal.json"); TLWMatchesCreatorTipperPokal creator = new TLWMatchesCreatorTipperPokal(2021, 98, "WTL_Tipper.json", matchdaysCreator.getMatchdays()); diff --git a/src/test/java/de/jeyp91/tippliga/TLWMatchesCreatorTipperTest.java b/src/test/java/de/jeyp91/tippliga/TLWMatchesCreatorTipperTest.java index 7558a43..9a03a08 100644 --- a/src/test/java/de/jeyp91/tippliga/TLWMatchesCreatorTipperTest.java +++ b/src/test/java/de/jeyp91/tippliga/TLWMatchesCreatorTipperTest.java @@ -2,12 +2,14 @@ package de.jeyp91.tippliga; import org.junit.Test; +import java.text.ParseException; + import static org.junit.Assert.assertEquals; public class TLWMatchesCreatorTipperTest { @Test - public void getMatchesTippligaTest() { + public void getMatchesTippligaTest() throws ParseException { TLWMatchdaysCreator matchdaysCreator = new TLWMatchdaysCreator(2021, 2, "Tippliga.json"); TLWMatchesCreatorTipperLeague creator = new TLWMatchesCreatorTipperLeague(2021, 52, "2TL_Tipper.json", matchdaysCreator.getMatchdays()); diff --git a/src/test/java/de/jeyp91/tippliga/TLWTeamsCreatorTest.java b/src/test/java/de/jeyp91/tippliga/TLWTeamsCreatorTest.java index a8fc97b..625e8ba 100644 --- a/src/test/java/de/jeyp91/tippliga/TLWTeamsCreatorTest.java +++ b/src/test/java/de/jeyp91/tippliga/TLWTeamsCreatorTest.java @@ -2,6 +2,7 @@ package de.jeyp91.tippliga; import org.junit.Test; +import java.text.ParseException; import java.util.ArrayList; import static org.junit.Assert.assertEquals; @@ -18,7 +19,7 @@ public class TLWTeamsCreatorTest { } @Test - public void getTeamsTipper1TLTest() { + public void getTeamsTipper1TLTest() throws ParseException { TLWMatchdaysCreator matchdaysCreator = new TLWMatchdaysCreator(2021, 1, "Tippliga.json"); TLWMatchesCreatorTipperLeague creator = new TLWMatchesCreatorTipperLeague(2021, 51, "1TL_Tipper.json", matchdaysCreator.getMatchdays()); @@ -31,7 +32,7 @@ public class TLWTeamsCreatorTest { } @Test - public void getTeamsTipper2TLTest() { + public void getTeamsTipper2TLTest() throws ParseException { TLWMatchdaysCreator matchdaysCreator = new TLWMatchdaysCreator(2021, 2, "Tippliga.json"); TLWMatchesCreatorTipperLeague creator = new TLWMatchesCreatorTipperLeague(2021, 52, "2TL_Tipper.json", matchdaysCreator.getMatchdays()); @@ -44,7 +45,7 @@ public class TLWTeamsCreatorTest { } @Test - public void getTeamsTipperWTLTest() { + public void getTeamsTipperWTLTest() throws ParseException { TLWMatchdaysCreator matchdaysCreator = new TLWMatchdaysCreator(2021, 48, "WTL_Pokal.json"); TLWMatchesCreatorTipperPokal creator = new TLWMatchesCreatorTipperPokal(2021, 98, "WTL_Tipper.json", matchdaysCreator.getMatchdays());