如何获得“时差”在“自/之前”?这可能不使用任何库吗? [英] How to get "Time Difference" in "since/ago"? Is this possible without use of any library?

查看:150
本文介绍了如何获得“时差”在“自/之前”?这可能不使用任何库吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将当前日期(YYYY-MM-DD)和时间(SS:MM:HH)的格式更改为'n'几个月前,'n'天前,'n'小时以前格式。

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.

当前格式:

必填格式:

我使用Bean和Adapter类来获取当前日期。代码如下;

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

适配器类

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;
   }

在SOF中几乎所有相关问题都已消失,但没有得到我的信息想要因为我使用bean和适配器类。如果使用JSON解析,这可以转换日期和时间格式吗?

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?

推荐答案

我希望这对您有用。 。
只需在适配器中添加代码

         /***************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***************/

这篇关于如何获得“时差”在“自/之前”?这可能不使用任何库吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