时间戳(一分钟前或一小时前)

时间:2016-03-21 09:32:19

标签: php android json time

在Android上没有与“A minute ago”或“An hour ago”之类的字符串获得时差。我之前已经问过这个问题。

How to get "Time Difference" in "since/ago"? Is this possible without use of any library?

这里我从Json Parsing获得时间字符串。格式为YYYY-MM-DD,SS:MM:HH)。 Pic附加

http://i.stack.imgur.com/jhdsy.png

我想知道是否有可能从字母“A minute ago”或“An Hour ago”中获取时间差异。或者我们还需要在PHP Server中做一些更改吗?

这是我的适配器类:

    public class MessageAdapter extends BaseAdapter  {

private Activity activity;
private File cacheDir;
private List<MessageBean> messageBeanList;
public ImageLoader imageLoader;
private Context context;
File filepath;

public MessageAdapter (Activity activity,List<MessageBean> messageBeanList)
{
    super();
    this.activity = activity;     
    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.textViewMessage = (TextView) convertView
                .findViewById(R.id.messageDetail);
        itemHolder.textViewUserName = (TextView) convertView
                .findViewById(R.id.userName);
        itemHolder.textViewEffectiveVote = (TextView) convertView
                .findViewById(R.id.effectiveVote);
        itemHolder.textViewTotalVote = (TextView) convertView
                .findViewById(R.id.totalVote);
        itemHolder.imageView = (ImageView) convertView
                .findViewById(R.id.imageView2);
        itemHolder.voteUpImageButton = (ImageButton) convertView
                .findViewById(R.id.agree);
        itemHolder.shareit = (ImageButton) convertView
                .findViewById(R.id.share);

              .findViewById(R.id.disagree);
        itemHolder.videoView = (ImageView) convertView
                .findViewById(R.id.videoViewThumb);
        convertView.setTag(itemHolder);
             .findViewById(R.id.messageicon);
        convertView.setTag(itemHolder);
        itemHolder.timestampp = (TextView) convertView
                .findViewById(R.id.timestamp);
        convertView.setTag(itemHolder);

    } else {
        itemHolder = (ItemHolder) convertView.getTag();
    }
    if (messageBeanList != null && !messageBeanList.isEmpty())
    {
        final MessageBean messageBean = messageBeanList.get(position);
        if (messageBean != null)
        {
            if (itemHolder.textViewEffectiveVote != null && messageBean.getEffectiveVote() != null)
            {
                itemHolder.textViewEffectiveVote.setText(messageBean.getEffectiveVote());
            }
            if (itemHolder.textViewMessage != null && messageBean.getMessage() !=null)
            {

                itemHolder.imageView.setVisibility(View.INVISIBLE);
                itemHolder.videoView.setVisibility(View.INVISIBLE);
                itemHolder.textViewMessage.setVisibility(View.VISIBLE);
                itemHolder.textViewMessage.setText(messageBean.getMessage());
            }
            if (itemHolder.imageView != null && messageBean.getMessageId().length()>1)
            {


            }
            if (itemHolder.textViewUserName != null && messageBean.getUserName() != null)
            {
                itemHolder.textViewUserName.setText(messageBean.getUserName());
            }

            if (itemHolder.textViewTotalVote != null && messageBean.getTotalVotes() != null)
            {
                itemHolder.textViewTotalVote.setText(messageBean.getTotalVotes());
            }

            if (itemHolder.timestampp != null && messageBean.getDateCreated() != null)
            {

                itemHolder.timestampp.setText(messageBean.getDateCreated());


            }

            if (itemHolder.voteDownImageButton != null )
            {
                ItemHolder holder = itemHolder;
                holder.voteDownImageButton.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                        // messageBean.getTotalVotes()
                        ((Message) activity).SendVote(messageBean, "0");
                    }
                });
            }
            if (itemHolder.shareit != null )
            {
                ItemHolder holder = itemHolder;
                holder.shareit.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                   if (messageBean.getMessage().length() > 1){
                        Intent sharingIntent = new Intent(Intent.ACTION_SEND);
                        sharingIntent.putExtra(Intent.EXTRA_TEXT, messageBean.getMessage());
                        sharingIntent.setType("text/plain");
                        activity.startActivity(sharingIntent);
                    }

                   if (messageBean.getImagePath().length() > 1){

                       String urlstring = "http://14.140.200.186/LikeIT/Service/development";
                       String imagestring = messageBean.getImagePath().toString();
                       imagestring = imagestring.replace("./", "/");
                       urlstring = urlstring + imagestring;
          activity.startActivity(sendIntent);
                       File filepath = Environment.getExternalStorageDirectory();
                       cacheDir = new File(filepath.getAbsolutePath()
                               + "/LikeIT/");
                       cacheDir.mkdirs();
                       Intent intent = new Intent();
                       intent.setType("image/*");
                       intent.setAction(Intent.ACTION_SEND);
                       intent.putExtra(Intent.EXTRA_STREAM, Uri.parse(urlstring));
                       activity.startActivity(intent);

                   }
                    }
                });
            }
            if (itemHolder.voteUpImageButton != null)
            {
                ItemHolder holder = itemHolder;
                holder.voteUpImageButton.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                        ((Message) activity).SendVote(messageBean, "1");
                    }
                });
            }
            if (itemHolder.imageView != null && messageBean.getImage() !=null)
            {
                byte[] decodedString = Base64.decode(messageBean.getImage(), Base64.DEFAULT);
                Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
                itemHolder.imageView.setImageBitmap(decodedByte);
            }
            if (itemHolder.imageView != null && messageBean.getImagePath().length()>1) {

                itemHolder.videoView.setVisibility(View.INVISIBLE);
                itemHolder.imageView.setVisibility(View.VISIBLE );
                itemHolder.textViewMessage.setVisibility(View.INVISIBLE);

   itemHolder.imageview2.setBackgroundResource(R.drawable.pictureshai);
                String urlstring = "http://14.140.200.186/LikeIT/Service/development";
                String imagestring = messageBean.getImagePath().toString();
                imagestring = imagestring.replace("./", "/");
                urlstring = urlstring + imagestring;
                imageLoader.DisplayImage(urlstring, itemHolder.imageView);
                ItemHolder holder = itemHolder;
                holder.imageView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        String urlstring = "http://14.140.200.186/LikeIT/Service/development";

                        String imagestring = messageBean.getImagePath().toString();
                        imagestring = imagestring.replace("./", "/");
                        urlstring = urlstring + imagestring;
                        // messageBean.getTotalVotes()
                        ((Message) activity).SendImageUrl(urlstring);
                    }
                });
            }
            if (itemHolder.videoView != null && messageBean.getVideoPath().toString().length()>1) {

                itemHolder.imageView.setVisibility(View.INVISIBLE );
                itemHolder.textViewMessage.setVisibility(View.INVISIBLE);
                itemHolder.videoView.setVisibility(View.VISIBLE);
                String urlstring = "http://14.140.200.186/LikeIT/Service/development";
                String imagestring = messageBean.getVideoPath().toString();
                imagestring = imagestring.replace("./", "/");
                urlstring = urlstring + imagestring;

               itemHolder.videoView.setImageURI(Uri.parse(urlstring));
                itemHolder.videoView.setBackgroundResource(R.drawable.videothumb);
                itemHolder.videoView.setTag(urlstring);


            DownloadVideoThumbNailTask.execute(itemHolder.videoView);


                ItemHolder holder = itemHolder;
                holder.videoView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        String urlstring = "http://14.140.200.186/LikeIT/Service/development";

                        String imagestring = messageBean.getVideoPath().toString();
                        imagestring = imagestring.replace("./", "/");
                        urlstring = urlstring + imagestring;

                        ((Message) activity).sendVideoUrl(urlstring);

                    }

                });
            }
        }
    }
    return convertView;
}




class ItemHolder
{
    public TextView textViewUserName;
    public TextView textViewMessage;
    public TextView textViewEffectiveVote;
    public TextView textViewTotalVote;
    public ImageView imageView;
    public ImageButton voteUpImageButton;
    public ImageButton voteDownImageButton;
    public ImageView videoView;
    public ImageView imageview2;
    public TextView timestampp;
    public String differenceString;
    public ImageButton shareit;
}
@Override
public void notifyDataSetChanged() {
    super.notifyDataSetChanged();
    // Your code to nofify
}

}

1 个答案:

答案 0 :(得分:0)

使用DateUtils.getRelativeTimeSpanString(Context context, long millis);

将日期的字符串表示形式转换为日期对象

SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd ss:mm:HH");
Date d = dateFormat.parse(yourdate)

return DateUtils.getRelativeTimeSpanString(getContext(), d.getTime());