Java Code Examples for android.text.format.DateUtils#HOUR_IN_MILLIS

The following examples show how to use android.text.format.DateUtils#HOUR_IN_MILLIS . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AppUtils.java    From materialistic with Apache License 2.0 6 votes vote down vote up
public static String getAbbreviatedTimeSpan(long timeMillis) {
    long span = Math.max(System.currentTimeMillis() - timeMillis, 0);
    if (span >= DateUtils.YEAR_IN_MILLIS) {
        return (span / DateUtils.YEAR_IN_MILLIS) + ABBR_YEAR;
    }
    if (span >= DateUtils.WEEK_IN_MILLIS) {
        return (span / DateUtils.WEEK_IN_MILLIS) + ABBR_WEEK;
    }
    if (span >= DateUtils.DAY_IN_MILLIS) {
        return (span / DateUtils.DAY_IN_MILLIS) + ABBR_DAY;
    }
    if (span >= DateUtils.HOUR_IN_MILLIS) {
        return (span / DateUtils.HOUR_IN_MILLIS) + ABBR_HOUR;
    }
    return (span / DateUtils.MINUTE_IN_MILLIS) + ABBR_MINUTE;
}
 
Example 2
Source File: TimeFormatUtil.java    From ExoMedia with Apache License 2.0 6 votes vote down vote up
/**
 * Formats the specified milliseconds to a human readable format
 * in the form of (Hours : Minutes : Seconds).  If the specified
 * milliseconds is less than 0 the resulting format will be
 * "--:--" to represent an unknown time
 *
 * @param milliseconds The time in milliseconds to format
 * @return The human readable time
 */
public static String formatMs(long milliseconds) {
    if (milliseconds < 0) {
        return "--:--";
    }

    long seconds = (milliseconds % DateUtils.MINUTE_IN_MILLIS) / DateUtils.SECOND_IN_MILLIS;
    long minutes = (milliseconds % DateUtils.HOUR_IN_MILLIS) / DateUtils.MINUTE_IN_MILLIS;
    long hours = (milliseconds % DateUtils.DAY_IN_MILLIS) / DateUtils.HOUR_IN_MILLIS;

    formatBuilder.setLength(0);
    if (hours > 0) {
        return formatter.format("%d:%02d:%02d", hours, minutes, seconds).toString();
    }

    return formatter.format("%02d:%02d", minutes, seconds).toString();
}
 
Example 3
Source File: DataReductionStatsPreference.java    From 365browser with Apache License 2.0 6 votes vote down vote up
/**
 * Updates the preference screen to convey current statistics on data reduction.
 */
public void updateReductionStatistics() {
    long original[] = DataReductionProxySettings.getInstance().getOriginalNetworkStatsHistory();
    long received[] = DataReductionProxySettings.getInstance().getReceivedNetworkStatsHistory();

    mCurrentTime = DataReductionProxySettings.getInstance().getDataReductionLastUpdateTime();
    mRightPosition = mCurrentTime + DateUtils.HOUR_IN_MILLIS
            - TimeZone.getDefault().getOffset(mCurrentTime);
    mLeftPosition = mCurrentTime - DateUtils.DAY_IN_MILLIS * DAYS_IN_CHART;
    mOriginalNetworkStatsHistory = getNetworkStatsHistory(original, DAYS_IN_CHART);
    mReceivedNetworkStatsHistory = getNetworkStatsHistory(received, DAYS_IN_CHART);

    if (mDataReductionBreakdownView != null) {
        DataReductionProxySettings.getInstance().queryDataUsage(
                DAYS_IN_CHART, new Callback<List<DataReductionDataUseItem>>() {
                    @Override
                    public void onResult(List<DataReductionDataUseItem> result) {
                        mDataReductionBreakdownView.onQueryDataUsageComplete(result);
                    }
                });
    }
}
 
