• George (unregistered) in reply to BlueMoon
    BlueMoon:
    Hmm there is no checking if the input string has the correct length (10 characters) and if the YYYY, MM and DD characters are digit characters

    Using subString on a string that is to short is a ArrayIndexOutOfBoundsException

    Using parseInt on a non digit is a NumberFormattingException

    A well they probably do catch (Exception e) somewhere in the webserver

    Blue

  • George (unregistered) in reply to BlueMoon
    BlueMoon:
    Hmm there is no checking if the input string has the correct length (10 characters) and if the YYYY, MM and DD characters are digit characters

    Using subString on a string that is to short is a ArrayIndexOutOfBoundsException

    Using parseInt on a non digit is a NumberFormattingException

    A well they probably do catch (Exception e) somewhere in the webserver

    Blue

    Of course they do, it returns a FILE_NOT_FOUND

  • (cs) in reply to Licky Lindsay
    Licky Lindsay:
    Look at me! I'm on the internets!:
    In Java, January is 0.

    Java is a lot like Soviet Russia, isn't it?

    And yet, in Soviet Russia, exceptions throw you.

  • MoTH (unregistered) in reply to Look at me! I'm on the internets!

    That's why I use matlab.

  • iMalc (unregistered)

    Now for extra credit, you need to modify the function to be able to describe every possible date that could be supplied as a relative date, i.e. Today, Yesterday, Next Friday, Four weeks ago, One year, Three months, and seventeen days from now...

  • haha, nice (unregistered) in reply to Markp
    Markp:
    Hmmm...one line solution?
    return IDataUtil.getString( cursor, "date" ).replaceAll("[^0-9]", new String() ).equals( new SimpleDateFormat ("yyyyMMdd").format(  new GregorianCalendar().getTime() ) );
    

    All complete with less hazards than the previous solution.

    and by far the most readable solution out there. :P

  • Java.Net (unregistered)

    About the same as above, just readable :-p btw any could be a digit then you must parse the input

     
    private static String checkDate(String pDateString) {
            String ret ="NOT_TODAY";
            //must take substring because _any_ character could be a digit
            // if can't be a digit: pDateString = pDateString.replaceAll("[^0-9]", "");
            String year, day, month;
            year = pDateString.substring(0,4);
            month = pDateString.substring(5, 7);
            day = pDateString.substring(8);
            pDateString = year+month+day;
            
            Date today = Calendar.getInstance().getTime(); //now
            
            // Apply the right format to check the strings
            SimpleDateFormat sdf = new            SimpleDateFormat("yyyyMMdd");
            if (pDateString.equalsIgnoreCase(sdf.format(today)))
                ret =  "TODAY";
            
            return ret;
        }
    

    and a driver:

     
        public static void main(String[] args) {
            Scanner scan = new Scanner(System.in);
            String candidate = scan.nextLine();
            while (!candidate.equalsIgnoreCase("z")) {
                System.out.println(candidate + " ---> " + checkDate(candidate));
                candidate = scan.nextLine();
            }        
        }    
    }
    
  • Artemus Harper (unregistered)

    ok... here's my one-liner:

    return DateFormat.getDateInstance().parse(checkMe).getTime()/(1000606024) == System.currentTimeMillis()/(1000606024)? "TODAY" : "NOT_TODAY";

    This uses the fact that System.currentTimeMillis() returns milliseconds since January 1st 1970 (Midnight). Which tanks to rounding down with division I get the number of days since that date for the current time, and suggested time. The above code will work differently if you are using a non-US locale (which I could have explicitly set, but chances are you want the format to match the current locale).

    BTW, if you ever see the date December 31st 1969, chances are someone stored the time of that event as -1 and forgot to check for it.

  • DrNuts (unregistered) in reply to Ann Coulter
    Ann Coulter:
    This code will almost always return "NOT_TODAY" unless if statement is run at midnight exactly.

    No you're wrong, it will work just fine. Unfortunately, String comparison is probably the best way to do this with the base java API. There is JodaTime, which attempts to rectify the many problems with Java Date handling.

    You are right in thinking '?' is invalid. Has to be regexed out I guess.

    I'm still surprised people think code like this is OK. Maybe you just have worse standards where you are.

  • (cs) in reply to meissnersd
    meissnersd:
    There is a small race condition at midnight. If it is Tuesday with a millisecond to go to Wednesday, it may be Wednesday by the time the caller gets the result. Maybe thats okay. Shrug.

    But...provided the parse exceptions are handled by the callers, I would not call this a WTF.

    Welllll... that would be true of any method whose output depends on the value of the current date.

    It's just a fact of life we can't really guard against (or more to the point, not worth the added complexity to guard against)

    My favourite thing about this WTF is that any delimiters are allowed. So '2007W03T29F' will still be a valid date :-P

  • (cs) in reply to Licky Lindsay
    Licky Lindsay:
    You don't need to RTFS, just plain old fashioned RTFM. The javadocs for Calendar state that there is another month UNDECIMBER that comes after DECEMBER.
    Would the month after that be UNJANUERY, and then perhaps UNFEBYOOARY ? Beware the IDEs of UNMARCH.

    Anyway, the real WTF is this site's software. The topic URL is : Maybe_NOT_0x5f_TODAY

    That's right, space becomes underscore and underscore becomes "0x5f". I wonder what will happen if someone submites a story whose title includes "0x5f".

  • Joseph Newton (unregistered) in reply to whicker
    whicker:
    snoofle:
    Zonkers:
    This code is not that bad. I'm curious why one is being added to currentMonth though.
      int currentMonth = today.get( GregorianCalendar.MONTH ) + 1;
    In Java, GregorianCalendar.get(Calendar.MONTH) returns 0..11
    wtf?!

    Well it sort of, almost makes sense from the perspective of calculation, if your are calculating the day of the year, then days_to_add => array ( 0 => 0, 1 => 31, 2 => 59, 3 => 90, 4 => 120, 5 => 151, 6 => 181, 7 => 212, 8 => 243, 9 => 273, 10 => 304, 11 => 335);

    ...with some logic thrown in for handling leap year situations if (month > 1 and ( !(year % 4) && (year % 100) || !(year % 400) ) ), of course.

    I'm not sure that there really is any simple formulaic approach for getting the day of the year. It takes an array of some sort, unless you want to be really ugly and use a case statement or, worse yet, a twelve-step if-else [which would definitely be enough to drive one to drink and/or drugs].

  • Joseph Newton (unregistered) in reply to Thorin
    Thorin:
    T$:
    That's not so bad actually, at least it's easily readable.
    String checkMe = date.trim();

    Why is this here, didn't it already say must be in the format YYYY?MM?DD

    A user would never put in extra characters or anything.....

    Good point -- uh, maybe. It raise the question though. YTF are we asking the using to provide input in some goddamn geeky format?! That's what computers are for, ferfucksake!

    Give them three combo boxes, one for months, one for days and one for four-digit years, and then lets do our end of the job with the data they enter.

  • anonymous (unregistered)

    This piece of code is from WebMethods (an integration platform). Although it could have been written a bit nicer it is quite common.

    WebMethods doesn't know boolean etc. (it is java based, but uses a DOM like structure), everything is an object. To debug it is quite common to do most things as strings - hence the return value. It doesn't matter if you check for "true" or "today".

    And no - there aren't users who can input something else - it's only integration (well, in most cases)

  • Sch3lp (unregistered) in reply to chb
    chb:
    This IDataUtil and cursor variables worry me a bit.

    reminds me of fread(FH,...)

    IData is an object commonly used when making interfaces with SAP. (BusinessConnector) And it's really annoying to work with. Also, about the comments about the date being returned as a string: it gets returned as a string by the cursor of the IData object, blame that class. You could blame the programmer for not parsing to a date though. But imho this is not a real wtf either, ...or I'm missing it. :)

  • McWizard (unregistered) in reply to htg
    public boolean isToday(Date toCheck) { Calendar c1 = new GregorianCalendar(); c1.setTime(toCheck); Calendar c2 = new GregorianCalendar(); return ((c1.YEAR == c2.YEAR) && (c1.MONTH == c2.MONTH) && (c1.DAY_OF_MONTH == c2.DAY_OF_MONTH)); }

    Don't do that! Creating Gregorian Calendars is still very slow. Better reuse them if you can. :)

  • Kefer (unregistered)

    If you're going to use string-conversions any way, why not convert today's date to string and compare it to the input (ignoring the seperation characters of course)?

    Takes out all your number conversion error problems or issues with the time or whatever.

    Sorry to spoil the challenge...

  • onomatopoeia (unregistered)

    String date = (IDataUtil.getString(cursor, "date")).trim(); DateFormat format = new SimpleDateFormat("dd.MM.yyyy"); String today = format.format(new Date()); if (today.equals(date)) return "TODAY"; return "NOT_TODAY";

    wouldn't that make it easier?

  • onomatopoeia (unregistered)

    Didn't read the comment on top, just remove those characters then and just use "ddMMyyyy" as pattern for the SDF then.

  • Godi (unregistered)

    This is what i've found in a vendor-who's-name-shall-not-be-mentioned supplied PL/SQL program. Enjoy. I'm seriuosly NOT going to verify it actually works, i hope for god it does so i won't have to debug this.

    Oh, and offcourse there is also a toGmtDiff function.

    function fromgmtdiff(xDateTime DATE ,xTimezone char) return int as lDstStartMonthBegin DATE; lDstEndMonthBegin DATE; lDstStartMonthEnd DATE; lDstEndMonthEnd DATE; lDstStartDay INT; lLocStartDay INT; lDstStartWeek INT; lDstStartHour char(2); lDstStartMinute char(2); lDstStartSec char(2); lDstStartDate char(2); lLocStartDate char(2); lDstStartMonth char(2); lDstEndMonth char(2); lDstEndDay INT; lLocEndDay INT; lDstEndweek INT; lDstEndHour char(2); lDstEndMinute char(2); lDstEndSec char(2); lDstEndDate char(2); lLocEndDate char(2); lDSTOffset INT; dateOfFirstDayInstance INT; instanceNumber INT; dateInMonth INT; begin lDSTOffset:=0; /* 1753 is the earliest year supported by sql svr, dates beyond this cause date format errors */ if ((xDateTime is null)or(xTimezone is null)or(to_number(to_char(xDateTime ,'YYYY'))<1753)) then return 0; end if;

    begin /get dst start and end date (upto month last date resolution) for the year of the given datetime/ select to_date(decode (td.dst_start_month ,1 ,31 ,2 ,p1.febDays ,3 ,31 ,4 ,30 ,5 ,31 ,6 ,30 ,7 ,31 ,8 ,31 ,9 ,30 ,10 ,31 ,11 ,30 ,12 ,31)||'-'||lpad(td.dst_start_month ,2 ,'0')||'-'||p1.digitYear ,'DD-MM-YYYY') ,to_date(decode (td.dst_end_month ,1 ,31 ,2 ,p1.febDays ,3 ,31 ,4 ,30 ,5 ,31 ,6 ,30 ,7 ,31 ,8 ,31 ,9 ,30 ,10 ,31 ,11 ,30 ,12 ,31)||'-'||lpad(td.dst_end_month ,2 ,'0')||'-'||p1.digitYear ,'DD-MM-YYYY') ,to_date(1||'-'||lpad(td.dst_start_month ,2 ,'0')||'-'||p1.digitYear ,'DD-MM-YYYY') ,to_date(1||'-'||lpad(td.dst_end_month ,2 ,'0')||'-'||p1.digitYear ,'DD-MM-YYYY') ,td.dst_start_month ,td.dst_start_date ,td.loc_start_date ,td.dst_start_week ,td.dst_start_day ,td.loc_start_day ,td.dst_start_hr ,td.dst_start_min ,td.dst_start_sec ,td.dst_end_month ,td.dst_end_date ,td.loc_end_date ,td.dst_end_week ,td.dst_end_day ,td.loc_end_day ,td.dst_end_hr ,td.dst_end_min ,td.dst_end_sec ,td.timediff into lDstStartMonthEnd ,lDstEndMonthEnd ,lDstStartMonthBegin ,lDstEndMonthBegin ,lDstStartMonth ,lDstStartDate ,lLocStartDate ,lDstStartWeek ,lDstStartDay ,lLocStartDay ,lDstStartHour ,lDstStartMinute ,lDstStartSec ,lDstEndMonth ,lDstEndDate ,lLocEndDate ,lDstEndWeek ,lDstEndDay ,lLocEndDay ,lDstEndHour ,lDstEndMinute ,lDstEndSec ,lDSTOffset from fgt_timezone_dst td ,(select decode(mod(to_number(p.digitYear) ,400) ,0 ,29 ,decode(mod(to_number(p.digitYear) ,100) ,0 ,28 ,decode(mod(to_number(p.digitYear) ,4) ,0 ,29 ,28)))febdays ,p.digitYear digitYear from (select to_char(xDateTime ,'YYYY')digitYear from dual)p)p1 where td.timezone_id=xTimezone; exception when no_data_found then return 0; end;

    /create the start and end dates for the DST range/ if (lDstStartDate is not null and lDstEndDate is not null) then /* the local time with timediff results in GMT time being on the earlier day */ if (lLocStartDate<>lDstStartDate) then if (lLocStartDate=1) then /if local date is first of month then GMT date will be on last date of previous month/ lDstStartMonthBegin:=to_date(to_char(lDstStartMonthBegin-1 ,'DD-MM-YYYY')||' '||lDstStartHour||':'||lDstStartMinute||':'||lDstStartSec ,'DD-MM-YYYY HH24:MI:SS'); else /GMT date will be date of previous to local date in same month/ lDstStartMonthBegin:=to_date((lLocStartDate-1)||'-'||to_char(lDstStartMonthBegin ,'MM-YYYY')||' '||lDstStartHour||':'||lDstStartMinute||':'||lDstStartSec ,'DD-MM-YYYY HH24:MI:SS'); end if;

      if (lLocEndDate=1)
      then
        /*if local date is first of month then GMT date will be on last date of previous month*/
        lDstEndMonthBegin:=to_date(to_char(lDstEndMonthBegin-1
                                          ,'DD-MM-YYYY')||' '||lDstEndHour||':'||lDstEndMinute||':'||lDstEndSec
                                  ,'DD-MM-YYYY HH24:MI:SS');
      else
        /*GMT date will be date of previous to local date in same month*/
        lDstEndMonthBegin:=to_date((lLocEndDate-1)||'-'||to_char(lDstEndMonthBegin
                                                                ,'MM-YYYY')||' '||lDstEndHour||':'||lDstEndMinute||':'||lDstEndSec
                                  ,'DD-MM-YYYY HH24:MI:SS');
      end if;
    
    else
      /*get the start date and time*/
      lDstStartMonthBegin:=to_date(lDstStartDate||'-'||to_char(lDstStartMonthBegin
                                                              ,'MM-YYYY')||' '||lDstStartHour||':'||lDstStartMinute||':'||lDstStartSec
                                  ,'DD-MM-YYYY HH24:MI:SS');
      /*get the End date and time*/
      lDstEndMonthBegin:=to_date(lDstEndDate||'-'||to_char(lDstEndMonthBegin
                                                          ,'MM-YYYY')||' '||lDstEndHour||':'||lDstEndMinute||':'||lDstEndSec
                                ,'DD-MM-YYYY HH24:MI:SS');
    end if;
    

    else /* Calcluate the start date */ instanceNumber:=lDstStartWeek; if (to_char(lDstStartMonthBegin ,'D')>lLocStartDay) then dateOfFirstDayInstance:=abs(to_char(lDstStartMonthBegin ,'D')-(7+lLocStartDay))+1; elsif(to_char(lDstStartMonthBegin ,'D')<=lLocStartDay) then dateOfFirstDayInstance:=abs(to_char(lDstStartMonthBegin ,'D')-lLocStartDay)+1; end if;

    if (to_char(lDstStartMonthEnd
               ,'W')<>5)
    then
      if (lDstStartWeek=5)
      then
        instanceNumber:=4;
      end if;
    
    elsif(to_char(lDstStartMonthEnd
                 ,'W')=5)
    then
      if (lDstStartWeek=5)
      then
        /* if date of first day instance is after first week then decrement instance number by one 				 note oracle calculates week numbers every 7 days from the start day of the month not 				 using week starting from sunday 				*/
        /* if the last week of month does not span across a sunday */
        if (to_char(lDstStartMonthBegin
                   ,'D')-to_char(lDstStartMonthEnd
                                ,'D')<0)
        then
          /* check if the start day is in the last week*/
          if (to_char(lDstStartMonthBegin
                     ,'D')<=lLocStartDay and lLocStartDay<=to_char(lDstStartMonthEnd
                                                                  ,'D'))
          then
            instanceNumber:=5;
          else
            instanceNumber:=4;
          end if;
    
        else
          /* if the last week of month spans across a sunday */
          /* check if the start day is in the last week*/
          if ((to_char(lDstStartMonthBegin
                      ,'D')<=lLocStartDay and lLocStartDay<=7)or(1<=lLocStartDay and lLocStartDay<=to_char(lDstStartMonthEnd
                                                                                                          ,'D')))
          then
            instanceNumber:=5;
          else
            instanceNumber:=4;
          end if;
    
        end if;
    
      end if;
    
    end if;
    
    dateInMonth:=((instanceNumber-1)*7)+dateOfFirstDayInstance;
    /* the local time with timediff results in GMT time being on the earlier day */
    if (lDstStartDay<>lLocStartDay)
    then
      if (dateInMonth=1)
      then
        /*if local date is first of month then GMT date will be on last date of previous month*/
        lDstStartMonthBegin:=to_date(to_char(lDstStartMonthBegin-1
                                            ,'DD-MM-YYYY')||' '||lDstStartHour||':'||lDstStartMinute||':'||lDstStartSec
                                    ,'DD-MM-YYYY HH24:MI:SS');
      else
        /*GMT date will be date of previous to local date in same month*/
        lDstStartMonthBegin:=to_date((dateInMonth-1)||'-'||to_char(lDstStartMonthBegin
                                                                  ,'MM-YYYY')||' '||lDstStartHour||':'||lDstStartMinute||':'||lDstStartSec
                                    ,'DD-MM-YYYY HH24:MI:SS');
      end if;
    
    else
      /*get the start date and time*/
      lDstStartMonthBegin:=to_date(dateInMonth||'-'||to_char(lDstStartMonthBegin
                                                            ,'MM-YYYY')||' '||lDstStartHour||':'||lDstStartMinute||':'||lDstStartSec
                                  ,'DD-MM-YYYY HH24:MI:SS');
    end if;
    
    /* Calculate the end date */
    instanceNumber:=lDstEndWeek;
    if (to_char(lDstEndMonthBegin
               ,'D')>lLocEndDay)
    then
      dateOfFirstDayInstance:=abs(to_char(lDstEndMonthBegin
                                         ,'D')-(7+lLocEndDay))+1;
    elsif(to_char(lDstEndMonthBegin
                 ,'D')<=lLocEndDay)
    then
      dateOfFirstDayInstance:=abs(to_char(lDstEndMonthBegin
                                         ,'D')-lLocEndDay)+1;
    end if;
    
    if (to_char(lDstEndMonthEnd
               ,'W')<>5)
    then
      if (lDstEndWeek=5)
      then
        instanceNumber:=4;
      end if;
    
    elsif(to_char(lDstEndMonthEnd
                 ,'W')=5)
    then
      if (lDstEndWeek=5)
      then
        /* if date of first day instance is after first week then decrement instance number by one 				 note oracle calculates week numbers every 7 days from the start day of the month not 				 using week starting from sunday 				*/
        /* if the last week of month does not span across a sunday */
        if (to_char(lDstEndMonthBegin
                   ,'D')-to_char(lDstEndMonthEnd
                                ,'D')<0)
        then
          /* check if the start day is in the last week*/
          if (to_char(lDstEndMonthBegin
                     ,'D')<=lLocEndDay and lLocEndDay<=to_char(lDstEndMonthEnd
                                                              ,'D'))
          then
            instanceNumber:=5;
          else
            instanceNumber:=4;
          end if;
    
        else
          /* if the last week of month spans across a sunday */
          /* check if the start day is in the last week*/
          if ((to_char(lDstEndMonthBegin
                      ,'D')<=lLocEndDay and lLocEndDay<=7)or(1<=lLocEndDay and lLocEndDay<=to_char(lDstEndMonthEnd
                                                                                                  ,'D')))
          then
            instanceNumber:=5;
          else
            instanceNumber:=4;
          end if;
    
        end if;
    
      end if;
    
    end if;
    
    dateInMonth:=((instanceNumber-1)*7)+dateOfFirstDayInstance;
    /* the local time with timediff results in GMT time being on the earlier day */
    if (lDstEndDay<>lLocEndDay)
    then
      if (dateInMonth=1)
      then
        /*if local date is first of month then GMT date will be on last date of previous month*/
        lDstEndMonthBegin:=to_date(to_char(lDstEndMonthBegin-1
                                          ,'DD-MM-YYYY')||' '||lDstEndHour||':'||lDstEndMinute||':'||lDstEndSec
                                  ,'DD-MM-YYYY HH24:MI:SS');
      else
        /*GMT date will be date of previous to local date in same month*/
        lDstEndMonthBegin:=to_date((dateInMonth-1)||'-'||to_char(lDstEndMonthBegin
                                                                ,'MM-YYYY')||' '||lDstEndHour||':'||lDstEndMinute||':'||lDstEndSec
                                  ,'DD-MM-YYYY HH24:MI:SS');
      end if;
    
    else
      /*get the End date and time*/
      lDstEndMonthBegin:=to_date(dateInMonth||'-'||to_char(lDstEndMonthBegin
                                                          ,'MM-YYYY')||' '||lDstEndHour||':'||lDstEndMinute||':'||lDstEndSec
                                ,'DD-MM-YYYY HH24:MI:SS');
    end if;
    

    end if;

    /check if date is in range and set the offset value/ if (lDstStartMonth-lDstEndMonth<0) then /if the range is in the same year/ if ((xDateTime>=lDstStartMonthBegin)and(xDateTime<=lDstEndMonthBegin)) then return lDSTOffset; else return 0; end if;

    elsif(lDstStartMonth-lDstEndMonth>0) then /if the range is a year crossover/ if ((xDateTime>=lDstStartMonthBegin)or(xDateTime<=lDstEndMonthBegin)) then return lDSTOffset; else return 0; end if;

    end if;

    return lDSTOffset; end FromGMTDiff;

  • Kefer (unregistered) in reply to Godi

    OMG...

    Captcha: riaa Indeed, that was about the second thing that came to mind

  • Asd (unregistered)

    God bless Sun and IBM for Date and Calendar! Never fix APIs!!!!

  • newcron (unregistered)

    In my opinion this is not so terribly wrong. When you compare two GregorianCalendar instances in Java (I suppose this being java code), not only the date, but also the time is compared. If there is no Time set explicitely, the system's current time is taken.

    So if you concider the "reference date" beging created at program start (and updated, if date changes), you can assume that it will never be equal to the current date, when the new date is instanciated later.

  • Anon_Coder (unregistered)
    import org.apache.commons.lang.time.DateUtils;
    ...       
        public static final String TODAY = "TODAY";
        public static final String NOT_TODAY = "NOT_TODAY";    
        /**
         * @deprecated maintained only for backwards compatibility.
         * @see {@link #isToday(Date)}
         * @see {@link #formatDate(String)}
         */
        public String isToday(String date) {
            return isToday(formatDate(date)) ? TODAY : NOT_TODAY;
        }
        public boolean isToday(Date date) {
             return DateUtils.isSameDay(new Date(), date);        
        }    
        public Date formatDate(String date) throws NotAValidDateException {
            try {                        
                return new SimpleDateFormat("yyyy?MM?dd").parse(date);
            } catch (ParseException e) {
                throw new NotAValidDateException(date+" is not valid, must be in format yyyy?MM?dd", e);
            }
        }    
    ...
    
  • martin (unregistered) in reply to htg
    return ((c1.YEAR == c2.YEAR) && (c1.MONTH == c2.MONTH) && (c1.DAY_OF_MONTH == c2.DAY_OF_MONTH));

    Read the API ffs ! These are constants used for rolling the date up and down, not the values.

    If it aint broken, fix it until it is, literally.

  • Anonymous Tart (unregistered)
    public static bool isToday(String date)
    {
        String[] cmd = { "/usr/local/bin/ruby", "-rdate", "-e exit Date.today==Date.parse('" + date + "')" };
        Process p = Runtime.getRuntime().exec(cmd);
        return (p.exitValue() == 0);
    }
    
    
  • Anita Tinkle (unregistered) in reply to Anonymous Tart
    Anonymous Tart:
    public static bool isToday(String date)
    {
        String[] cmd = { "/usr/local/bin/ruby", "-rdate", "-e exit Date.today==Date.parse('" + date + "')" };
        Process p = Runtime.getRuntime().exec(cmd);
        return (p.exitValue() == 0);
    }
    

    That is the funniest response EV-ER. :D

  • Reverie (unregistered) in reply to Blame
    Blame:
    It seems inefficient to extract the year, extract the month and extract the day, then get the current year, get the current month and get the current day and then compare them.

    Depending on the application, it's probably better to extract the year, get the current year and compare them and return if they're different without looking at the month and day.

    If it's more likely that the day will vary, then check that first.

    Actually, I think you've got the race condition backwards. After all, the date IS equal to TODAY at the time the function returns, which may be more interesting. However, if the current time is grabbed too early then it will moments later say that it's NOT_TODAY, even though it is.

    Regardless, it's unlikely to hurt anything.

  • (cs) in reply to rufio
    rufio:
    snoofle:
    In Java, GregorianCalendar.get(Calendar.MONTH) returns 0..11
    well, it can actually return 12. You knew that if you read some source files
    Lousy Smarch weather...
  • Cognac (unregistered)

    Today or not today, that is the question...

  • Not in the mood (unregistered) in reply to Licky Lindsay
    Licky Lindsay:
    rufio:
    well, it can actually return 12. You knew that if you read some source files

    You don't need to RTFS, just plain old fashioned RTFM. The javadocs for Calendar state that there is another month UNDECIMBER that comes after DECEMBER.

    http://java.sun.com/j2se/1.5.0/docs/api/java/util/Calendar.html#UNDECIMBER

    I think it's missin 13 as MONTH_NOT_FOUND

  • Godi (unregistered)

    I allways get my 13th month in May, but that's another story

  • Tinkerghost (unregistered) in reply to Zonkers
    Zonkers:
    This code is not that bad. I'm curious why one is being added to currentMonth though.
      int currentMonth = today.get( GregorianCalendar.MONTH ) + 1;
    Some languages date the month as 0-11 instead of 1-12.
  • return "THIS_CODE_SUCKS" (unregistered) in reply to CynicalTyler
    CynicalTyler:
    This function is incomplete: other acceptable return values are MAYBE_NOT_TOMORROW and BUT_SOME_DAY.
    And FILE_NOT_FOUND.
  • Derekwaisy (unregistered)
    Comment held for moderation.
  • Jimmyanten (unregistered)

    prednisone for dogs: http://prednisone1st.store/# prednisone 30 mg

Leave a comment on “Maybe NOT_TODAY”

Log In or post as a guest

Replying to comment #:

« Return to Article