-1

I have to show the time that has passed after a message has been posted. I'm writing a method the takes publisheddate as input and returns elapsed time.

d1.gettime-pubdate.gettime=time in milliseconds.

I'm writing an if-condition where

if time==something concat days ago,
elseif hours ago
elseif min ago
elseif seconds ago
elseif justnow.

What should the if-condition contain? E.g. days should be if(time>=1440) (if I take the date in minutes, which can be done by time/1000*60*60); what should I do for minutes, hours, &c.?

Based on the advice given here is what i wrote .any improvement suggestions??

public static String TimeElapsed(final Date date)

{  

       Date Interval=new Date();
       DateTime Interval1 = new DateTime();//Converting java time to joda time
       Interval1 = new DateTime(date);
       DateTime Interval2 = new DateTime();
       Interval2=new DateTime(Interval);

       //Now we have two joda time instances

       Duration dd=new Duration(Interval1,Interval2);
      //get duration between     given            and current time
       Duration rd1=new Duration(1000);//seconds
       Duration rd2=new Duration(60000);//minutes
       Duration rd7=new Duration(60000*2);//2minutes
       Duration rd3=new Duration(3600000);//hours
       Duration rd6=new Duration(3600000*2);//2hours
       Duration rd4=new Duration(86400000);//days
       Duration rd5=new Duration(86400000*2);//2days
       if(dd.isShorterThan(rd1)){
           String k="just now";
           return k;
       }
       else if(dd.isEqual(rd1)){
           String k="just now";
           return k;

       }
       else if(dd.isLongerThan(rd1)&& dd.isShorterThan(rd2))
       {
           String k=dd.getStandardSeconds()+""+" seconds ago";
           return k;
       }
       else if(dd.isEqual(rd2) || dd.isShorterThan(rd7)){
           String k=dd.getStandardMinutes()+""+" minute ago";
           return k;
       }
       else if(dd.isLongerThan(rd2) && dd.isShorterThan(rd3)){
           String k=dd.getStandardMinutes()+""+" minutes ago";
           return k;
       }
       else if(dd.isEqual(rd3) || dd.isShorterThan(rd6))
       {
           String k=dd.getStandardHours()+""+" hour ago";
           return k;
       }
       else if(dd.isLongerThan(rd3) && dd.isShorterThan(rd4)){
            String k=dd.getStandardHours()+""+"hours ago";
            return k;
       }
       else if(dd.isEqual(rd4) || dd.isShorterThan(rd5) ) {
           String k=dd.getStandardDays()+""+" day ago";
           return k;
       }

       else if(dd.isLongerThan(rd4)){
           String k=dd.getStandardDays()+""+" days ago";
           return k;
       }
       else{
           String k="";
           return k;
       }
   }

2 Answers2

2

I agree with wrschneider99's suggestion of using Joda-Time, but IMO you should be using a Duration rather than an Interval, assuming you're talking about an elapsed duration which is a fixed number of milliseconds, rather than something more human-centric such as a number of months.

I would suggest something like:

// Assuming this really is a constant...
private static final Duration POST_DURATION = Duration.standardDays(2);

// Then elsewhere...
Instant expiry = post.getTime().plus(POST_DURATION);
// See below
Instant now = clock.now();

if (now.isAfter(expiry)) {
    ...
}

Note that I'd use some sort of Clock interface with a "just for test" implementation and a "based on system time" implementation and inject that as you would any other dependency, for testability. See Kevin Bourrillion's blog post about pure functions for more on this.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
1

I think you're looking for something like Joda Time's Period.normalizedStandard method.

See: Period to string

Community
  • 1
  • 1
wrschneider
  • 17,913
  • 16
  • 96
  • 176