java - Convert 12 hours to 24 hours -
how convert 12 hours hh:mm a
hh:mm
? when converting unable 24 hours format. here time format pm not converting when assigned start time @ 10pm , wake time 7am. here unable total time. it's getting stopped @ 12:00am , getting total time double 10pm 7am 18 hours. when time changing 11:59pm 00:00am "00:00am" causing problem here.
public class wakeup extends activity { imagebutton home, back, up_arw2, up_arw1, up_arw3, down_arw4, down_arw5, down_arw6; textview hours, minutes, ampm; button save_btn; sharedpreferences timepreference; sharedpreferences.editor edittime; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.wakeup_time); hours = (textview) findviewbyid(r.id.hours); minutes = (textview) findviewbyid(r.id.minutes); ampm = (textview) findviewbyid(r.id.ampm); home = (imagebutton) findviewbyid(r.id.home); save_btn = (button) findviewbyid(r.id.save_btn); timepreference = getsharedpreferences("childtime", mode_private); calendar c = calendar.getinstance(); simpledateformat df = new simpledateformat("hh:mm a"); string formattedtime = df.format(c.gettime()); edittime = timepreference.edit(); home.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { startactivity(new intent(getapplicationcontext(), settings.class)); } }); = (imagebutton) findviewbyid(r.id.back); back.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { startactivity(new intent(getapplicationcontext(), settings.class)); } }); up_arw2 = (imagebutton) findviewbyid(r.id.up_arw2); up_arw2.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { int timenum = integer.parseint(hours.gettext().tostring()); if (timenum == 11) { if (ampm.gettext().tostring().equals("am")) { ampm.settext("pm"); timenum++; hours.settext(string.valueof(timenum)); } else { ampm.settext("am"); timenum++; hours.settext(string.valueof(timenum)); } } else { timenum++; hours.settext(string.valueof(timenum)); down_arw4.setclickable(true); } if (timenum > 12) { hours.settext("1"); } } }); down_arw4 = (imagebutton) findviewbyid(r.id.down_arw4); down_arw4.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { int timenum = integer.parseint(hours.gettext().tostring()); if (timenum == 1) { hours.settext("12"); } else { timenum--; hours.settext(string.valueof(timenum)); } } }); up_arw3 = (imagebutton) findviewbyid(r.id.up_arw3); up_arw3.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { int timenum = integer.parseint(minutes.gettext().tostring()); if (timenum == 59) { minutes.settext("00"); } else { timenum++; minutes.settext(roundedmin(string.valueof(timenum))); } } }); down_arw5 = (imagebutton) findviewbyid(r.id.down_arw5); down_arw5.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { int timenum = integer.parseint(minutes.gettext().tostring()); if (timenum == 0) { minutes.settext("59"); } else { timenum--; minutes.settext(roundedmin(string.valueof(timenum))); up_arw3.setclickable(true); } } }); down_arw6 = (imagebutton) findviewbyid(r.id.down_arw6); down_arw6.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { ampm.settext("pm"); } }); up_arw1 = (imagebutton) findviewbyid(r.id.up_arw1); up_arw1.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { ampm.settext("am"); } }); save_btn.setonclicklistener(new view.onclicklistener() { date date; long gottimesss,millisecndslong; @override public void onclick(view v) { simpledateformat displayformat = new simpledateformat("hh:mm"); simpledateformat parseformat = new simpledateformat("hh:mm a"); try { date = parseformat.parse(hours.gettext().tostring() + ":" + minutes.gettext().tostring() + " " + ampm.gettext().tostring()); string gottime = displayformat.format(date); string[] timedivided = gottime.split(":"); string gothr = timedivided[0]; long gotlong = timeunit.hours.tominutes(long.parselong(gothr)); string gotmin = timedivided[1]; int gotintmin = integer.parseint(gotmin); gottimesss=gotlong+gotintmin; millisecndslong=timeunit.minutes.tomillis(gottimesss); convertsectohoursminute(millisecndslong); log.d("gotssss", string.valueof(millisecndslong)); log.d("timing", ""); } catch (parseexception e) { e.printstacktrace(); } edittime.putlong("savedwakeuptime", millisecndslong); edittime.commit(); startactivity(new intent(getapplicationcontext(), home.class)); } }); } string convertsectohoursminute(long sec) { long hours = sec / 3600; long minutes = (sec % 3600) / 60; long seconds = (sec % 3600) % 60; string ampm = "am"; if (minutes == 60) { minutes = 0; hours = hours + 1; } if (hours == 12) { ampm = "pm"; } if (hours == 0) { hours = 12; } if (hours > 12) { hours = hours - 12; ampm = "pm"; } log.d("timingdata",setzerobeforenine(hours) + ":" + setzerobeforenine(minutes) + " " + ampm); return setzerobeforenine(hours) + ":" + setzerobeforenine(minutes) + " " + ampm; } public static string setzerobeforenine(long digit) { try { if (digit <= 9) { return "0" + digit; } } catch (exception e) { e.printstacktrace(); } return "" + digit; } public static string roundedmin(string min) { if (min.length() == 1) { min = "0" + min; } return min; } }
use these 2 method convert second 24hours format public static
string convertsectohoursminute(long sec) { long hours = sec / 3600; long minutes = (sec % 3600) / 60; long seconds = (sec % 3600) % 60; string ampm = "am"; if (minutes == 60) { minutes = 0; hours = hours + 1; } if (hours == 12) { ampm = "pm"; } if (hours == 0) { hours = 12; } if (hours > 12) { hours = hours - 12; ampm = "pm"; } return setzerobeforenine(hours) + ":" + setzerobeforenine(minutes) + " " + ampm; } /** * set 0 before digits if less 9(for date , time) * * @param digit : date , time before 0 placed if less 9 * @return :return digit placing 0 prefix if digit less 9 */ public static string setzerobeforenine(long digit) { try { if (digit <= 9) { return "0" + digit; } } catch (exception e) { e.printstacktrace(); } return "" + digit; }