Example 4
Source File: WidgetHelper.java    From materialistic with Apache License 2.0 6 votes vote down vote up
private void scheduleUpdate(int appWidgetId) {
    String frequency = getConfig(appWidgetId, R.string.pref_widget_frequency);
    long frequencyHourMillis = DateUtils.HOUR_IN_MILLIS * (TextUtils.isEmpty(frequency) ?
            DEFAULT_FREQUENCY_HOUR : Integer.valueOf(frequency));
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        getJobScheduler().schedule(new JobInfo.Builder(appWidgetId,
                new ComponentName(mContext.getPackageName(), WidgetRefreshJobService.class.getName()))
                .setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY)
                .setPeriodic(frequencyHourMillis)
                .build());
    } else {
        mAlarmManager.setInexactRepeating(AlarmManager.RTC,
                System.currentTimeMillis() + frequencyHourMillis,
                frequencyHourMillis,
                createRefreshPendingIntent(appWidgetId));
    }

}
 
Example 5
Source File: LocalNotificationSchedule.java    From OsmGo with MIT License 6 votes vote down vote up
/**
 * Get constant long value representing specific interval of time (weeks, days etc.)
 */
public Long getEveryInterval() {
  switch (every) {
    case "year":
      return DateUtils.YEAR_IN_MILLIS;
    case "month":
      // This case is just approximation as months have different number of days
      return 30 * DateUtils.DAY_IN_MILLIS;
    case "two-weeks":
      return 2 * DateUtils.WEEK_IN_MILLIS;
    case "week":
      return DateUtils.WEEK_IN_MILLIS;
    case "day":
      return DateUtils.DAY_IN_MILLIS;
    case "hour":
      return DateUtils.HOUR_IN_MILLIS;
    case "minute":
      return DateUtils.MINUTE_IN_MILLIS;
    case "second":
      return DateUtils.SECOND_IN_MILLIS;
    default:
      return null;
  }
}
 
Example 6
Source File: TimeUtils.java    From OmniList with GNU Affero General Public License v3.0 6 votes vote down vote up
public static String getTimeLength(Context context, long startMillis, long endMillis){
    if (endMillis < startMillis) return "--";
    long timeSpan = endMillis - startMillis;
    int days = (int) (timeSpan / DateUtils.DAY_IN_MILLIS);
    int hours = (int) (timeSpan % DateUtils.DAY_IN_MILLIS / DateUtils.HOUR_IN_MILLIS);
    int minutes = (int) (timeSpan % DateUtils.DAY_IN_MILLIS % DateUtils.HOUR_IN_MILLIS / DateUtils.MINUTE_IN_MILLIS);
    StringBuilder sb = new StringBuilder();
    if (days != 0){
        sb.append(String.valueOf(days));
    }
    if (hours != 0){
        sb.append(String.valueOf(hours));
    }
    if (minutes != 0){
        sb.append(String.valueOf(minutes));
    }
    return sb.toString();
}
 
Example 7
Source File: ItemModel.java    From cathode with Apache License 2.0 6 votes vote down vote up
public long getNextUpdateTime() {
  // Update at least every six hours
  long updateTime = System.currentTimeMillis() + 6 * DateUtils.HOUR_IN_MILLIS;

  if (getItemCount() > 0) {
    ItemInfo info = getItem(1);
    final long airTime = info.getFirstAired() + DateUtils.HOUR_IN_MILLIS;
    if (airTime < updateTime) {
      updateTime = airTime;
    }
  }

  // Update at most once an hour
  updateTime = Math.max(updateTime, DateUtils.HOUR_IN_MILLIS);

  return updateTime;
}
 
Example 8
Source File: XulSystemUtil.java    From starcor.xul with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * 将给定的毫秒数转换为可读字符串
 *
 * @param milliseconds 待转换的毫秒数
 * @return 该毫秒数转换为 * days * hours * minutes * seconds 后的格式
 */
