android - Displaying different date formats in the application based on the date comparision -


in application getting published date of content , showing in mmm,dd format.

it working pretty well, want show date format based on published date.

  1. if published date today, want show date in 24' hour format (like 19:45)
  2. if published date in 7 days, want show weekday , time (sunday, 14:30)
  3. otherwise need show mm/dd/yyyy hh:mm:ss.

for tried code:

public static string formatedate( string inputdate )     {          simpledateformat dateformat = new simpledateformat( "yyyy-mm-dd hh:mm:ss" );         string ldate = null;          date date = new date();          simpledateformat dateformater =null;          try         {             date = dateformat.parse( inputdate );              log.d("debug", "published date" + date);              int comparision=diff(date);              log.d("debug", "date comparision" + comparision);              if(comparision==0)             {                 dateformater = new simpledateformat( "kk:mm");             }             else if(comparision>0 && comparision<=7)             {                 dateformater = new simpledateformat( "eeee kk:mm");             }             else if(comparision > 7)             {                 dateformater = new simpledateformat( "mmm.dd.yyyy kk:mm" );             }              //dateformater = new simpledateformat( "mmm,dd" );              ldate = dateformater.format( date );         }         catch( exception e )         {             e.printstacktrace();         }          return ldate;     } 

here counting difference between days

public static int diff( date date1)     {         calendar c1 = calendar.getinstance();         calendar c2 = calendar.getinstance();          date todaydate=new date();          c1.settime( date1 );         c2.settime( todaydate );         int diffday = 0;          if( c1.before( c2 ) )         {             diffday = countdiffday( c1,                     c2 );         }         else         {             diffday = countdiffday( c2,                     c1 );         }          return diffday;     } 

in way able somehow expecting. think there feasible approach rather this. can 1 point me out this?

long currtime= system.currenttimemillis(); calendar calander = calendar.getinstance(); calander .settimeinmillis(currtime);  simpledateformat dateformat = new simpledateformat("dd/mm/yyyy hh:mm:ss a");  //above line of sdf give output in 24 hour format string dateforrow = dateformat.format(calander.gettime());  simpledateformat dateformat2 = new simpledateformat("dd/mm/yyyy hh:mm:ss a");  //above line of sdf give output in 12 hour format  string dateforrow2 = dateformat2.format(calander.gettime()); 

Comments

Popular posts from this blog

javascript - Google App Script ContentService downloadAsFile not working -

javascript - Function overwritting -

php - Find a regex to take part of Email -