检查当前时间是否在Java中的给定时间和日期名称之间

时间:2018-11-16 09:34:08

标签: java

情况如下。

您必须检查当前时间是否在给定的开始日期+时间和结束日期+时间之间。没有指定具体的日期范围,该范围可以在两个不同的日期。

示例:

String startDayAndTime = "SATURDAY 17:00";
String endDayAndTime = "SUNDAY 17:00";

SimpleDateFormat simpleDateFormatForTime = new SimpleDateFormat("HH:mm");
SimpleDateFormat simpleDateFormatForDay = new SimpleDateFormat("EEEE");
simpleDateFormatForTime.setTimeZone(timeZone);
simpleDateFormatForDay.setTimeZone(timeZone);

String deviceTimeString = simpleDateFormatForTime.format(date);

String deviceDayString = simpleDateFormatForDay.format(date).toUpperCase();

// TODO boolean isValidDayRange =
boolean withInRange = deviceTimeString.compareTo(startDayAndTime.split(" ")[1]) >= 0 && deviceTimeString.compareTo(endDayAndTime.split(" ")[1]) <= 0;
if (isValidDayRange && withInRange) {
      //do something
}

我正在处理的上述代码可以检查时间,但不能检查日期。 如何实现呢? TIA。

PS:真实世界的示例:一家商店在每个周末的星期六(星期六)下午5点至星期日的下午5点之间提供产品折扣。尝试检查当前时间符合条件。

需求更新:必须在Java 1.7或更低版​​本中

6 个答案:

答案 0 :(得分:1)

您可以按以下方式访问日期

    LocalDateTime today = LocalDateTime.now();

    LocalDate ld = LocalDate.now();
    ld = ld.with(TemporalAdjusters.previous(DayOfWeek.SATURDAY));
    LocalDateTime previousMonday = ld.atTime(17, 00);

    LocalDate ld2 = LocalDate.now();
    ld2 = ld2.with(TemporalAdjusters.next(DayOfWeek.SUNDAY));
    LocalDateTime nextSunday = ld2.atTime(17, 00);

您可以在此之后编写逻辑。

如上所述,您可以找到上一个星期一和下一个星期日。

如果上周一和下周日的时间间隔少于一周,则您处于间隔状态。否则你就进不了。

答案 1 :(得分:1)

java.time中没有任何东西可以表示日期和时间(例如MonthDay),但是我们可以很容易地定义自己的日期和时间。我选择了DayOfWeekDuration。确保您确认时间是非负数且少于24小时。

请注意,DayOfWeek具有星期一(最低)到星期日(最高)的自然顺序。

class DayTime implements Comparable<DayTime>
{
    private final DayOfWeek dayOfWeek;
    private final Duration time;

    //...

    public int compareTo(final DayTime o) { /* ... */ }
}

然后我们可以定义一个指定开始和结束时间的事件。确保确认起点不在终点之后。

class Event
{
    private final DayTime start;
    private final DayTime end;

    //...

    boolean isTimeDuringEvent(final DayTime dayTime) { /* ... */ }
}

我将实施细节留给您。这是你的任务。

我们可以像这样使用这些类:

final DayTime start = new DayTime(DayOfWeek.SATURDAY, Duration.ofHours(17));
final DayTime end = new DayTime(DayOfWeek.SUNDAY, Duration.ofHours(17));
final Event event = new Event(start, end);

final LocalDateTime now = LocalDateTime.now();
final DayTime test = new DayTime(
    now.getDayOfWeek(),
    Duration.ofNanos(now.toLocalTime().toNanoOfDay())
);
System.out.println(event.isTimeDuringEvent(test));

如果您需要将用户输入作为字符串输入,那么我建议您首先使逻辑正确(如上所述,使用诸如DayOfWeek.SATURDAY这样的硬编码值),然后再 当您确定可行时,请解析输入。 (您可能需要DateTimeFormatter

答案 2 :(得分:1)

使用TemporalAccessor

您可以使用TemporalAccessors和DateTimeFormatter读取值,然后构建简单的比较器来检查工作。

import pandas as pd
df = pd.DataFrame([obj.__dict__ for obj in objectlist]).set_index(['attr1', 'attr2'])

答案 3 :(得分:0)

您可以在此处使用日历

 Calendar startDateTime=Calendar.getInstance();
    startDateTime.set(Calendar.DAY_OF_WEEK,Calendar.SUNDAY);
    startDateTime.set(Calendar.HOUR_OF_DAY,1);
    startDateTime.set(Calendar.MINUTE,0);
    startDateTime.set(Calendar.SECOND,0);

    System.out.println("start Date : "+startDateTime.getTime());
    Calendar endDateTime=Calendar.getInstance();
    endDateTime.set(Calendar.DAY_OF_WEEK,Calendar.FRIDAY);
    endDateTime.set(Calendar.HOUR_OF_DAY,17);
    endDateTime.set(Calendar.MINUTE,0);
    endDateTime.set(Calendar.SECOND,0);

    System.out.println("end Date : "+endDateTime.getTime());

   Calendar today = Calendar.getInstance();

   if(today.after(startDateTime) && today.before(endDateTime))
   {
       System.out.println("Yes");
   }

但是在这里,您需要保持日期顺序,因为日历WEEKS以SUNDAY开始,以SATURDAY结尾。因此,您必须遵循此命令。开始日期较小,结束日期较大。

订单是这样的

/**
 * Value of the {@link #DAY_OF_WEEK} field indicating
 * Sunday.
 */
public final static int SUNDAY = 1;

/**
 * Value of the {@link #DAY_OF_WEEK} field indicating
 * Monday.
 */
public final static int MONDAY = 2;

/**
 * Value of the {@link #DAY_OF_WEEK} field indicating
 * Tuesday.
 */
public final static int TUESDAY = 3;

/**
 * Value of the {@link #DAY_OF_WEEK} field indicating
 * Wednesday.
 */
public final static int WEDNESDAY = 4;

/**
 * Value of the {@link #DAY_OF_WEEK} field indicating
 * Thursday.
 */
public final static int THURSDAY = 5;

/**
 * Value of the {@link #DAY_OF_WEEK} field indicating
 * Friday.
 */
public final static int FRIDAY = 6;

/**
 * Value of the {@link #DAY_OF_WEEK} field indicating
 * Saturday.
 */
public final static int SATURDAY = 7;

但是我认为这足以满足您的要求。

答案 4 :(得分:0)

感谢您的回答和对话。我通过提供的TemporalAccessorCalendar选项设法解决了这个问题。后来对我正在使用的util类提出了新要求,因为它必须是不支持Java 1.8的android应用程序的库,所以必须使用Java 1.7或更低版​​本编写。因此,@ flopcoder的答案被接受,其理由是将星期日视为一周的开始,并且该逻辑仅适用于一周之内的范围。

答案 5 :(得分:-1)

您已经编写了仅检查日期和时间的代码,未编写代码来检查日期,请使用simpleDateFormat并发送系统当前日期进行检查和比较,您正在传递“ HH:mm”,这将为您提供系统当前时间和“ EEEE”会给您一天的时间,所以您只需要传递“ dd”即可获取当前系统日期,然后根据您的要求进行比较

相关问题