Android Open Source - android_twitter_client Twitter Time Utils






From Project

Back to project page android_twitter_client.

License

The source code is released under:

GNU General Public License

If you think the Android project android_twitter_client listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.github.snambi.twitterclient.utils;
// w  ww. j  a v a 2 s .c  om
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Locale;

import android.content.res.Resources;
import android.text.format.DateUtils;
import android.text.format.Time;

public class TwitterTimeUtils {

    public static final long SECOND_IN_MILLIS = 1000;
    public static final long MINUTE_IN_MILLIS = SECOND_IN_MILLIS * 60;
    public static final long HOUR_IN_MILLIS = MINUTE_IN_MILLIS * 60;
    public static final long DAY_IN_MILLIS = HOUR_IN_MILLIS * 24;
    public static final long WEEK_IN_MILLIS = DAY_IN_MILLIS * 7;
    /**
     * This constant is actually the length of 364 days, not of a year!
     */
    public static final long YEAR_IN_MILLIS = WEEK_IN_MILLIS * 52;

    
  public static String getRelativeTimeAgo(String rawJsonDate) {
    String twitterFormat = "EEE MMM dd HH:mm:ss ZZZZZ yyyy";
    SimpleDateFormat sf = new SimpleDateFormat(twitterFormat, Locale.ENGLISH);
    sf.setLenient(true);
   
    String relativeDate = "";
    try {
      long dateMillis = sf.parse(rawJsonDate).getTime();

//      relativeDate = DateUtils.getRelativeTimeSpanString(dateMillis,
//                                System.currentTimeMillis(), 
//                                DateUtils.SECOND_IN_MILLIS).toString();
      
      relativeDate = getTwitterRelativeTimeSpanString(dateMillis,
                                System.currentTimeMillis(), 
                                DateUtils.SECOND_IN_MILLIS).toString();

    } catch (ParseException e) {
      e.printStackTrace();
    }
   
    return relativeDate;
  }
  
  /**
   * Only handles past time in twitter format.
   * 2s
   * 2m
   * 2h
   * 3d
   * 
   * @param time
   * @param now
   * @param minResolution
   * @return
   */
    public static CharSequence getTwitterRelativeTimeSpanString(long time, 
                                  long now, 
                                  long minResolution) {
      StringBuilder result = new StringBuilder();
      
      Resources r = Resources.getSystem();
        boolean past = (now >= time);
        long duration = Math.abs(now - time);

        int resId;
        long count;
        if (duration < MINUTE_IN_MILLIS && minResolution < MINUTE_IN_MILLIS) {
            count = duration / SECOND_IN_MILLIS;
            if (past) {
              result.append(count);
              result.append("s");
            } else {
              result.append("-");
              result.append(count);
              result.append("s");
            }
        } else if (duration < HOUR_IN_MILLIS && minResolution < HOUR_IN_MILLIS) {
            count = duration / MINUTE_IN_MILLIS;
            if (past) {
              result.append(count);
              result.append("m");
            } else {
              result.append("-");
              result.append(count);
              result.append("m");
            }
        } else if (duration < DAY_IN_MILLIS && minResolution < DAY_IN_MILLIS) {
            count = duration / HOUR_IN_MILLIS;
            if (past) {
              result.append(count);
              result.append("h");
            } else {
              result.append("-");
              result.append(count);
              result.append("h");
            }
        } else if (duration < WEEK_IN_MILLIS && minResolution < WEEK_IN_MILLIS) {
            result.append( DateUtils.getRelativeTimeSpanString(time, now, minResolution) );
            //return getRelativeDayString(r, time, now);
        } else {
            // We know that we won't be showing the time, so it is safe to pass
            // in a null context.
            result.append( DateUtils.formatDateRange(null, time, time, 0) );
        }

//        String format = r.getQuantityString(resId, (int) count);
//        return String.format(format, count);
        
        return result.toString();
    }
    

}




Java Source Code List

com.github.snambi.twitterclient.TwitterApplication.java
com.github.snambi.twitterclient.activities.ComposeActivity.java
com.github.snambi.twitterclient.activities.LoginActivity.java
com.github.snambi.twitterclient.activities.ProfileActivity.java
com.github.snambi.twitterclient.activities.TimelineActivity.java
com.github.snambi.twitterclient.adapters.TwitterArrayAdapter.java
com.github.snambi.twitterclient.clients.TwitterRestClient.java
com.github.snambi.twitterclient.db.TweetDbHelper.java
com.github.snambi.twitterclient.fragemets.HomeTimelineFragment.java
com.github.snambi.twitterclient.fragemets.MentionsTimelineFragment.java
com.github.snambi.twitterclient.fragemets.ProfileHeaderFragment.java
com.github.snambi.twitterclient.fragemets.TwitterListFragment.java
com.github.snambi.twitterclient.fragemets.UserTimelineFragment.java
com.github.snambi.twitterclient.listeners.EndlessScrollListener.java
com.github.snambi.twitterclient.listeners.FragmentTabListener.java
com.github.snambi.twitterclient.models.SampleModel.java
com.github.snambi.twitterclient.models.Tweet.java
com.github.snambi.twitterclient.models.User.java
com.github.snambi.twitterclient.utils.TwitterTimeUtils.java