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

The following examples show how to use android.text.format.DateUtils#SECOND_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: ConnectivityController.java    From android_9.0.0_r45 with Apache License 2.0 6 votes vote down vote up
/**
 * Test to see if running the given job on the given network is insane.
 * <p>
 * For example, if a job is trying to send 10MB over a 128Kbps EDGE
 * connection, it would take 10.4 minutes, and has no chance of succeeding
 * before the job times out, so we'd be insane to try running it.
 */
@SuppressWarnings("unused")
private static boolean isInsane(JobStatus jobStatus, Network network,
        NetworkCapabilities capabilities, Constants constants) {
    final long estimatedBytes = jobStatus.getEstimatedNetworkBytes();
    if (estimatedBytes == JobInfo.NETWORK_BYTES_UNKNOWN) {
        // We don't know how large the job is; cross our fingers!
        return false;
    }

    // We don't ask developers to differentiate between upstream/downstream
    // in their size estimates, so test against the slowest link direction.
    final long slowest = NetworkCapabilities.minBandwidth(
            capabilities.getLinkDownstreamBandwidthKbps(),
            capabilities.getLinkUpstreamBandwidthKbps());
    if (slowest == LINK_BANDWIDTH_UNSPECIFIED) {
        // We don't know what the network is like; cross our fingers!
        return false;
    }

    final long estimatedMillis = ((estimatedBytes * DateUtils.SECOND_IN_MILLIS)
            / (slowest * TrafficStats.KB_IN_BYTES / 8));
    if (estimatedMillis > JobServiceContext.EXECUTING_TIMESLICE_MILLIS) {
        // If we'd never finish before the timeout, we'd be insane!
        Slog.w(TAG, "Estimated " + estimatedBytes + " bytes over " + slowest
                + " kbps network would take " + estimatedMillis + "ms; that's insane!");
        return true;
    } else {
        return false;
    }
}
 
Example 2
Source File: LrcEntry.java    From YCAudioPlayer with Apache License 2.0 6 votes vote down vote up
private static List<LrcEntry> parseLine(String line) {
    if (TextUtils.isEmpty(line)) {
        return null;
    }

    line = line.trim();
    Matcher lineMatcher = Pattern.compile("((\\[\\d\\d:\\d\\d\\.\\d\\d\\])+)(.+)").matcher(line);
    if (!lineMatcher.matches()) {
        return null;
    }

    String times = lineMatcher.group(1);
    String text = lineMatcher.group(3);
    List<LrcEntry> entryList = new ArrayList<>();

    Matcher timeMatcher = Pattern.compile("\\[(\\d\\d):(\\d\\d)\\.(\\d\\d)\\]").matcher(times);
    while (timeMatcher.find()) {
        long min = Long.parseLong(timeMatcher.group(1));
        long sec = Long.parseLong(timeMatcher.group(2));
        long mil = Long.parseLong(timeMatcher.group(3));
        long time = min * DateUtils.MINUTE_IN_MILLIS + sec * DateUtils.SECOND_IN_MILLIS + mil * 10;
        entryList.add(new LrcEntry(time, text));
    }
    return entryList;
}
 
Example 3
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 4
Source File: LocationRequestUtils.java    From android-location-service with Apache License 2.0 6 votes vote down vote up
public static LocationRequest byMilesPerHour(float mph, int intervalInSeconds) {

        // convert mph to mps
        final float metersPerSecond = MILES_PER_HOUR_TO_METERS_PER_SECOND * mph;

        // convert to requested interval
        final float metersPerInterval = metersPerSecond * intervalInSeconds;

        // convert seconds to milliseconds
        final long intervalInMillis = intervalInSeconds * DateUtils.SECOND_IN_MILLIS;

        // create the request
        return LocationRequest.create()
                .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
                .setInterval(intervalInMillis)
                .setFastestInterval(intervalInMillis)
                .setSmallestDisplacement(metersPerInterval);
    }
 
