【问题标题】:Local Date get only week dates Java [duplicate]本地日期仅获取周日期Java [重复]
【发布时间】:2017-01-05 19:35:03
【问题描述】:

我正在寻找一种仅从本地日期(JODA TIME)API 获取星期日期的方法?我正在尝试以这种格式编写代码..

LocalDate today = new LocalDate();
LocalDate getDate = new LocalDate(sqlDate);// i will recieve sqldate from sql table

//if the getdate and number of days to remind is equals to today 
//and if it is between monday to friday ,send an email. 
//ReminderDays is an integer
if(getDate+ReminderDays == today)
SendMail();

如果可能,请提供代码?谢谢。

【问题讨论】:

    标签: java date jodatime


    【解决方案1】:

    您可以执行以下操作

    1) 将天数添加到 getDate 以获取 newDate

    2) 与今天的日期比较

    3) 如果为 true,那么您可以使用 @joe 指定的 getDayOfWeek() 方法,然后检查它是否在 1 和 5 之间,并调用发送邮件方法。

    伪代码:

    LocalDate getDate = new LocalDate(sqlDate);
    LocalDate newDate =getDate.plusDays(int days) ;
    if(compare newDate & todays date)
    //if true then do
    if(1<=newDate.getDayOfWeek()<=5)
    //call send mail
    

    【讨论】:

    • 我忘了问,getDate.plusDays(int days) 应该只计算工作日吗?
    • 来自 JavaDocs :返回此日期的副本加上指定的天数。此 LocalDate 实例是不可变的,不受此方法调用的影响。这意味着它包括每天。在您的情况下,这里的天数是“ReminderDays”。
    • 感谢您的回答,是否有可能排除每日计数并仅在工作日计数?
    • @vijay 你想在你的 getdate 中添加一些 x 天数并检查它是否等于今天,对吗?因为 plusDays 方法就足够了,还是你期待别的?
    • @vijay 根据该线程仅添加工作日可能会很麻烦你可以这样做添加剩余的天数到 getDate 并想出一个日期,如果那一天是今天,今天是工作日,如果那是你发送邮件今天是今天但不是周末找到下一个可能的工作日,根据您的逻辑安排在当天发送邮件。一个周末……
    【解决方案2】:

    有一个名为 getDayOfWeek() 的方法将返回如下值

        public static final int MONDAY = 1;
        public static final int TUESDAY = 2;
        public static final int WEDNESDAY = 3;
        public static final int THURSDAY = 4;
        public static final int FRIDAY = 5;
        public static final int SATURDAY = 6;
        public static final int SUNDAY = 7;
    

    【讨论】:

      【解决方案3】:

      如果您更喜欢使用常量而不是整数值。

      Joda 使用来自 DateTimeConstants 的值。这些是 ISO8601 常量,

      源码在这里:

      http://joda-time.sourceforge.net/apidocs/src-html/org/joda/time/DateTimeConstants.html#line.70

      用法可能是这样的:

      // Joda uses MONDAY=1 to SUNDAY=7
      if (startDate.getDayOfWeek() < DateTimeConstants.SATURDAY) {
          doMyWeekdayThang();
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-11-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-12-16
        • 2018-01-15
        相关资源
        最近更新 更多