比较java中的日期时间(以天为单位)hh:mm hh:mm
我们如何在java中压缩日期并获得剩余的可用插槽比较java中的日期时间(以天为单位)hh:mm hh:mm,java,date,datetime,time,dayofweek,Java,Date,Datetime,Time,Dayofweek,我们如何在java中压缩日期并获得剩余的可用插槽 Sun 10:00-11:00 Sun 12:00-14:00 Sun 15:00-16:00 那么,除了这三个时段之外,我们怎样才能获得一天的所有可用时间呢?我们如何在java中比较这些日期?有人可以建议吗?使用org.apache.commons.lang3.time.DateUtils为时间添加特定的小时数,并使用java.util.Date进行各种比较。希望这会有所帮助。谢谢。您可以使用比较法。定义是int java.util.Date
Sun 10:00-11:00
Sun 12:00-14:00
Sun 15:00-16:00
那么,除了这三个时段之外,我们怎样才能获得一天的所有可用时间呢?我们如何在java中比较这些日期?有人可以建议吗?使用
org.apache.commons.lang3.time.DateUtils
为时间添加特定的小时数,并使用java.util.Date
进行各种比较。希望这会有所帮助。谢谢。您可以使用比较法。定义是int java.util.Date.与(日期另一个日期)进行比较。示例代码如下:
Calendar cal1 = Calendar.getInstance();
cal1.set(2015, Calendar.JANUARY, 1);
Calendar cal2 = Calendar.getInstance();
cal2.set(2016, Calendar.JANUARY, 1);
Date date1 = cal1.getTime();
Date date2 = cal2.getTime();
//Print -1, negative
System.out.println(date1.compareTo(date2));
cal1.set(2017, Calendar.JANUARY, 1);
date1 = cal1.getTime();
//Print 1, positive
System.out.println(date1.compareTo(date2));
cal1 = cal2;
date1 = cal1.getTime();
//Print 0, equal
System.out.println(date1.compareTo(date2));
为占用的插槽设计一个类。听起来您需要一个
DayOfWeek
字段和两个LocalTime
字段。将日期时间解析为这样的对象:在连字符处拆分,并使用两个DateTimeFormatter
s。对对象进行排序。对于每对相邻的对象,确定它们之间是否有空槽(如果有重叠,也报告错误)。您还需要决定是否在第一个日期时间之前(星期日上午00点到10点?)和最后一个日期之后考虑空置时隙。
我有意提到类和java.time
中的enum
,这是现代java日期和时间API。与之合作很好,我毫不犹豫地推荐它
编辑:您现在有五天的时间编写您的课程,所以现在可能是时候分享我的版本,供您比较,并供将来的读者阅读
public class Slot {
private static Pattern textPattern = Pattern.compile("(\\w+) ([0-9:]+)-([0-9:]+)");
private static DateTimeFormatter dayFormatter = DateTimeFormatter.ofPattern("EEE");
public static Slot parse(String text) {
Matcher textMatcher = textPattern.matcher(text);
if (textMatcher.matches()) {
DayOfWeek day = DayOfWeek.from(dayFormatter.parse(textMatcher.group(1)));
LocalTime startTime = LocalTime.parse(textMatcher.group(2));
LocalTime endTime = LocalTime.parse(textMatcher.group(3));
return new Slot(day, startTime, endTime);
} else {
throw new IllegalArgumentException("Unparsable slot " + text + ", expected format Sun 12:00-14:00");
}
}
private final DayOfWeek day;
private final LocalTime startTime;
private final LocalTime endTime;
Slot(DayOfWeek day, LocalTime startTime, LocalTime endTime) {
if (! endTime.isAfter(startTime)) {
throw new IllegalArgumentException("End time must be after start time");
}
this.day = Objects.requireNonNull(day);
this.startTime = startTime;
this.endTime = endTime;
}
public DayOfWeek getDay() {
return day;
}
public LocalTime getStartTime() {
return startTime;
}
public LocalTime getEndTime() {
return endTime;
}
/**
* @param nextSlot
* @return A new Slot object representing the vacant Slot between this Slot and nextSlot,
* or an empty Optional if no gap
*/
public Optional<Slot> slotBetween(Slot nextSlot) {
if (! nextSlot.getDay().equals(day)) {
throw new IllegalArgumentException("Cannot compare slots on different days of week");
}
if (nextSlot.getStartTime().isBefore(endTime)) {
throw new IllegalArgumentException("Error: overlap between slots " + this + " and " + nextSlot);
}
if (nextSlot.getStartTime().isAfter(endTime)) { // there is a gap
return Optional.of(new Slot(day, endTime, nextSlot.getStartTime()));
} else {
return Optional.empty();
}
}
@Override
public String toString() {
return dayFormatter.format(day) + ' ' + startTime + '-' + endTime;
}
}
公共类插槽{
私有静态模式textPattern=Pattern.compile(“(\\w+)([0-9:]+)-([0-9:]+)”;
私有静态DateTimeFormatter dayFormatter=DateTimeFormatter.of模式(“EEE”);
公共静态插槽解析(字符串文本){
Matcher textmacher=textPattern.Matcher(文本);
if(textMatcher.matches()){
DayOfWeek day=DayOfWeek.from(dayFormatter.parse(textMatcher.group(1));
LocalTime startTime=LocalTime.parse(textMatcher.group(2));
LocalTime endTime=LocalTime.parse(textMatcher.group(3));
返回新插槽(天、开始时间、结束时间);
}否则{
抛出新的IllegalArgumentException(“不可解析插槽”+文本+”,预期格式为Sun 12:00-14:00);
}
}
星期一的最后一天;
私有最终本地时间开始时间;
私有最终本地时间结束时间;
时隙(星期几、本地时间开始时间、本地时间结束时间){
如果(!endTime.isAfter(startTime)){
抛出新的IllegalArgumentException(“结束时间必须在开始时间之后”);
}
this.day=Objects.requirennoull(day);
this.startTime=startTime;
this.endTime=endTime;
}
公众工作日(星期一){
回归日;
}
公共LocalTime getStartTime(){
返回起始时间;
}
公共LocalTime getEndTime(){
返回结束时间;
}
/**
*@param nextSlot
*@返回一个新插槽对象,表示此插槽和下一个插槽之间的空闲插槽,
*如果没有间隙,则为空可选
*/
公共可选插槽之间(插槽下一个插槽){
如果(!nextSlot.getDay().equals(day)){
抛出新的IllegalArgumentException(“无法比较一周中不同日期的插槽”);
}
if(nextSlot.getStartTime().isBefore(endTime)){
抛出新的IllegalArgumentException(“错误:插槽“+this+”和“+nextSlot”之间的重叠);
}
如果(nextSlot.getStartTime().isAfter(endTime)){//存在间隙
返回可选的.of(新插槽(day、endTime、nextSlot.getStartTime());
}否则{
返回可选的.empty();
}
}
@凌驾
公共字符串toString(){
返回dayFormatter.format(天)+''+startTime+'-'+endTime;
}
}
可能是一个开始的好地方。请不要教年轻人使用过时的日期和日历课程,这个答案使用了麻烦的旧类,这些旧类现在是遗留的,被java.time类取代。