Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Aquí guardo el tiempo.
- mp.getLongs().put(item.getName(), new Date().getTime());
- // Así hago un check de si ya ha pasado el tiempo y si no, obtengo lo que falta
- long timestampNow = System.currentTimeMillis() / 1000;
- long expires = mp.getLongs().get(item.getName());
- if(timestampNow >= expires){
- mp.getLongs().remove(item.getName());
- }else{
- expires = (long) expires * 1000;
- String formatExpires = Utils.formatDateDiff(expires);
- }
- // Esto me devuelve un String con el tiempo dividido en años, meses, días, horas, minutos y segundos:
- public static String formatDateDiff(Calendar fromDate, Calendar toDate){
- boolean future = false;
- if (toDate.equals(fromDate)) {
- return "1 segundo";
- }
- if (toDate.after(fromDate)) {
- future = true;
- }
- StringBuilder sb = new StringBuilder();
- int[] types = new int[] { Calendar.YEAR, Calendar.MONTH, Calendar.DAY_OF_MONTH, Calendar.HOUR_OF_DAY, Calendar.MINUTE, Calendar.SECOND };
- String[] names = new String[] { "año", "años", "mes", "meses", "día", "días", "hora", "horas", "minuto", "minutos", "segundo", "segundos" };
- for (int i = 0; i < types.length; i++) {
- int diff = dateDiff(types[i], fromDate, toDate, future);
- if (diff > 0) {
- sb.append(" ").append(diff).append(" ").append(names[i * 2 + (diff > 1 ? 1 : 0)]);
- }
- }
- if (sb.length() == 0) {
- return "1 segundo";
- }
- return sb.toString().trim();
- }
- public static String formatDateDiff(long date) {
- Calendar now = new GregorianCalendar();
- Calendar c = new GregorianCalendar();
- c.setTimeInMillis(date);
- return formatDateDiff(now, c);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement