Clock Angle

Problem

We are given a specific time(like 02:23), we need to get the angle between hour and minute(less than 180)

Solution

 1 public static double clockAngle(String time) {
 2     if(time == null)    return -1;
 3     double angle = 0;
 4     
 5     int hour = 0, minute = 0;
 6     String[] hm = new String[2];
 7     hm = time.split(":");
 8     System.out.println(Arrays.toString(hm));
 9     
10     hour = Integer.parseInt(hm[0]);
11     minute = Integer.parseInt(hm[1]);
12     
13     doubleminAngle = minute*6;
14     double hourAngle = hour*30 + minAngle/12;
15     angle = Math.abs(minAngle - hourAngle);
16     if(angle > 180) angle = 360 - angle;
17     
18     return angle;
19 }

 

posted on 2014-11-21 09:33  SuperBo  阅读(261)  评论(0编辑  收藏  举报