java获取自然日,自然周,自然月的开始时间

public static long  nextMonthStartTimeMilli(long timestamp) {

Clock clock = Clock.fixed(Instant.ofEpochMilli(timestamp),DEFAULT_ZONE_ID);

Month nextMonth = MonthDay.now(clock).getMonth().plus(1);

return LocalDate.now(clock)

.with(MonthDay.of(nextMonth,1))

.atStartOfDay(DEFAULT_ZONE_ID)

.toInstant()

.toEpochMilli();

}

public static long  nextWeekStartTimeMilli(long timestamp) {

return LocalDate.now(Clock.fixed(Instant.ofEpochMilli(timestamp),DEFAULT_ZONE_ID))

.plusWeeks(1)

.with(DayOfWeek.MONDAY)

.atStartOfDay(DEFAULT_ZONE_ID)

.toInstant()

.toEpochMilli();

}

public static long  tomorrowStartTimeMilli(long timestamp) {

return LocalDate.now(Clock.fixed(Instant.ofEpochMilli(timestamp),DEFAULT_ZONE_ID))

.plusDays(1)

.atStartOfDay(DEFAULT_ZONE_ID)

.toInstant()

.toEpochMilli();

}

/**

* 距离下个自然月剩余的毫秒数

*@return

*/

public static long nextMonthLeftTimeMilli() {

long now = System.currentTimeMillis();

return nextMonthStartTimeMilli(now) - now;

}

/**

* 距离下个周一剩余的毫秒数

*@return

*/

public static long nextWeekLeftTimeMilli() {

long now = System.currentTimeMillis();

return nextWeekStartTimeMilli(now) - now;

}

/**

* 距离明天剩余的毫秒数

*@return

*/

public static long tomorrowLeftTimeMilli() {

long now = System.currentTimeMillis();

return tomorrowStartTimeMilli(now) - now;

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容