forked from AllenDowney/ThinkJavaCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTimeZoneDemo.java
More file actions
52 lines (41 loc) · 1.12 KB
/
TimeZoneDemo.java
File metadata and controls
52 lines (41 loc) · 1.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package com.company;
import java.util.HashMap;
public class TimeZoneDemo
{
HashMap<String,Integer> hashMap = new HashMap<>();
public static void main(String[] args)
{
TimeZoneDemo timeZoneDemo = new TimeZoneDemo();
timeZoneDemo.demo();
}
private void demo()
{
hashMap.put("EST",-5);
hashMap.put("CST",-6);
hashMap.put("MST",-7);
hashMap.put("PST",-8);
hashMap.put("GMT",0);
System.out.println(getTimeDiff("PST","EST"));
System.out.println(getTimeDiff("EST","PST"));
}
private double getTimeDiff(String timeZone1, String timeZone2)
{
double timeDiff = 0;
double tz1 = hashMap.get(timeZone1);
double tz2 = hashMap.get(timeZone2);
//Check to make sure the result would be the same regardless of the order the keys are entered in
if (tz1 < tz2)
{
timeDiff = tz1 - tz2;
}
else if (tz2 < tz1)
{
timeDiff = tz2 - tz1;
}
else
{
System.out.println("Oops.");
}
return timeDiff;
}
}