1

I want to change the format of, Currently Date(YYYY-MM-DD) and Time (SS:MM:HH) to 'n' Months ago,'n' Days ago , 'n' Hours "ago" format.

CURRENT FORMAT:

enter image description here

REQUIRED FORMAT:

enter image description here

I am using Bean and Adapter class to get Current Date. Code is given Below;

Adapter Class:

public class MessageAdapter extends BaseAdapter  {

private Activity activity;

private List<MessageBean> messageBeanList;
public ImageLoader imageLoader;
private Context context;

public MessageAdapter (Activity activity,List<MessageBean> messageBeanList)
{
    super();
    this.activity = activity;
   // this.context = context;
    this.messageBeanList = messageBeanList;
    this.context=context;

}
@Override
public int getCount() {
    return messageBeanList.size();
}

@Override
public Object getItem(int position) {
    return messageBeanList.get(position);
}

@Override
public long getItemId(int position) {
    return 0;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    ItemHolder itemHolder = new ItemHolder();


    if (convertView == null) {
        LayoutInflater vi = (LayoutInflater) activity.getSystemService(
                Context.LAYOUT_INFLATER_SERVICE);

        convertView = vi.inflate(
                R.layout.message_item, null);
        imageLoader=new ImageLoader(activity.getApplicationContext());

        itemHolder.timestampp = (TextView) convertView
                .findViewById(R.id.timestamp);
        convertView.setTag(itemHolder);

    } else {
        itemHolder = (ItemHolder) convertView.getTag();
    }

class ItemHolder
{

    public TextView timestampp;

}
@Override
public void notifyDataSetChanged() {
    super.notifyDataSetChanged();
    // Your code to nofify
  }
 }

BEAN CLASS:

   import com.google.gson.annotations.SerializedName;


public class MessageBean {


   @SerializedName("date_created")

   private String dateCreated = "";
 }


    public String getDateCreated() {
    return dateCreated;
   }

   public void setDateCreated(String dateCreated) {
    this.dateCreated = dateCreated;
   }

Gone through almost every related question in SOF, but didn't get what I want as I am using bean and adapter class. Is this possible to convert Date and Time format if using JSON parsing?

Arjun Singh
  • 724
  • 1
  • 8
  • 24

4 Answers4

1

I hope this will work for you.. Just add code in your adapter

         /***************get current date time function*************/
            String curDateTime = "";
            Calendar c = Calendar.getInstance();
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            try{
              String curDateTime = df.format(c.getTime());
            }catch(Exception e){} 
          /***************get current date time function*************/

          // add simple_date_format for uniqueness
          SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

          try {

            Date date1 = simpleDateFormat.parse(messageBean.getDateCreated()); // like 2016-03-09 07:08:27

            Date date2 = simpleDateFormat.parse(curDateTime); // 2016-03-09 07:08:27

            differentDateTime = printDifference(date1, date2);

          } catch (Exception e) {
            e.printStackTrace();
         }

         itemHolder.timestampp.setText(differentDateTime);

         /************function of print different for showing date into ago format***************/
         //1 minute = 60 seconds
         //1 hour = 60 x 60 = 3600
        //1 day = 3600 x 24 = 86400
         public String printDifference(Date startDate, Date endDate){

            String allDaysMonsSeconds="";
             //milliseconds
             long different = endDate.getTime() - startDate.getTime();

             Log.d("TAG","startDate : " + startDate);
             Log.d("TAG","endDate : "+ endDate);
             Log.d("TAG","different : " + different);

             long secondsInMilli = 1000;
             long minutesInMilli = secondsInMilli * 60;
             long hoursInMilli = minutesInMilli * 60;
             long daysInMilli = hoursInMilli * 24;
             long yearInMilli = daysInMilli * 365;

             long elapsedDays = different / daysInMilli;
             different = different % daysInMilli;

             long elapsedHours = different / hoursInMilli;
             different = different % hoursInMilli;

             long elapsedMinutes = different / minutesInMilli;
             different = different % minutesInMilli;

             long elapsedSeconds = different / secondsInMilli;

             long elapsedYears = different / yearInMilli;

             Log.d("TAG","%d days, %d hours, %d minutes, %d seconds %n, %d years:"+elapsedDays+","+elapsedHours+","+elapsedMinutes+","+elapsedSeconds+","+elapsedYears);

             // code for showing before days...
             if(elapsedDays<=0){
                 if (elapsedHours<=0)
                  {

                      if (elapsedMinutes<=0)
                      {
                         allDaysMonsSeconds = elapsedSeconds+" second ago";

                      }
                      else
                      {

                         allDaysMonsSeconds = elapsedMinutes+" minute ago";

                      }
                  }
                  else
                  {

                     allDaysMonsSeconds = elapsedHours+" hour ago";

                  }
             }
             else{
                allDaysMonsSeconds = elapsedDays+" day ago";
             }


             return allDaysMonsSeconds;
         }
          /************function of print different for showing date into ago format***************/
rockstar
  • 587
  • 4
  • 22
0

try this,

initialize your textview first.

TextView t = new TextView();

Call below method as,

getTimeDifference(date, t);

private void getTimeDifference(String pDate, TextView time) {
    int diffInDays = 0;
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    format.setTimeZone(TimeZone.getTimeZone("GMT"));
    Calendar c = Calendar.getInstance();
    String formattedDate = format.format(c.getTime());

    Date d1 = null;
    Date d2 = null;
    try {

        d1 = format.parse(formattedDate);
        d2 = format.parse(pDate);
        long diff = d1.getTime() - d2.getTime();

        diffInDays = (int) (diff / (1000 * 60 * 60 * 24));
        if (diffInDays > 0) {
            if (diffInDays == 1) {
                time.setText(diffInDays + " day ago");
            } else {
                time.setText(diffInDays + " days ago");
            }
        } else {
            int diffHours = (int) (diff / (60 * 60 * 1000));
            if (diffHours > 0) {
                if (diffHours == 1) {
                    time.setText(diffHours + " hr ago");
                } else {
                    time.setText(diffHours + " hrs ago");
                }
            } else {

                int diffMinutes = (int) ((diff / (60 * 1000) % 60));
                if (diffMinutes == 1) {
                    time.setText(diffMinutes + " min ago");
                } else {
                    time.setText(diffMinutes + " mins ago");
                }

            }
        }

    } catch (ParseException e) {
        // System.out.println("Err: " + e);
        e.printStackTrace();
    }

}
Silvans Solanki
  • 1,267
  • 1
  • 14
  • 27
  • Kindly explain. Actually i m not getting the logic, how to convert. What you mentioned above, where to use this? Is this java class or what is this (above)? – Arjun Singh Mar 09 '16 at 10:43
0

Use the following code and just concatenate ago as a string in the time:

