下面列出了java.time.OffsetDateTime#getDayOfMonth ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Calculates the period for which the resource usage like volume of used data, connection duration etc.
* is to be retrieved from the Prometheus server based on the mode defined by
* {@link TenantConstants#FIELD_PERIOD_MODE}.
*
* @param effectiveSince The point of time on which the resource limit came into effect.
* @param currentDateTime The current date and time used for the resource usage period calculation.
* @param mode The mode of the period defined by {@link TenantConstants#FIELD_PERIOD_MODE}.
* @param periodInDays The number of days defined by {@link TenantConstants#FIELD_PERIOD_NO_OF_DAYS}.
* @return The period in days for which the resource usage is to be calculated.
*/
long calculateResourceUsagePeriod(
final OffsetDateTime effectiveSince,
final OffsetDateTime currentDateTime,
final PeriodMode mode,
final long periodInDays) {
final long inclusiveDaysBetween = ChronoUnit.DAYS.between(effectiveSince, currentDateTime) + 1;
switch (mode) {
case DAYS:
if (inclusiveDaysBetween > 0 && periodInDays > 0) {
final long dataUsagePeriodInDays = inclusiveDaysBetween % periodInDays;
return dataUsagePeriodInDays == 0 ? periodInDays : dataUsagePeriodInDays;
}
return 0L;
case MONTHLY:
if (YearMonth.from(currentDateTime).equals(YearMonth.from(effectiveSince))
&& effectiveSince.getDayOfMonth() != 1) {
return inclusiveDaysBetween;
}
return currentDateTime.getDayOfMonth();
default:
return 0L;
}
}
void initImageCreationTime(OffsetDateTime offsetDateTime) {
// Check for incoming arguments
if (offsetDateTime != null) {
// set values that make up Standard/Document/ImageCreationTime
creation_time_present = true;
creation_time_year = offsetDateTime.getYear();
creation_time_month = offsetDateTime.getMonthValue();
creation_time_day = offsetDateTime.getDayOfMonth();
creation_time_hour = offsetDateTime.getHour();
creation_time_minute = offsetDateTime.getMinute();
creation_time_second = offsetDateTime.getSecond();
creation_time_offset = offsetDateTime.getOffset();
}
}
public static int writeGeneralizedTime(Date time, byte[] out, int offset) {
OffsetDateTime offsetTime = time.toInstant().atOffset(ZoneOffset.UTC);
int idx = offset;
out[idx++] = 0x18;
out[idx++] = 15;
// yyyyMMddhhmmssZ
// year
int year = offsetTime.getYear();
out[idx++] = (byte) (0x30 + year / 1000);
out[idx++] = (byte) (0x30 + year / 100 % 10);
out[idx++] = (byte) (0x30 + year / 10 % 10);
out[idx++] = (byte) (0x30 + year % 10);
// month
int month = offsetTime.getMonthValue();
out[idx++] = (byte) (0x30 + month / 10);
out[idx++] = (byte) (0x30 + month % 10);
// day
int day = offsetTime.getDayOfMonth();
out[idx++] = (byte) (0x30 + day / 10);
out[idx++] = (byte) (0x30 + day % 10);
// hour
int hour = offsetTime.getHour();
out[idx++] = (byte) (0x30 + hour / 10);
out[idx++] = (byte) (0x30 + hour % 10);
// minute
int minute = offsetTime.getMinute();
out[idx++] = (byte) (0x30 + minute / 10);
out[idx++] = (byte) (0x30 + minute % 10);
// second
int second = offsetTime.getSecond();
out[idx++] = (byte) (0x30 + second / 10);
out[idx++] = (byte) (0x30 + second % 10);
out[idx++] = 'Z';
return idx - offset;
}
/**
* Calculates the effective resource limit for a tenant for the given period from the configured values.
* <p>
* In the <em>monthly</em> mode, if the effectiveSince date doesn't fall on the
* first day of the month then the effective resource limit for the tenant is
* calculated as below.
* <pre>
* configured limit
* ---------------------------------- x No. of days from effectiveSince till lastDay of the targetDateMonth.
* No. of days in the current month
* </pre>
* <p>
* For rest of the months and the <em>days</em> mode, the configured limit is used directly.
*
* @param effectiveSince The point of time on which the given resource limit came into effect.
* @param targetDateTime The target date and time.
* @param mode The mode of the period.
* @param configuredLimit The configured limit.
* @return The effective resource limit that has been calculated.
*/
long calculateEffectiveLimit(
final OffsetDateTime effectiveSince,
final OffsetDateTime targetDateTime,
final PeriodMode mode,
final long configuredLimit) {
if (PeriodMode.MONTHLY.equals(mode)
&& configuredLimit > 0
&& !targetDateTime.isBefore(effectiveSince)
&& YearMonth.from(targetDateTime).equals(YearMonth.from(effectiveSince))
&& effectiveSince.getDayOfMonth() != 1) {
final OffsetDateTime lastDayOfMonth = effectiveSince.with(TemporalAdjusters.lastDayOfMonth());
final long daysBetween = ChronoUnit.DAYS
.between(effectiveSince, lastDayOfMonth) + 1;
return Double.valueOf(Math.ceil(daysBetween * configuredLimit / lastDayOfMonth.getDayOfMonth()))
.longValue();
}
return configuredLimit;
}
int getDay(OffsetDateTime offsetDateTime) {
return offsetDateTime.getDayOfMonth();
}