月份枚举 java_Java Data Type - 如何创建枚举以表示各种下一持续时间,小时,周,月,年...

这是一个Java枚举示例,定义了`RepeatingDuration`,用于表示小时、天、周、月和年的周期性间隔。每个枚举常量包含计算下一个周期时间点的方法,基于`ZonedDateTime`进行时间操作。
摘要由CSDN通过智能技术生成

/*

* JLibs: Common Utilities for Java

* Copyright (C) 2009 Santhosh Kumar T

*

* This library is free software; you can redistribute it and/or

* modify it under the terms of the GNU Lesser General Public

* License as published by the Free Software Foundation; either

* version 2.1 of the License, or (at your option) any later version.

*

* This library is distributed in the hope that it will be useful,

* but WITHOUT ANY WARRANTY; without even the implied warranty of

* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU

* Lesser General Public License for more details.

*/

//package jlibs.nio.util;

import java.text.SimpleDateFormat;

import java.time.Clock;

import java.time.DayOfWeek;

import java.time.Instant;

import java.time.ZonedDateTime;

import java.time.temporal.TemporalAdjusters;

import java.util.Date;

/**

* @author Santhosh

*/

public enum RepeatingDuration{

HOURLY{

protected long next(ZonedDateTime zdt){

return zdt.plusHours(1)

.withMinute(0).withSecond(0).withNano(0)

.toInstant().toEpochMilli();

}

},

DAILY{

protected long next(ZonedDateTime zdt){

return zdt.plusDays(1)

.withHour(0).withMinute(0).withSecond(0).withNano(0)

.toInstant().toEpochMilli();

}

},

WEEKLY{

protected long next(ZonedDateTime zdt){

return zdt.plusWeeks(1).with(DayOfWeek.SUNDAY)

.withHour(0).withMinute(0).withSecond(0).withNano(0)

.toInstant().toEpochMilli();

}

},

MONTHLY{

protected long next(ZonedDateTime zdt){

return zdt.with(TemporalAdjusters.firstDayOfNextMonth())

.withHour(0).withMinute(0).withSecond(0).withNano(0)

.toInstant().toEpochMilli();

}

},

YEARLY{

protected long next(ZonedDateTime zdt){

return zdt.with(TemporalAdjusters.firstDayOfNextYear())

.withHour(0).withMinute(0).withSecond(0).withNano(0)

.toInstant().toEpochMilli();

}

};

protected abstract long next(ZonedDateTime zdt);

public long next(long from){

return next(ZonedDateTime.ofInstant(Instant.ofEpochMilli(from), Clock.systemDefaultZone().getZone()));

}

public long next(){

return next(ZonedDateTime.now());

}

public static RepeatingDuration forFormat(String format){

SimpleDateFormat sdf = new SimpleDateFormat(format);

Date date = new Date();

String valueNow = sdf.format(date);

for(RepeatingDuration rd: values()){

if(!sdf.format(new Date(rd.next(date.getTime()))).equals(valueNow))

return rd;

}

return null;

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值