Example 5
Source File: BeaconControlManagerImpl.java    From BeaconControl_Android_SDK with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private boolean tokenIsInvalid(TokenResponse token) {
    if (token.createdAt == REFRESH_TOKEN_STRATEGY_NOT_USED || token.expiresIn == REFRESH_TOKEN_STRATEGY_NOT_USED) {
        ULog.d(TAG, "Refresh token strategy not used.");
        return false;
    }

    long nowInSeconds = System.currentTimeMillis() / DateUtils.SECOND_IN_MILLIS;
    return nowInSeconds > (token.createdAt + token.expiresIn);
}
 
Example 6
Source File: LastSeenTimeUtil.java    From iGap-Android with GNU Affero General Public License v3.0 5 votes vote down vote up
private static String getMinute(long time) {

        long currentTime = System.currentTimeMillis();
        long difference = (currentTime - (time * DateUtils.SECOND_IN_MILLIS));
        //difference = -(70 * DateUtils.MINUTE_IN_MILLIS);
        if (TimeUnit.MILLISECONDS.toMinutes(difference) <= 0) {
            return G.fragmentActivity.getResources().getString(R.string.last_seen_recently);
        }

        /*else if (TimeUnit.MILLISECONDS.toMinutes(difference) >= 61) {
            return "*"+G.fragmentActivity.getResources().getString(R.string.last_seen_recently);
        }*/
        String str;
        if (HelperCalander.isPersianUnicode) {
            str = TimeUnit.MILLISECONDS.toMinutes(difference) + " " + "\u200F" + G.fragmentActivity.getResources().getString(R.string.minute_ago);
        } else {

            long minute = TimeUnit.MILLISECONDS.toMinutes(difference);
            String minuteAgo = G.fragmentActivity.getResources().getString(R.string.minute_ago);

            if (minute >= 2) {
                minuteAgo = G.fragmentActivity.getResources().getString(R.string.minutes_ago);
            }

            str = minute + " " + minuteAgo;
        }

        if (HelperCalander.isPersianUnicode) {
            str = HelperCalander.convertToUnicodeFarsiNumber(str);
        }
        return str;
    }
 
Example 7
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 8
Source File: PlayActivity.java    From AndroidDemo with MIT License 5 votes vote down vote up
public static String formatTime(String pattern, long milli) {
    int m = (int) (milli / DateUtils.MINUTE_IN_MILLIS);
    int s = (int) ((milli / DateUtils.SECOND_IN_MILLIS) % 60);
    String mm = String.format(Locale.getDefault(), "%02d", m);
    String ss = String.format(Locale.getDefault(), "%02d", s);
    return pattern.replace("mm", mm).replace("ss", ss);
}
 
Example 9
Source File: ApiSettings.java    From cathode with Apache License 2.0 5 votes vote down vote up
@Override public void updateTokens(AccessToken tokens) {
  synchronized (this) {
    final long expirationMillis =
        System.currentTimeMillis() + (tokens.getExpires_in() * DateUtils.SECOND_IN_MILLIS);
    Settings.get(context)
        .edit()
        .putString(TraktLinkSettings.TRAKT_ACCESS_TOKEN, tokens.getAccess_token())
        .putString(TraktLinkSettings.TRAKT_REFRESH_TOKEN, tokens.getRefresh_token())
        .putLong(TraktLinkSettings.TRAKT_TOKEN_EXPIRATION, expirationMillis)
        .apply();
  }
}
 
Example 10
Source File: MainActivity.java    From android-player-samples with Apache License 2.0 5 votes vote down vote up
/**
 * Specify where the ad should interrupt the main video.  This code provides a procedural
 * abastraction for the Google IMA Plugin setup code.
 */
private void setupCuePoints(Source source) {
    String cuePointType = "ad";
    Map<String, Object> properties = new HashMap<String, Object>();
    Map<String, Object> details = new HashMap<String, Object>();

    // preroll
    CuePoint cuePoint = new CuePoint(CuePoint.PositionType.BEFORE, cuePointType, properties);
    details.put(Event.CUE_POINT, cuePoint);
    eventEmitter.emit(EventType.SET_CUE_POINT, details);

    // midroll at 10 seconds.
    // Due HLS bugs in the Android MediaPlayer, midrolls are not supported.
    if (!source.getDeliveryType().equals(DeliveryType.HLS)) {
        int cuepointTime = 10 * (int) DateUtils.SECOND_IN_MILLIS;
        cuePoint = new CuePoint(cuepointTime, cuePointType, properties);
        details.put(Event.CUE_POINT, cuePoint);
        eventEmitter.emit(EventType.SET_CUE_POINT, details);
        // Add a marker where the ad will be.
        mediaController.getBrightcoveSeekBar().addMarker(cuepointTime);
    }

    // postroll
    cuePoint = new CuePoint(CuePoint.PositionType.AFTER, cuePointType, properties);
    details.put(Event.CUE_POINT, cuePoint);
    eventEmitter.emit(EventType.SET_CUE_POINT, details);
}
 
