Android Open Source - mobile2-android Date Time Util






From Project

Back to project page mobile2-android.

License

The source code is released under:

Apache License

If you think the Android project mobile2-android 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.ecollege.android.util;
//from   w  w w  .  ja  v a  2s  . co m
import java.util.Calendar;
import java.util.Date;

import android.text.format.DateFormat;

public class DateTimeUtil {

  public static String getShortFriendlyDate(Calendar cal) {
    return getShortFriendlyDate(cal.getTime());
  }
  
  public static String getShortFriendlyDate(long timeInMillis) {
    return getShortFriendlyDate(new Date(timeInMillis));
  }
  
  public static String getShortFriendlyDate(Date dt) {
    String dtStr = DateFormat.format("MMM d, yyyy", dt).toString();
    
    Date today = getToday();
    String todayStr = DateFormat.format("MMM d, yyyy", today).toString();
    
    if (dtStr.equals(todayStr)) {
      return "Today";
    }
    
    Date yesterday = getYesterday();
    String yesterdayStr = DateFormat.format("MMM d, yyyy", yesterday).toString();
    
    if (dtStr.equals(yesterdayStr)) {
      return "Yesterday";
    }
    
    if (dt.getYear() != today.getYear()) {
      return dtStr;
    } else {
      return DateFormat.format("MMM d", dt).toString();
    }
  }

  public static String getLongFriendlyDate(Calendar cal) {
    return getLongFriendlyDate(cal.getTime());
  }
  
  public static String getLongFriendlyDate(long timeInMillis) {
    return getLongFriendlyDate(new Date(timeInMillis));
  }
  
  public static String getLongFriendlyDate(Date dt) {
    String dtStr = DateFormat.format("MMM d, yyyy", dt).toString();
    
    Date today = getToday();
    String todayStr = DateFormat.format("MMM d, yyyy", today).toString();
    
    if (dtStr.equals(todayStr)) {
      return "Today " + DateFormat.format("h:mm aa", dt);
    }
    
    Date yesterday = getYesterday();
    String yesterdayStr = DateFormat.format("MMM d, yyyy", yesterday).toString();
    
    if (dtStr.equals(yesterdayStr)) {
      return "Yesterday " + DateFormat.format("h:mm aa", dt);
    }
    
    if (dt.getYear() != today.getYear()) {
      return DateFormat.format("MMM d, yyyy h:mm aa", dt).toString();
    } else {
      return DateFormat.format("MMM d h:mm aa", dt).toString();
    }
  }
  
  public static Date getToday() {
    Date now = new Date();
    Date today = new Date(now.getYear(),now.getMonth(),now.getDate()); //beginning of today
    return today;
  }
  
  public static Date getYesterday() {
    Date today = getToday();
    Date yesterday = new Date(today.getYear(),today.getMonth(),today.getDate()-1); //beginning of yesterday
    return yesterday;
  }
  
}




Java Source Code List

com.ecollege.android.AnnouncementActivity.java
com.ecollege.android.CourseActivity.java
com.ecollege.android.CourseAnnouncementsActivity.java
com.ecollege.android.CourseDiscussionsActivity.java
com.ecollege.android.CourseGradebookActivity.java
com.ecollege.android.CoursePeopleActivity.java
com.ecollege.android.CourseThreadActivity.java
com.ecollege.android.CoursesActivity.java
com.ecollege.android.DiscussionsActivity.java
com.ecollege.android.DropboxMessageActivity.java
com.ecollege.android.ECollegeApplication.java
com.ecollege.android.GradeActivity.java
com.ecollege.android.HomeActivity.java
com.ecollege.android.HtmlContentActivity.java
com.ecollege.android.LoginActivity.java
com.ecollege.android.MainActivity.java
com.ecollege.android.PersonActivity.java
com.ecollege.android.ProfileActivity.java
com.ecollege.android.SingleSignonActivity.java
com.ecollege.android.SplashActivity.java
com.ecollege.android.UserDiscussionActivity.java
com.ecollege.android.UserResponseActivity.java
com.ecollege.android.UserTopicActivity.java
com.ecollege.android.activities.ECollegeActivityHelper.java
com.ecollege.android.activities.ECollegeActivity.java
com.ecollege.android.activities.ECollegeDefaultActivity.java
com.ecollege.android.activities.ECollegeListActivity.java
com.ecollege.android.activities.ECollegeTabActivity.java
com.ecollege.android.adapter.ActivityFeedAdapter.java
com.ecollege.android.adapter.GroupedAdapter.java
com.ecollege.android.adapter.LoadMoreAdapter.java
com.ecollege.android.adapter.ParentAdapterObserver.java
com.ecollege.android.adapter.ResponseAdapter.java
com.ecollege.android.adapter.TopicsAdapter.java
com.ecollege.android.adapter.UberAdapter.java
com.ecollege.android.adapter.UberItem.java
com.ecollege.android.adapter.UpcomingEventsAdapter.java
com.ecollege.android.adapter.WaitingForApiAdapter.java
com.ecollege.android.errors.ECollegeAlertException.java
com.ecollege.android.errors.ECollegeException.java
com.ecollege.android.errors.ECollegePromptException.java
com.ecollege.android.errors.ECollegePromptRetryException.java
com.ecollege.android.tasks.ECollegeAsyncTask.java
com.ecollege.android.tasks.ServiceCallTask.java
com.ecollege.android.tasks.TaskPostProcessor.java
com.ecollege.android.util.CacheConfiguration.java
com.ecollege.android.util.DateTimeUtil.java
com.ecollege.android.util.FileCacheManager.java
com.ecollege.android.util.VolatileCacheManager.java
com.ecollege.android.view.HeaderView.java
com.ecollege.android.view.helpers.ResponseCountViewHelper.java