java - Android: Displaying days since received message after 24 hours pass -
i have implemented following code in android app splits the date/time value retrieved sql , displays number of minutes/hours since message received:
//splits date , time string retrieved sql 2 separate strings, //and stores them array string[] dt_array = m1.gettime().split(" "); //string date = dt_array[0]; string time = dt_array[1]; //splits time 2 strings, 1 hours , 1 minutes, , stores them //into array string[] timesplit = time.split(":"); string hourstring = timesplit[0]; string minutestring = timesplit[1]; //converts new separate time strings integer variables int msghour = integer.parseint(hourstring); int msgminute =integer.parseint(minutestring); calendar currenttime = calendar.getinstance(); //retrieves current hour , minute device , stores in separate ints int currenthour = currenttime.get(calendar.hour); int currentminute = currenttime.get(calendar.minute); int hourssince; int minutessince; string timedisplay = null; if (currenthour != 1) { //tells app display minutes if message received under hour ago if(currenthour - msghour < 1) { minutessince = currentminute - msgminute; //if minute has passed, text should read "1 minute ago", otherwise use plural "minutes" if (minutessince == 1) timedisplay = minutessince + " minute ago"; else timedisplay = minutessince + " minutes ago"; } else if (currenthour - msghour >= 1) { hourssince = currenthour - msghour; timedisplay = hourssince + " hours ago"; } } else { //for messages received @ 12th hour, if hour 1, correctly displays 1 hour ago if(msghour == 12 && currenthour == 1) { hourssince = 1; timedisplay = hourssince + " hours ago"; } }
what want add function displays days since received if hour count goes above 24. know should easy, i'm not sure how go on android.
your method of determining amount of time since message posted on complicated , prone issues when minutes , hours roll over. more simple:
simpledateformat formatter = new simpledateformat("dd/mm/yyyy hh:mm"); //change format needed date messagedate = formatter.parse(date); long since = system.currenttimemillis() - messagedate.gettime(); long seconds = since/1000; long minutes = seconds/60; long hours = minutes/60; long days = hours/24;
and there, building message want should trivial you.
Comments
Post a Comment