java - How to calculate difference between String times -
i have string of: "9am-5pm" subtract these 2 numbers , find difference in hours.
int intervalhours(string input) { string[] ts = input.split("-"); // {"9am", "5pm"} return tohours(ts[1]) - tohours(ts[0]); } int tohours(string t) { int h = integer.parseint(t.substring(0, t.length() - 2)); if (t.endswith("pm")) return h == 12 ? 12 : (h + 12); else return h == 12 ? 0 : h; } // ... system.out.println(intervalhours("9am-5pm"));
Comments
Post a Comment