public static String formatDuring(long milliseconds) {
    long days = milliseconds / DateUtils.DAY_IN_MILLIS;
    long hours = (milliseconds % DateUtils.DAY_IN_MILLIS) / DateUtils.HOUR_IN_MILLIS;
    long minutes = (milliseconds % DateUtils.HOUR_IN_MILLIS) / DateUtils.MINUTE_IN_MILLIS;
    long seconds = (milliseconds % DateUtils.MINUTE_IN_MILLIS) / DateUtils.SECOND_IN_MILLIS;
    return days + " days " + hours + " hours " + minutes + " minutes "
           + seconds + " seconds ";
}
 
Example 9
Source File: HomeActivity.java    From talk-android with MIT License 5 votes vote down vote up
private String sendAliveEvent() {
    String lastUseTimestampStr = MainApp.PREF_UTIL.getString(Constant.LAST_USE_TIMESTAMP, null);
    Date currentTime = new Date();
    if (lastUseTimestampStr != null) {
        Date lastUseTimestamp = DateUtil.parseISO8601(lastUseTimestampStr, DateUtil.DATE_FORMAT_JSON);
        if (currentTime.getTime() - lastUseTimestamp.getTime() > DateUtils.HOUR_IN_MILLIS) {
            AnalyticsHelper.getInstance().sendEvent(AnalyticsHelper.Category.retention, "alive", null);
            Logger.d("Home", "send alive " + currentTime);
        }
    }
    String currentTimeStr = DateUtil.formatISO8601(currentTime, DateUtil.DATE_FORMAT_JSON);
    MainApp.PREF_UTIL.putString(Constant.LAST_USE_TIMESTAMP, currentTimeStr);
    return currentTimeStr;
}
 
Example 10
Source File: TweetDateUtils.java    From twitter-kit-android with Apache License 2.0 5 votes vote down vote up
/**
 * This method is not thread safe. It has been modified from the original to not rely on global
 * time state. If a timestamp is in the future we return it as an absolute date string. Within
 * the same second we return 0s
 *
 * @param res resource
 * @param currentTimeMillis timestamp for offset
 * @param timestamp timestamp
 * @return the relative time string
 */
static String getRelativeTimeString(Resources res, long currentTimeMillis, long timestamp) {
    final long diff = currentTimeMillis - timestamp;
    if (diff >= 0) {
        if (diff < DateUtils.MINUTE_IN_MILLIS) { // Less than a minute ago
            final int secs = (int) (diff / 1000);
            return res.getQuantityString(R.plurals.tw__time_secs, secs, secs);
        } else if (diff < DateUtils.HOUR_IN_MILLIS) { // Less than an hour ago
            final int mins = (int) (diff / DateUtils.MINUTE_IN_MILLIS);
            return res.getQuantityString(R.plurals.tw__time_mins, mins, mins);
        } else if (diff < DateUtils.DAY_IN_MILLIS) { // Less than a day ago
            final int hours = (int) (diff / DateUtils.HOUR_IN_MILLIS);
            return res.getQuantityString(R.plurals.tw__time_hours, hours, hours);
        } else {
            final Calendar now = Calendar.getInstance();
            now.setTimeInMillis(currentTimeMillis);
            final Calendar c = Calendar.getInstance();
            c.setTimeInMillis(timestamp);
            final Date d = new Date(timestamp);

            if (now.get(Calendar.YEAR) == c.get(Calendar.YEAR)) {
                // Same year
                return RELATIVE_DATE_FORMAT.formatShortDateString(res, d);
            } else {
                // Outside of our year
                return RELATIVE_DATE_FORMAT.formatLongDateString(res, d);
            }
        }
    }
    return RELATIVE_DATE_FORMAT.formatLongDateString(res, new Date(timestamp));
}
 
Example 11
Source File: RelativeTimeTextView.java    From v2ex-daily-android with Apache License 2.0 5 votes vote down vote up
public void run() {
    long difference = Math.abs(System.currentTimeMillis() - mReferenceTime);
    long interval = DateUtils.MINUTE_IN_MILLIS;
    if (difference > DateUtils.WEEK_IN_MILLIS) {
        interval = DateUtils.WEEK_IN_MILLIS;
    } else if (difference > DateUtils.DAY_IN_MILLIS) {
        interval = DateUtils.DAY_IN_MILLIS;
    } else if (difference > DateUtils.HOUR_IN_MILLIS) {
        interval = DateUtils.HOUR_IN_MILLIS;
    }
    updateTextDisplay();
    mHandler.postDelayed(this, interval);
}
 