 startTime = "2016-03-09 16:23:30";
                        StringTokenizer tk = new StringTokenizer(startTime);
                        String date = tk.nextToken();  
                        String time = tk.nextToken();

                        SimpleDateFormat sdf = new SimpleDateFormat("hh:mm:ss");
                        SimpleDateFormat sdfs = new SimpleDateFormat("hh:mm a");
                        Date dt;
                        try {    
                            dt = sdf.parse(time);
                            System.out.println("Time Display: " + sdfs.format(dt)+" ago"); // <-- I got result here
                        } catch (ParseException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
Android Geek
  • 8,956
  • 2
  • 21
  • 35
0
        String curDateTime = "";
        Calendar c = Calendar.getInstance();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try{
          String curDateTime = df.format(c.getTime());
        }catch(Exception e){} 
      /***************get current date time function*************/

      // add simple_date_format for uniqueness
      SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

      try {

        Date date1 = simpleDateFormat.parse(messageBean.getDateCreated()); // like 2016-03-09 07:08:27

        Date date2 = simpleDateFormat.parse(curDateTime); // 2016-03-09 07:08:27

        differentDateTime = printDifference(date1, date2);

      } catch (Exception e) {
        e.printStackTrace();
     }

     itemHolder.timestampp.setText(differentDateTime);

     /************function of print different for showing date into ago format***************/
     //1 minute = 60 seconds
     //1 hour = 60 x 60 = 3600
    //1 day = 3600 x 24 = 86400
     public String printDifference(Date startDate, Date endDate){

        String allDaysMonsSeconds="";
         //milliseconds
         long different = endDate.getTime() - startDate.getTime();

         Log.d("TAG","startDate : " + startDate);
         Log.d("TAG","endDate : "+ endDate);
         Log.d("TAG","different : " + different);

         long secondsInMilli = 1000;
         long minutesInMilli = secondsInMilli * 60;
         long hoursInMilli = minutesInMilli * 60;
         long daysInMilli = hoursInMilli * 24;
         long yearInMilli = daysInMilli * 365;

         long elapsedDays = different / daysInMilli;
         different = different % daysInMilli;

         long elapsedHours = different / hoursInMilli;
         different = different % hoursInMilli;

         long elapsedMinutes = different / minutesInMilli;
         different = different % minutesInMilli;

         long elapsedSeconds = different / secondsInMilli;

         long elapsedYears = different / yearInMilli;

         Log.d("TAG","%d days, %d hours, %d minutes, %d seconds %n, %d years:"+elapsedDays+","+elapsedHours+","+elapsedMinutes+","+elapsedSeconds+","+elapsedYears);

         // code for showing before days...
         if(elapsedDays<=0){
             if (elapsedHours<=0)
              {

                  if (elapsedMinutes<=0)
                  {
                     allDaysMonsSeconds = elapsedSeconds+" second ago";

                  }
                  else
                  {

                     allDaysMonsSeconds = elapsedMinutes+" minute ago";

                  }
              }
              else
              {

                 allDaysMonsSeconds = elapsedHours+" hour ago";

              }
         }
         else{
            allDaysMonsSeconds = elapsedDays+" day ago";
         }


         return allDaysMonsSeconds;
     }