如何确定特定时间周一至周五的日期 [英] How to determine a date in between Friday and Sunday of the week at a particular time

查看:227
本文介绍了如何确定特定时间周一至周五的日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在尝试检查一个当前的日期和时间是在星期五17:42和周日17:42之间的Java。我这样做真的很糟糕的代码块。这是一个匆忙的解决方案。现在我正在重构,但是我找不到任何方法在joda或其他。



任何想法?
谢谢

  private final Calendar currentDate = Calendar.getInstance(); 
private final int day = currentDate.get(Calendar.DAY_OF_WEEK);
private final int hour = currentDate.get(Calendar.HOUR_OF_DAY);
private final int minute = currentDate.get(Calendar.MINUTE);

if(day!= 1&&&&&&&&&&! == 1;
} else {
返回徽章==产品;
}
} else {
if(day == 6&&& hour> 16){
if(hour == 17& amp;&分< ){
if(combined!= 0){
return badge == 1;
} else {
返回徽章==产品;
}
} else {
return badge == 0;
}
} else if(day == 6&&< hour< 17){
if(combined!= 0){
return badge == 1;
} else {
返回徽章==产品;
}
} else if(day == 1&&& hour> 16){
if(hour == 17&&分<43){
返回徽章== 0;
} else {
if(combined!= 0){
return badge == 1;
} else {
返回徽章==产品;
}
}
} else {
return badge == 0;
}
}

我已经使用了这样的解决方案, @MadProgrammer和@Meno Hochschild