Example 12
Source File: OrgCalendarEntry.java    From mOrgAnd with GNU General Public License v2.0 5 votes vote down vote up
public OrgCalendarEntry(String date) throws IllegalArgumentException {
	Matcher schedule = schedulePattern.matcher(date);

	if (schedule.find()) {
		try {
			if(schedule.group(BEGIN_TIME) == null) { // event is an entire day event
				this.beginTime = CalendarUtils.dateformatter.parse(schedule.group(DATE)).getTime();
				
				// All day events need to be in UTC and end time is exactly one day after
				this.beginTime = CalendarUtils.getDayInUTC(beginTime);
				this.endTime = this.beginTime + DateUtils.DAY_IN_MILLIS;
				this.allDay = 1;
			}
			else if (schedule.group(BEGIN_TIME) != null && schedule.group(END_TIME) != null) {
				this.beginTime = CalendarUtils.dateTimeformatter.parse(schedule.group(DATE) + " " + schedule.group(BEGIN_TIME)).getTime();
				this.endTime = CalendarUtils.dateTimeformatter.parse(schedule.group(DATE) + " " + schedule.group(END_TIME)).getTime();
				this.allDay = 0;
			} else if(schedule.group(BEGIN_TIME) != null) {
				this.beginTime = CalendarUtils.dateTimeformatter.parse(schedule.group(DATE) + " " + schedule.group(BEGIN_TIME)).getTime();
				this.endTime = beginTime + DateUtils.HOUR_IN_MILLIS;
				this.allDay = 0;
			}

			return;
		} catch (ParseException e) {
			Log.w("MobileOrg", "Unable to parse schedule: " + date);
		}
	} else
		throw new IllegalArgumentException("Could not create date out of entry");
}
 
Example 13
Source File: CalendarUtils.java    From android with MIT License 5 votes vote down vote up
/**
 * Get the current daytime in MilliSeconds, compatible with Intervals
 *
 * @return Today's milliseconds (since midnight)
 */
public static long todayMillis() {
    final Calendar cal = GregorianCalendar.getInstance();
    cal.setTime(new Date());

    return cal.get(Calendar.HOUR_OF_DAY) * DateUtils.HOUR_IN_MILLIS
            + cal.get(Calendar.MINUTE) * DateUtils.MINUTE_IN_MILLIS;
}
 
Example 14
Source File: RelativeTimeTextView.java    From Tweetin with Apache License 2.0 5 votes vote down vote up
@Override
public void run() {
	long difference = Math.abs(System.currentTimeMillis() - mRefTime);
          long interval = DateUtils.MINUTE_IN_MILLIS;
          if (difference > DateUtils.WEEK_IN_MILLIS) {
              interval = DateUtils.WEEK_IN_MILLIS;
          } else if (difference > DateUtils.DAY_IN_MILLIS) {
              interval = DateUtils.DAY_IN_MILLIS;
          } else if (difference > DateUtils.HOUR_IN_MILLIS) {
              interval = DateUtils.HOUR_IN_MILLIS;
          }
          updateTextDisplay();
          mHandler.postDelayed(this, interval);
	
}
 
