Search in sources :

Example 6 with Instant

use of org.threeten.bp.Instant in project SeriesGuide by UweTrottmann.

the class TimeTools method formatToLocalDayAndRelativeWeek.

/**
 * Formats to day and relative week in relation to the current system time (e.g. "Mon in 3
 * weeks") as defined by the devices locale.
 * - If the time is today, returns local variant of 'Released today'.
 * - If the time is within the next or previous 6 days, just returns the day.
 * - If the time is more than 6 weeks away, returns the day with a short date.
 *
 * Note: on Android L_MR1 and below, shows date instead as 'in x weeks' is not supported.
 */
public static String formatToLocalDayAndRelativeWeek(Context context, Date thenDate) {
    if (DateUtils.isToday(thenDate.getTime())) {
        return context.getString(R.string.released_today);
    }
    // day abbreviation, e.g. "Mon"
    SimpleDateFormat localDayFormat = new SimpleDateFormat("EEEE", Locale.getDefault());
    StringBuilder dayAndTime = new StringBuilder(localDayFormat.format(thenDate));
    Instant then = Instant.ofEpochMilli(thenDate.getTime());
    ZonedDateTime thenZoned = ZonedDateTime.ofInstant(then, ZoneId.systemDefault());
    // append 'in x wk.' if date is not within a week, for example if today is Thursday:
    // - append for previous Thursday and earlier,
    // - and for next Thursday and later
    long weekDiff = LocalDate.now().until(thenZoned.toLocalDate(), ChronoUnit.WEEKS);
    if (weekDiff != 0) {
        // use weekDiff to calc "now" for relative time string to be daylight saving safe
        // Android L_MR1 and below do not support 'in x wks.', display date instead
        // so make sure that time is the actual time
        Instant now = then.minus(weekDiff * 7, ChronoUnit.DAYS);
        dayAndTime.append(" ");
        if (Math.abs(weekDiff) <= 6) {
            dayAndTime.append(DateUtils.getRelativeTimeSpanString(then.toEpochMilli(), now.toEpochMilli(), DateUtils.WEEK_IN_MILLIS, DateUtils.FORMAT_ABBREV_RELATIVE));
        } else {
            // for everything further away from now display date instead
            dayAndTime.append(formatToLocalDateShort(context, thenDate));
        }
    }
    return dayAndTime.toString();
}
Also used : ZonedDateTime(org.threeten.bp.ZonedDateTime) Instant(org.threeten.bp.Instant) SimpleDateFormat(java.text.SimpleDateFormat)

Example 7 with Instant

use of org.threeten.bp.Instant in project ThreeTenABP by JakeWharton.

the class ExamplesTest method minificationHappened.

/**
 * Assert that ProGuard has run and obfuscated a library type. This implicitly also tests the
 * embedded ProGuard rules in the library are correct since currently ProGuard fails without them.
 */
@Test
public void minificationHappened() {
    Examples activity = examplesActivity.getActivity();
    Instant now = activity.now();
    assertNotEquals("Instant", now.getClass().getSimpleName());
}
Also used : Instant(org.threeten.bp.Instant) Test(org.junit.Test)

Example 8 with Instant

use of org.threeten.bp.Instant in project google-cloud-java by GoogleCloudPlatform.

the class MessageDispatcher method setupNextAckDeadlineExtensionAlarm.

private void setupNextAckDeadlineExtensionAlarm(Instant expiration) {
    Instant possibleNextAlarmTime = expiration.minus(ackExpirationPadding);
    alarmsLock.lock();
    try {
        if (nextAckDeadlineExtensionAlarmTime.isAfter(possibleNextAlarmTime)) {
            logger.log(Level.FINER, "Scheduling next alarm time: {0}, previous alarm time: {1}", new Object[] { possibleNextAlarmTime, nextAckDeadlineExtensionAlarmTime });
            if (ackDeadlineExtensionAlarm != null) {
                logger.log(Level.FINER, "Canceling previous alarm");
                ackDeadlineExtensionAlarm.cancel(false);
            }
            nextAckDeadlineExtensionAlarmTime = possibleNextAlarmTime;
            ackDeadlineExtensionAlarm = systemExecutor.schedule(new AckDeadlineAlarm(), nextAckDeadlineExtensionAlarmTime.toEpochMilli() - clock.millisTime(), TimeUnit.MILLISECONDS);
        }
    } finally {
        alarmsLock.unlock();
    }
}
Also used : Instant(org.threeten.bp.Instant)