方法:

  public static boolean isBetween(LocalDateTime check,LocalDateTime startTime,LocalDateTime endTime){
return((check.equals(startTime)|| check.isAfter(startTime))&&(check.equals(endTime)|| check.isBefore (时间结束)));

使用方法:

  static LocalDateTime now = LocalDateTime.now(); 
static LocalDateTime friday = now.with(DayOfWeek.FRIDAY).toLocalDate()。atTime(17,41);
static LocalDateTime sunday = friday.plusDays(2).plusMinutes(1);

if(!isBetween(now,friday,sunday)){...}

再次感谢你的努力。

解决方案

日期 日历具有可以对其他实例进行比较的方法 Date / Calendar 等于,之前之后 p>

但是,我鼓励使用Java 8的新Time API。

  public static boolean isBetween(LocalDateTime check,LocalDateTime startTime,LocalDateTime endTime){
return((check.equals(startTime)|| check.isAfter(startTime))&&
(check.equals (endTime)|| check.isBefore(endTime)));
}

哪些将返回 true 如果提供的 LocalDateTime 包含在指定范围内。



像...一样...

  LocalDateTime start = LocalDateTime.now(); 
start = start.withDayOfMonth(26).withHour(17).withMinute(42).withSecond(0).withNano(0);
LocalDateTime end = start.plusDays(2);

LocalDateTime check = LocalDateTime.now();

System.out.println(check +in range =+ isBetween(check,start,end));
check = start;
System.out.println(check +within range =+ isBetween(check,start,end));
check = end;
System.out.println(check +within range =+ isBetween(check,start,end));
check = start.plusDays(1);
System.out.println(check +within range =+ isBetween(check,start,end));
check = end.plusMinutes(1);
System.out.println(check +within range =+ isBetween(check,start,end));

哪些输出

 code> 2015-06-25T18:31:32.969 is within range = false 
2015-06-26T17:42 is within range = true
2015-06-28T17:42 is within range = true
2015-06-27T17:42在range = true
2015-06-28T17:43在range = false

Joda-Time有一个间隔类,使其成为eaiser

  Interval targetInterval = new Interval(targetStart,targetEnd); 
System.out.println(Contains interval =+ interval.contains(targetInterval)

此处



一种不同的方法...



所以我正在回家的路上,假设你所有的是你要检查的日期/时间你可能会确定一天是否在你的范围内

  LocalDateTime now = LocalDateTime.now(); 
boolean isBetween = false;
switch(now.getDayOfWeek()){
case FRIDAY:
case SATURDAY:
case SUNDAY:
LocalDateTime lastFriday = getLastFriday(now);
LocalDateTime nextSunday = getNextSunday(now);
isBetween = isBetween(now,lastFriday,nextSunday);
System.out.println(lastFriday + - + nextSunday +:+ end);
break;
}

这是什么检查 dayOfWeek ,看看它是否在所需的范围内,如果是,它会发现以前的星期五和下一个星期日从指定的日期检查,看看它是否在它们之间(见上一个例子)



lastFriday nextSunday 只是从指定的日期/日期开始添加/减去时间直到达到所需的 dayOfWeek ,然后种子所需的时间约束

  public static LocalDateTime getLastFriday(LocalDateTime anchor){
LocalDateTime ldt = LocalDateTime.from(anchor);
return ldt.with(DayOfWeek.FRIDAY).withHour(17).withMinute(42).withSecond(0).withNano(0);
}

public static LocalDateTime getNextSunday(LocalDateTime anchor){
LocalDateTime ldt = LocalDateTime.from(anchor);
return ldt.with(DayOfWeek.SUNDAY).withHour(17).withMinute(42).withSecond(0).withNano(0);
}


I'm trying to check a current date and time is in between Friday 17:42 and Sunday 17:42 of the week with Java.

At the moment I'm doing this with really really bad code block. It was a hurry solution. Now I'm refactoring but I couldn't find any method in joda or etc.

Any ideas? Thanks

private final Calendar currentDate = Calendar.getInstance();
private final int day = currentDate.get(Calendar.DAY_OF_WEEK);
private final int hour = currentDate.get(Calendar.HOUR_OF_DAY);
private final int minute = currentDate.get(Calendar.MINUTE);

if (day != 1 && day != 6 && day != 7) {
    if (combined != 0) {
        return badge == 1;
    } else {
        return badge == product;
    }
} else {
    if (day == 6 && hour > 16) {
        if (hour == 17 && minute < 43) {
            if (combined != 0) {
                return badge == 1;
            } else {
                return badge == product;
            }
        } else {
            return badge == 0;
        }
    } else if (day == 6 && hour < 17) {
        if (combined != 0) {
            return badge == 1;
        } else {
            return badge == product;
        }
    } else if (day == 1 && hour > 16) {
        if (hour == 17 && minute < 43) {
            return badge == 0;
        } else {
            if (combined != 0) {
                return badge == 1;
            } else {
                return badge == product;
            }
        }
    } else {
        return badge == 0;
    }
}

I've used the solution like thiswith the help of @MadProgrammer and @Meno Hochschild

Method:

public static boolean isBetween(LocalDateTime check, LocalDateTime startTime, LocalDateTime endTime) {
 return ((check.equals(startTime) || check.isAfter(startTime)) && (check.equals(endTime) || check.isBefore(endTime))); }

Usage:

static LocalDateTime now = LocalDateTime.now();
static LocalDateTime friday = now.with(DayOfWeek.FRIDAY).toLocalDate().atTime(17, 41);
static LocalDateTime sunday = friday.plusDays(2).plusMinutes(1);

if (!isBetween(now, friday, sunday)) { ... }

Thanks again for your efforts.

解决方案

Date and Calendar have methods that can perform comparisons on other instances of Date/Calendar, equals, before and after

However, I'd encourage the use of Java 8's new Time API

public static boolean isBetween(LocalDateTime check, LocalDateTime startTime, LocalDateTime endTime) {
    return ((check.equals(startTime) || check.isAfter(startTime)) && 
                    (check.equals(endTime) || check.isBefore(endTime)));
}

Which will return true if the supplied LocalDateTime is within the specified range inclusively.

Something like...

LocalDateTime start = LocalDateTime.now();
start = start.withDayOfMonth(26).withHour(17).withMinute(42).withSecond(0).withNano(0);
LocalDateTime end = start.plusDays(2);

LocalDateTime check = LocalDateTime.now();

System.out.println(check + " is within range = " + isBetween(check, start, end));
check = start;
System.out.println(check + " is within range = " + isBetween(check, start, end));
check = end;
System.out.println(check + " is within range = " + isBetween(check, start, end));
check = start.plusDays(1);
System.out.println(check + " is within range = " + isBetween(check, start, end));
check = end.plusMinutes(1);
System.out.println(check + " is within range = " + isBetween(check, start, end));

Which outputs

2015-06-25T18:31:32.969 is within range = false
2015-06-26T17:42 is within range = true
2015-06-28T17:42 is within range = true
2015-06-27T17:42 is within range = true
2015-06-28T17:43 is within range = false

Joda-Time has an Interval class which makes it even eaiser

Interval targetInterval = new Interval(targetStart, targetEnd);
System.out.println("Contains interval = " + interval.contains(targetInterval)

which is demonstrated here

A different approach...

So I was thinking on way home, assuming all you have is the date/time you want to check, how you might determine if the day falls within your range

LocalDateTime now = LocalDateTime.now();
boolean isBetween = false;
switch (now.getDayOfWeek()) {
    case FRIDAY:
    case SATURDAY:
    case SUNDAY:
        LocalDateTime lastFriday = getLastFriday(now);
        LocalDateTime nextSunday = getNextSunday(now);
        isBetween = isBetween(now, lastFriday, nextSunday);
        System.out.println(lastFriday + " - " + nextSunday + ": " + end);
        break;
}

What this does is checks the dayOfWeek to see if it's within the desired range, if it is, it finds the previous Friday and next Sunday from the specified date and checks to see if it falls between them (see the previous example)

lastFriday and nextSunday simply adds/subtracts a day from the specified date/time until to reaches the desired dayOfWeek, it then seeds the required time constraints

public static LocalDateTime getLastFriday(LocalDateTime anchor) {
    LocalDateTime ldt = LocalDateTime.from(anchor);
    return ldt.with(DayOfWeek.FRIDAY).withHour(17).withMinute(42).withSecond(0).withNano(0);
}

public static LocalDateTime getNextSunday(LocalDateTime anchor) {
    LocalDateTime ldt = LocalDateTime.from(anchor);
    return ldt.with(DayOfWeek.SUNDAY).withHour(17).withMinute(42).withSecond(0).withNano(0);
}

这篇关于如何确定特定时间周一至周五的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