Example 15
Source File: MainActivity.java    From samples with MIT License 4 votes vote down vote up
@Override
public boolean onOptionsItemSelected(MenuItem item) {

    final long min;
    switch (item.getItemId()) {
        case R.id.min_0:
            min = 0l;
            break;
        case R.id.min_min:
            min = DateUtils.MINUTE_IN_MILLIS;
            break;
        case R.id.min_hour:
            min = DateUtils.HOUR_IN_MILLIS;
            break;
        case R.id.min_day:
            min = DateUtils.DAY_IN_MILLIS;
            break;
        default:
            min = -1;
            break;
    }

    if (min >= 0) {
        item.setChecked(true);
        mAdapter.setMinResolution(min);
        return true;
    }

    item.setChecked(!item.isChecked());
    final int flag;
    switch (item.getItemId()) {
        case R.id.FORMAT_SHOW_TIME:
            flag = DateUtils.FORMAT_SHOW_TIME;
            break;
        case R.id.FORMAT_SHOW_WEEKDAY:
            flag = DateUtils.FORMAT_SHOW_WEEKDAY;
            break;
        case R.id.FORMAT_SHOW_YEAR:
            flag = DateUtils.FORMAT_SHOW_YEAR;
            break;
        case R.id.FORMAT_NO_YEAR:
            flag = DateUtils.FORMAT_NO_YEAR;
            break;
        case R.id.FORMAT_SHOW_DATE:
            flag = DateUtils.FORMAT_SHOW_DATE;
            break;
        case R.id.FORMAT_NO_MONTH_DAY:
            flag = DateUtils.FORMAT_NO_MONTH_DAY;
            break;
        case R.id.FORMAT_NO_NOON:
            flag = DateUtils.FORMAT_NO_NOON;
            break;
        case R.id.FORMAT_NO_MIDNIGHT:
            flag = DateUtils.FORMAT_NO_MIDNIGHT;
            break;
        case R.id.FORMAT_ABBREV_TIME:
            flag = DateUtils.FORMAT_ABBREV_TIME;
            break;
        case R.id.FORMAT_ABBREV_WEEKDAY:
            flag = DateUtils.FORMAT_ABBREV_WEEKDAY;
            break;
        case R.id.FORMAT_ABBREV_MONTH:
            flag = DateUtils.FORMAT_ABBREV_MONTH;
            break;
        case R.id.FORMAT_NUMERIC_DATE:
            flag = DateUtils.FORMAT_NUMERIC_DATE;
            break;
        case R.id.FORMAT_ABBREV_RELATIVE:
            flag = DateUtils.FORMAT_ABBREV_RELATIVE;
            break;
        case R.id.FORMAT_ABBREV_ALL:
            flag = DateUtils.FORMAT_ABBREV_ALL;
            break;
        default:
            throw new UnsupportedOperationException("unknown id");
    }

    if (item.isChecked()) {
        mAdapter.addFlag(flag);
    } else {
        mAdapter.removeFlag(flag);
    }

    return true;
}
 
Example 16
Source File: Preferences.java    From android-vlc-remote with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Checks if {@link #setResumeOnIdle()} was called in the last hour.
 * @return {@code true} if {@link #setResumeOnIdle()} was called in the last hour.
 */
public boolean isResumeOnIdleSet() {
    long start = mPreferences.getLong(KEY_RESUME_ON_IDLE, 0L);
    long end = System.currentTimeMillis();
    return start < end && (end - start) < DateUtils.HOUR_IN_MILLIS;
}
 
Example 17
Source File: LotAgendaPeriod.java    From android with MIT License 4 votes vote down vote up
public long getStartMillis() {
    return (long) (getStartHour() * DateUtils.HOUR_IN_MILLIS);
}
 
Example 18
Source File: TimeUtils.java    From OmniList with GNU Affero General Public License v3.0 4 votes vote down vote up
public static int getHour(int timeMillis) {
    return (int) (timeMillis / DateUtils.HOUR_IN_MILLIS);
}
 
Example 19
Source File: TweetDateUtilsTest.java    From twitter-kit-android with Apache License 2.0 4 votes vote down vote up
@Test
public void testGetRelativeTimeString_hoursAgo() {
    final long twoHoursAgo = NOW_IN_MILLIS - DateUtils.HOUR_IN_MILLIS * 2;
    assertEquals("2h",
            TweetDateUtils.getRelativeTimeString(resources, NOW_IN_MILLIS, twoHoursAgo));
}
 
Example 20
Source File: LotAgendaPeriod.java    From android with MIT License 4 votes vote down vote up
public long getEndMillis() {
    return (long) (getEndHour() * DateUtils.HOUR_IN_MILLIS);
}