Example 9 with Instant

use of org.threeten.bp.Instant in project open-event-android by fossasia.

the class DateConverter method getRelativeTimeFromUTCTimeStamp.

public static String getRelativeTimeFromUTCTimeStamp(String timeStamp) throws DateTimeParseException {
    Instant timestampInstant = Instant.parse(timeStamp);
    ZonedDateTime timeCreatedDate = ZonedDateTime.ofInstant(timestampInstant, getZoneId());
    return (String) android.text.format.DateUtils.getRelativeTimeSpanString((timeCreatedDate.toInstant().toEpochMilli()), System.currentTimeMillis(), android.text.format.DateUtils.SECOND_IN_MILLIS);
}
Also used : ZonedDateTime(org.threeten.bp.ZonedDateTime) Instant(org.threeten.bp.Instant)

Example 10 with Instant

use of org.threeten.bp.Instant in project google-cloud-java by GoogleCloudPlatform.

the class FindingSnippets method listFindingsAtTime.

// [END securitycenter_list_filtered_findings]
/**
 * List findings at a specific time under a source.
 *
 * @param sourceName The source to list findings at a specific time for.
 */
// [START securitycenter_list_findings_at_time]
static ImmutableList<ListFindingsResult> listFindingsAtTime(SourceName sourceName) {
    try (SecurityCenterClient client = SecurityCenterClient.create()) {
        // SourceName sourceName = SourceName.of(/*organizationId=*/"123234324",
        // /*sourceId=*/"423432321");
        // 5 days ago
        Instant fiveDaysAgo = Instant.now().minus(Duration.ofDays(5));
        ListFindingsRequest.Builder request = ListFindingsRequest.newBuilder().setParent(sourceName.toString()).setReadTime(Timestamp.newBuilder().setSeconds(fiveDaysAgo.getEpochSecond()).setNanos(fiveDaysAgo.getNano()));
        // Call the API.
        ListFindingsPagedResponse response = client.listFindings(request.build());
        // This creates one list for all findings.  If your organization has a large number of
        // findings this can cause out of memory issues.  You can process them in incrementally
        // by returning the Iterable returned response.iterateAll() directly.
        ImmutableList<ListFindingsResult> results = ImmutableList.copyOf(response.iterateAll());
        System.out.println("Findings:");
        System.out.println(results);
        return results;
    } catch (IOException e) {
        throw new RuntimeException("Couldn't create client.", e);
    }
}
Also used : Instant(org.threeten.bp.Instant) ListFindingsRequest(com.google.cloud.securitycenter.v1.ListFindingsRequest) IOException(java.io.IOException) SecurityCenterClient(com.google.cloud.securitycenter.v1.SecurityCenterClient) ListFindingsPagedResponse(com.google.cloud.securitycenter.v1.SecurityCenterClient.ListFindingsPagedResponse) ListFindingsResult(com.google.cloud.securitycenter.v1.ListFindingsResponse.ListFindingsResult)

Aggregations

Instant (org.threeten.bp.Instant)17 SecurityCenterClient (com.google.cloud.securitycenter.v1.SecurityCenterClient)6 IOException (java.io.IOException)6 Test (org.junit.Test)5 Finding (com.google.cloud.securitycenter.v1.Finding)4 ZonedDateTime (org.threeten.bp.ZonedDateTime)4 Value (com.google.protobuf.Value)2 GroupFindingsRequest (com.google.cloud.securitycenter.v1.GroupFindingsRequest)1 GroupResult (com.google.cloud.securitycenter.v1.GroupResult)1 ListFindingsRequest (com.google.cloud.securitycenter.v1.ListFindingsRequest)1 ListFindingsResult (com.google.cloud.securitycenter.v1.ListFindingsResponse.ListFindingsResult)1 GroupFindingsPagedResponse (com.google.cloud.securitycenter.v1.SecurityCenterClient.GroupFindingsPagedResponse)1 ListFindingsPagedResponse (com.google.cloud.securitycenter.v1.SecurityCenterClient.ListFindingsPagedResponse)1 UpdateFindingRequest (com.google.cloud.securitycenter.v1.UpdateFindingRequest)1 FieldMask (com.google.protobuf.FieldMask)1 ReceivedMessage (com.google.pubsub.v1.ReceivedMessage)1 SimpleDateFormat (java.text.SimpleDateFormat)1 ArrayList (java.util.ArrayList)1 Clock (org.threeten.bp.Clock)1 DayOfWeek (org.threeten.bp.DayOfWeek)1