From f59d4c758d8edb2b538e77e342365d256d85d55c Mon Sep 17 00:00:00 2001 From: mo khan Date: Mon, 5 Aug 2019 17:25:41 -0600 Subject: format dates in UTC --- src/Q10/Station.java | 23 +++++++++++++++-------- src/Q10/StationTest.java | 30 ++++++++++++++++++++++++++++++ src/Q10/TrainTimeTable.java | 1 + 3 files changed, 46 insertions(+), 8 deletions(-) create mode 100644 src/Q10/StationTest.java (limited to 'src') diff --git a/src/Q10/Station.java b/src/Q10/Station.java index 906ca51..3fcca70 100644 --- a/src/Q10/Station.java +++ b/src/Q10/Station.java @@ -1,5 +1,6 @@ package Q10; +import java.text.*; import java.util.*; public class Station { @@ -27,18 +28,10 @@ public class Station { return day; } - public String getArrival() { - return this.getArrivalDate().toString(); - } - public String getCity() { return this.city; } - public String getDeparture() { - return getDepartureDate().toString(); - } - public void setArrivalDate(Date arrival) { this.arrival = arrival; } @@ -54,4 +47,18 @@ public class Station { public void setDepartureDate(Date departure) { this.departure = departure; } + + public String getArrival() { + return formatDate(this.arrival); + } + + public String getDeparture() { + return formatDate(this.departure); + } + + private String formatDate(Date date) { + DateFormat format = new SimpleDateFormat("HH:mm"); + format.setTimeZone(TimeZone.getTimeZone("UTC")); + return format.format(date); + } } diff --git a/src/Q10/StationTest.java b/src/Q10/StationTest.java new file mode 100644 index 0000000..a32191a --- /dev/null +++ b/src/Q10/StationTest.java @@ -0,0 +1,30 @@ +package ca.mokhan.test; + +import Q10.*; +import java.io.*; +import java.text.*; +import java.util.*; +import junit.framework.Test; +import junit.framework.TestCase; +import junit.framework.TestSuite; + +public class StationTest extends TestCase { + private Station subject; + + public StationTest(String testName) { + super(testName); + this.subject = new Station("Edmonton", new Date(1546470000000L), new Date(1546473540000L), 2); + } + + public static Test suite() { + return new TestSuite(StationTest.class); + } + + public void test_getArrival() { + assertEquals("23:00", subject.getArrival()); + } + + public void test_getDeparture() { + assertEquals("23:59", subject.getDeparture()); + } +} diff --git a/src/Q10/TrainTimeTable.java b/src/Q10/TrainTimeTable.java index ab5dfa9..3e952e6 100644 --- a/src/Q10/TrainTimeTable.java +++ b/src/Q10/TrainTimeTable.java @@ -33,6 +33,7 @@ public class TrainTimeTable { } public Station stationIn(String city) { + for (Station station : this.schedule) if (station.getCity() == city) return station; return null; } -- cgit v1.2.3