Example 11
Source File: TransactionDetailActivity.java    From alpha-wallet-android with MIT License 5 votes vote down vote up
private String localiseUnixTime(long timeStampInSec)
{
    Date date = new java.util.Date(timeStampInSec*DateUtils.SECOND_IN_MILLIS);
    DateFormat timeFormat = java.text.DateFormat.getTimeInstance(DateFormat.SHORT, LocaleUtils.getDeviceLocale(this));
    DateFormat dateFormat = java.text.DateFormat.getDateInstance(DateFormat.MEDIUM, LocaleUtils.getDeviceLocale(this));
    return timeFormat.format(date) + " | " + dateFormat.format(date);
}
 
Example 12
Source File: RouteDescriptionHandler.java    From msdkui-android with Apache License 2.0 5 votes vote down vote up
private long getTta(final boolean isTraffic) {
    if (isTraffic) {
        return mRoute.getTtaIncludingTraffic(WHOLE_ROUTE)
                .getDuration() * DateUtils.SECOND_IN_MILLIS;
    }
    return mRoute.getTtaExcludingTraffic(WHOLE_ROUTE)
            .getDuration() * DateUtils.SECOND_IN_MILLIS;
}
 
Example 13
Source File: QuitTimer.java    From YCAudioPlayer with Apache License 2.0 5 votes vote down vote up
@Override
public void run() {
    mTimerRemain -= DateUtils.SECOND_IN_MILLIS;
    if (mTimerRemain > 0) {
        mTimerCallback.onEvent(mTimerRemain);
        mHandler.postDelayed(this, DateUtils.SECOND_IN_MILLIS);
    } else {
        mPlayService.quit();
    }
}
 
Example 14
Source File: QuitTimer.java    From YCAudioPlayer with Apache License 2.0 5 votes vote down vote up
public void start(long milli) {
    if(mHandler==null){
        ToastUtils.showShort("请先进行初始化");
        return;
    }
    stop();
    if (milli > 0) {
        mTimerRemain = milli + DateUtils.SECOND_IN_MILLIS;
        mHandler.post(mQuitRunnable);
    } else {
        mTimerRemain = 0;
        mTimerCallback.onEvent(mTimerRemain);
    }
}
 
Example 15
Source File: YCLrcCustomView.java    From YCAudioPlayer with Apache License 2.0 5 votes vote down vote up
private String formatTime(long milli) {
    int m = (int) (milli / DateUtils.MINUTE_IN_MILLIS);
    int s = (int) ((milli / DateUtils.SECOND_IN_MILLIS) % 60);
    String mm = String.format(Locale.getDefault(), "%02d", m);
    String ss = String.format(Locale.getDefault(), "%02d", s);
    return mm + ":" + ss;
}
 
Example 16
Source File: Chronometer.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
private static String formatDuration(long ms) {
    int duration = (int) (ms / DateUtils.SECOND_IN_MILLIS);
    if (duration < 0) {
        duration = -duration;
    }

    int h = 0;
    int m = 0;

    if (duration >= HOUR_IN_SEC) {
        h = duration / HOUR_IN_SEC;
        duration -= h * HOUR_IN_SEC;
    }
    if (duration >= MIN_IN_SEC) {
        m = duration / MIN_IN_SEC;
        duration -= m * MIN_IN_SEC;
    }
    final int s = duration;

    final ArrayList<Measure> measures = new ArrayList<Measure>();
    if (h > 0) {
        measures.add(new Measure(h, MeasureUnit.HOUR));
    }
    if (m > 0) {
        measures.add(new Measure(m, MeasureUnit.MINUTE));
    }
    measures.add(new Measure(s, MeasureUnit.SECOND));

    return MeasureFormat.getInstance(Locale.getDefault(), FormatWidth.WIDE)
                .formatMeasures(measures.toArray(new Measure[measures.size()]));
}
 
