2

I am looking for a way to get only week dates from local date(JODA TIME) API? I am trying write the code in this format..

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();

If possible please provide the code? Thank you.

vijay
  • 1,129
  • 7
  • 22
  • 34

3 Answers3

3

You can do the following

1) Add the number of days to getDate to get a newDate

2) compare it with today's date

3) if true then you can use getDayOfWeek() method as specified by @joe and then check if it is between 1 & 5 inclusive and call the send mail method.

Pseudocode:

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
Lakshmi
  • 2,204
  • 3
  • 29
  • 49
  • i forgotten to ask, getDate.plusDays(int days) should only count the week days? – vijay Mar 11 '13 at 12:09
  • From JavaDocs : Returns a copy of this date plus the specified number of days. This LocalDate instance is immutable and unaffected by this method call.Which means that it includes everyday.Here days will be the number of days in your case it is the 'ReminderDays' . – Lakshmi Mar 11 '13 at 12:13
  • Thanks for ur answer, is there any possibility to exclude everyday count and count only if it is a week day? – vijay Mar 11 '13 at 12:33
  • @vijay u want to add some x number of days to your getdate and check if it equal to today right ? for that the plusDays method is enough or do you expect something else? – Lakshmi Mar 11 '13 at 12:38
  • I got the same problem, http://stackoverflow.com/questions/12728527/joda-time-add-weekdays-to-date – vijay Mar 11 '13 at 13:01
  • 1
    @vijay as per that thread adding only weekdays could be troublesome what you could do this add the remaining days to getDate and come up with a date if that day is today and today is weekday u sendmail if that day is today but not a weekend find the next possible day that can be weekday and schedule the send mail on that day according to your logic..I think this method should be checked everyday so that prior mails can be send if the day will be a weekend.... – Lakshmi Mar 11 '13 at 16:36
  • @vijay your welcome accept that answer if it was helpful to you. – Lakshmi Mar 12 '13 at 09:40
2

There is a method called getDayOfWeek() which will return the values as follows

    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;
Joe2013
  • 1,007
  • 1
  • 9
  • 24
0

If you prefer to use the constants rather than integer values.

Joda uses values from DateTimeConstants. These are the ISO8601 constants,

Source code is here:

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

Usage could be something like:

// Joda uses MONDAY=1 to SUNDAY=7
if (startDate.getDayOfWeek() < DateTimeConstants.SATURDAY) {
    doMyWeekdayThang();
}
chim
  • 8,407
  • 3
  • 52
  • 60