Code example for TimeUnit

Methods: toMinutes

0
        Date past = null;
        try { 
 
            past = format.parse(theDate);
            Date now = new Date();
            long diff = TimeUnit.MILLISECONDS.toMinutes(now.getTime() - past.getTime());
 
            if (diff < 60)
                theDate = diff + " minuter sedan";
            else if (diff < 60 * 24)
                theDate = diff / 24 + " timmar, " + diff % 60 + " minuter sedan";
            else if (diff < 60 * 60 * 24) {
                if ((diff / 60 / 24) == 1)
                    theDate = diff / 60 / 24 + " dag, " + diff % 24 + " timmar sedan";
                else 
                    theDate = diff / 60 / 24 + " dagar, " + diff % 24 + " timmar sedan";
            } 
        } catch (ParseException e) {
            /* Kunde inte parse'a datum, sätt det till Thu, 07 Mar */ 
            theDate = theDate.substring(0, 11);
            e.printStackTrace();