Example 17
Source File: RealmRoomMessage.java    From iGap-Android with GNU Affero General Public License v3.0 5 votes vote down vote up
public static boolean isBothDelete(long messageTime) {
    long currentTime;
    if (G.userLogin) {
        currentTime = G.currentServerTime * DateUtils.SECOND_IN_MILLIS;
    } else {
        currentTime = System.currentTimeMillis();
    }

    return !HelperTimeOut.timeoutChecking(currentTime, messageTime, G.bothChatDeleteTime);
}
 
Example 18
Source File: ConfigImpl.java    From BeaconControl_Android_SDK with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public long getEventsSendoutDelayInMillis() {
    return resources.getInteger(R.integer.sdk_config__events_sendout_timeout_seconds) * DateUtils.SECOND_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_secondsAgo() {
    final long tenSecondsAgo = NOW_IN_MILLIS - DateUtils.SECOND_IN_MILLIS * 10;
    assertEquals("10s",
            TweetDateUtils.getRelativeTimeString(resources, NOW_IN_MILLIS, tenSecondsAgo));
}
 
Example 20
Source File: LastSeenTimeUtil.java    From iGap-Android with GNU Affero General Public License v3.0 4 votes vote down vote up
/**
 * get millis and return days
 *
 * @param beforeMillis before time that user was online
 * @return exactly time if is lower than one days otherwise return days
 */

private static String computeDays(long beforeMillis, boolean ltr) {

    String time = "";

    String exactlyTime = String.valueOf(String.valueOf(HelperCalander.getClocktime(beforeMillis * DateUtils.SECOND_IN_MILLIS, ltr)));

    long currentMillis = System.currentTimeMillis();
    int days = (int) ((currentMillis - (beforeMillis * DateUtils.SECOND_IN_MILLIS)) / DateUtils.DAY_IN_MILLIS);
    if (days <= 7) {
        switch (days) {
            case 0:
                time = HelperCalander.getClocktime(beforeMillis * DateUtils.SECOND_IN_MILLIS, ltr);

                Calendar date = Calendar.getInstance();
                date.setTimeInMillis(beforeMillis * DateUtils.SECOND_IN_MILLIS);

                if (Calendar.getInstance().get(Calendar.DAY_OF_YEAR) != date.get(Calendar.DAY_OF_YEAR)) {
                    time = G.fragmentActivity.getResources().getString(R.string.yesterday) + " " + time;
                }

                break;
            case 1:
                //time = G.fragmentActivity.getResources().getString(R.string.last_seen) + " " + G.fragmentActivity.getResources().getString(R.string.yesterday) + " " + exactlyTime;
                time = G.fragmentActivity.getResources().getString(R.string.yesterday) + " " + exactlyTime;
                break;
            case 2:
                time = G.fragmentActivity.getResources().getString(R.string.two_day);//+ exactlyTime
                break;
            case 3:
                time = G.fragmentActivity.getResources().getString(R.string.three_day);//+ exactlyTime
                break;
            case 4:
                time = G.fragmentActivity.getResources().getString(R.string.four_day);// + exactlyTime
                break;
            case 5:
                time = G.fragmentActivity.getResources().getString(R.string.five_day);// + exactlyTime
                break;
            case 6:
                time = G.fragmentActivity.getResources().getString(R.string.six_day);// + exactlyTime
                break;
            case 7:
                time = G.fragmentActivity.getResources().getString(R.string.last_week);
                break;
        }
    } else {
        if (beforeMillis == 0) {
            time = G.fragmentActivity.getResources().getString(R.string.last_seen_recently);
        } else {
            time = HelperCalander.checkHijriAndReturnTime(beforeMillis) + " " + exactlyTime;
        }
    }

    if (HelperCalander.isPersianUnicode) {
        time = HelperCalander.convertToUnicodeFarsiNumber(time);
    }

    return time;
}