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();
}
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());
}
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();
}
}
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);
}
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);
}
}
Aggregations