Devan L. was playing with his young daughter, when she asked him if he knew all of the days of the week. He replied: Sunday, Monday, Frogday, Flubberday, ...

She quickly admonished him for being silly. He told her that he was a very busy man and that there were too many days to remember. They laughed.

Then he went to work and encountered the date utility library code below.

Now he wants to cry.

    //This doesn't roll across month boundaries correctly
    //public Date getTuesdayInWeek(Date in) {
    //    GregorianCalendar gc = new GregorianCalendar();
    //    gc.setTime(in);
    //    gc.add(Calendar.DATE,  Calendar.TUESDAY - gc.get(Calendar.DAY_OF_WEEK));
    //    return gc.getTime();
    //}

    public Date getSundayInWeek(Date in) {
        GregorianCalendar gc = new GregorianCalendar();
        gc.setTime(in);
        return new Date(in.getTime() + (Calendar.SUNDAY - gc.get(Calendar.DAY_OF_WEEK))*24*3600*1000);
    }

    public Date getMondayInWeek(Date in) {
        GregorianCalendar gc = new GregorianCalendar();
        gc.setTime(in);
        return new Date(in.getTime() + (Calendar.MONDAY - gc.get(Calendar.DAY_OF_WEEK))*24*3600*1000);
    }

    // Not sure if this will work for leap years
    public Date getTuesdayInWeek(Date in) {
        GregorianCalendar gc = new GregorianCalendar();
        gc.setTime(in);
        return new Date(in.getTime() + (Calendar.TUESDAY - gc.get(Calendar.DAY_OF_WEEK))*24*3600*1000);
    }

    public Date getWednesdayInWeek(Date in) {
        GregorianCalendar gc = new GregorianCalendar();
        gc.setTime(in);
        return new Date(in.getTime() + (Calendar.WEDNESDAY - gc.get(Calendar.DAY_OF_WEEK))*24*3600*1000);
    }

    public Date getThursdayInWeek(Date in) {
        GregorianCalendar gc = new GregorianCalendar();
        gc.setTime(in);
        return new Date(in.getTime() + (Calendar.THURSDAY - gc.get(Calendar.DAY_OF_WEEK))*24*3600*1000);
    }

    public Date getFridayInWeek(Date in) {
        GregorianCalendar gc = new GregorianCalendar();
        gc.setTime(in);
        return new Date(in.getTime() + (Calendar.FRIDAY - gc.get(Calendar.DAY_OF_WEEK))*24*3600*1000);
    }

    public Date getSaturdayInWeek(Date in) {
        GregorianCalendar gc = new GregorianCalendar();
        gc.setTime(in);
        return new Date(in.getTime() + (Calendar.SATURDAY - gc.get(Calendar.DAY_OF_WEEK))*24*3600*1000);
    }

    public Date getDateInWeek(Date in, int desiredDay) {
        switch (desiredDay) {
            case Calendar.SUNDAY:    return getSundayInWeek(in);
            case Calendar.MONDAY:    return getMondayInWeek(in);
            case Calendar.TUESDAY:   return getTuesdayInWeek(in);
            case Calendar.WEDNESDAY: return getWednesdayInWeek(in);
            case Calendar.THURSDAY:  return getThursdayInWeek(in);
            case Calendar.FRIDAY:    return getFridayInWeek(in);
            case Calendar.SATURDAY:  return getSaturdayInWeek(in);
            default: throw new IllegalArgumentException("unknown desired day: "+desiredDay);
        }
    }

    public Date getReleaseDateInWeek(Date in) {
        return getDateInWeek(in, Calendar.TUESDAY);
    }
[Advertisement] BuildMaster allows you to create a self-service release management platform that allows different teams to manage their applications. Explore how!