showing current time in TimePicker in android -
showing current time in TimePicker in android -
i utilize code show current time in timepicker, don't know really,why doesn't show correctly!? illustration time (system time) 2:56 pm, shows 2:56 am!. how can solve it? thanks.
private long timer1 = 0; ... switch (view.getid()) { case r.id.add1: { if (timer1 == 0) { timer1 = getcurrenttime(); } calendar calendar = calendar.getinstance(); calendar.settimeinmillis(timer1); timepicker timepicker = (timepicker) findviewbyid(r.id.tp_1); timepicker.setcurrenthour(calendar.get(calendar.hour)); timepicker.setcurrentminute(calendar.get(calendar.minute)); break; . .. } private long getcurrenttime() { calendar calendar = gregoriancalendar.getinstance(); homecoming calendar.gettimeinmillis(); }
and have same problem when want convert time string form method below:
private string converttimetostring(long time) { calendar calendar = calendar.getinstance(); calendar.settimeinmillis(time); int hr = calendar.get(calendar.hour_of_day); //int hr = get; int min = calendar.get(calendar.minute); string timeset; if (hour > 12) { hr -= 12; timeset = "pm"; } else if (hour == 0) { hr += 12; timeset = "am"; } else if (hour == 12) { timeset = "pm"; } else { timeset = "am"; } string minutes; if (minute < 10) { minutes = "0" + minute; } else { minutes = string.valueof(minute); } // append in stringbuilder homecoming string.format("%s:%s %s", hour, minutes, timeset); }
please help me!
updated 1) wrong hr problem: seek edit code this:
... timepicker.setcurrenthour(calendar.get(calendar.hour_of_day)); ...
2) string representation problem: general approach convert date/time string following:
string format = "hh:mm"; // or other format - see note below simpledateformat formatter = new simpledateformat(format); formatter.format(yourdate);
or when utilize long
timestamp lastly line be:
formatter.format(new date(yourlongtimestamp));
useful links: different date/time format symbols may here. test different format patterns may here.
android time picker
Comments
Post a Comment