android - format ISO date/time and later compare to phone's date/time -
i've got string of date/time in iso-8601 format, like: 2011-04-15t20:08:18z
i want format more readable date/time , able compare later phone's local time. i'm bit confused what's been deprecated , hasn't. need use calendar
object?
can walk me through how this?
following code snippet can use
stringbuffer sbdate = new stringbuffer(); sbdate.append(cdate); string newdate = sbdate.substring(0, 19).tostring(); string rdate = newdate.replace("t", " "); string ndate = rdate.replaceall("-", "/");
ndate give date in normal format, compare 2 dates continue like
long epoch = new java.text.simpledateformat("yyyy/mm/dd hh:mm:ss").parse(ndate).gettime(); date currentdate = new date(); long diffinseconds = (currentdate.gettime() - epoch) / 1000; string elapsed = ""; long seconds = diffinseconds; long mins = diffinseconds / 60; long hours = diffinseconds / 3600; long days = diffinseconds / 86400; long weeks = diffinseconds / 604800; long months = diffinseconds / 2592000; if (seconds < 120) { elapsed = "a min ago"; } else if (mins < 60) { elapsed = mins + " mins ago"; } else if (hours < 24) { elapsed = hours + " "+ (hours > 1 ? "hrs" : "hr")+ " ago"; } else if (hours < 48) { elapsed = "a day ago"; } else if (days < 7) { elapsed = days + " days ago"; } else if (weeks < 5) { elapsed = weeks + " " + (weeks > 1 ? "weeks" : "week") + " ago"; } else if (months < 12) { elapsed = months" " + (months > 1 ? "months" : "months")+ " ago"; } else { elapsed = "more year ago"; }
this way you'll able date in proper format you'll able compare dates , difference in years, months, weeks, days, hours, mins or secs.
Comments
Post a Comment