Java help, getting time ago in specific date.

Discussion in 'Programming' started by cris02, Aug 27, 2013.

  1. #1
    Please help with my code, sometimes it returning 0 instead the time passed in specific date.

    public static String getUTCdatetimeAsString() {
        String DATEFORMAT = "yyyy-MM-dd'T'HH:mm:ss.SSSZ";
        final SimpleDateFormat sdf = new SimpleDateFormat(DATEFORMAT);
        sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
        final String utcTime = sdf.format(new Date());
        return utcTime;
    }
    public static Timestamp getStringUTCdatetimeToTimeStamp(String inputStr) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
        dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
        Date parsedDate = null;
        Timestamp timestamp = null;
        try {
            if ((inputStr != null) && !inputStr.trim().equals("")) {
    
                parsedDate = dateFormat.parse(inputStr);
                timestamp = new java.sql.Timestamp(parsedDate.getTime());
            }
    
        } catch (ParseException e) {
            e.printStackTrace();
        }
    
        return timestamp;
    
    }
    public static String getTimeAgo() throws ParseException {
        Timestamp currentTime = getStringUTCdatetimeToTimeStamp(getUTCdatetimeAsString());
       
        SimpleDateFormat datetimeFormatter1 = new SimpleDateFormat(
                    "yyyy-MM-dd hh:mm:ss");
        Date lFromDate1 = datetimeFormatter1.parse("2013-08-26 06:13:46.95");
        Timestamp startDate = new Timestamp(lFromDate1.getTime());
    
       
        String retVal = "0";
        long inMinutes = TimeUnit.MILLISECONDS.toMinutes(currentTime.getTime() - startDate.getTime());
        long inHours = TimeUnit.MILLISECONDS.toHours(currentTime.getTime() - startDate.getTime());
        long inDays = TimeUnit.MILLISECONDS.toDays(currentTime.getTime() - startDate.getTime());
       
        int remainder = 0;
        if (inMinutes > 1 && inMinutes < 60) {
            retVal = inMinutes + " minute/s";
        }
        if (inHours > 1 && inHours < 24) {
            remainder = (int) (inMinutes % 60);
            retVal = inHours + " hour/s " + remainder + " minute/s";
        }
    
        if (inDays > 1 && inDays < 6) {
            remainder = (int) (inHours % 24);
            retVal = inDays + " day/s " + remainder + " hour/s";
        }
    
        if (inDays > 6 && inDays < 30) {
            int weekNum = (int) (inDays / 7);
            remainder = (int) (inDays % 7);
            retVal = weekNum + " week/s " + remainder + " day/s";
        }
    
        if (inDays > 30 && inDays < 365) {
            int monthNum = (int) (inDays / 30);
            retVal = monthNum + " month/s";
    
        }
    
        if (inDays > 365 && inDays < 3650) { // 10 years
            int monthNum = (int) (inDays / 30);
            int yearNum = (int) (inDays / 365);
            remainder = (int) (monthNum % 12);
            retVal = yearNum + " year/s " + remainder + " month/s";
    
        }
    
        if (inDays > 3650) {
            retVal = "More than 10 year/s";
    
        }
        return retVal;
    }
    Code (markup):
    In this example it return a 0 instead of returning the time passed frm this timestamp: 2013-08-26 06:13:46.95 to current timestamp. If you have any idea how to fix this please share. Thanks in advance.
     
    cris02, Aug 27, 2013 IP
  2. felomeno

    felomeno Peon

    Messages:
    8
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #2
    The problem was mainly that you used
    variable > 1
    Code (markup):
    when it has to be
    variable >= 1
    Code (markup):
    I changed a little bit your method.

     public static String getTimeAgo() throws ParseException {
            Timestamp currentTime = getStringUTCdatetimeToTimeStamp(getUTCdatetimeAsString());
    
            SimpleDateFormat datetimeFormatter1 = new SimpleDateFormat(
                        "yyyy-MM-dd hh:mm:ss");
            Date lFromDate1 = datetimeFormatter1.parse("2013-08-26 06:13:46.95");
            Timestamp startDate = new Timestamp(lFromDate1.getTime());
    
    
            String retVal = "";
            long inMinutes = TimeUnit.MILLISECONDS.toMinutes(currentTime.getTime() - startDate.getTime());
            long inHours = inMinutes/60;
            long inDays = inHours/24;
            long inMonths = inDays/30;
            long inYears = inDays/365;
            inMinutes%=60;
            inHours%=24;
            inDays%=7;
            inMonths%=12;
    
            if(inYears>10){
                retVal = "More than 10 years";
            }
            else{
                if(inYears>0)
                    retVal = inYears +" year/s";
                if(inMonths>0)
                    retVal += " " + inMonths + "month/s";
                if(inDays>0)
                    retVal += " " + inDays + "day/s";
                if(inHours>0)
                    retVal += " " + inHours + "hour/s";
                if(inMinutes>0)
                    retVal += " " + inMinutes + "minute/s";
            }
    
    
            return retVal;
        }
    Code (markup):
    The difference in the logic is that now it returns the complete difference between time.
    For example for 2012-08-26 06:13:46.95 will return 1 year/s 2day/s 6hour/s 31minute/s while yours would only return year and month.

    I'm also not using weeks as there is no exactly number of weeks on a month (you can use %4 as you used %30 for days on a month) so it wouldn't be correct.

    This of course is not completely precise, as not every month has 30 days, but as you did it that way I followed the same logic.

    Please let me know if you need something else.
     
    felomeno, Aug 27, 2013 IP