• Simon (unregistered)

    Yeah, I'm just going to blame Java date APIs for this one. I mean, yes, it's pretty clear that this is newbie code... but I'm not going to blame some newly graduated developer for not knowing the most efficient ways of abusing the java.util.Calendar class. There's no right way of using that sorry excuse for an API... the best you can hope for is something that isn't too ugly a hack.

  • Bert (unregistered)

    Surely this beginner should know better! WTF, right guys?

  • (nodebb)

    How do you actually get a java Date back from a java Calendar?

  • gandy (unregistered)

    Now, if you are using Java 8's new Time library, it is way better to just use TemporalAdjusters: https://docs.oracle.com/javase/8/docs/api/java/time/temporal/TemporalAdjusters.html#previousOrSame-java.time.DayOfWeek-

  • ammoQ (unregistered)

    Medinoc: calendar.getTime() returns a Date. WTF, but that's how it is.

  • ammoQ (unregistered)

    This code is not as good as it could be, but IMHO not TDWTF-Frontpage worthy. BTW, there would have been better ways to accomplish that, but the SimpleDateFormat-detour also servers to get rid of the time component of the result, which might or might not be a desired effect.

  • Jester (unregistered)

    If you send in a date that happens to be a SaturDay it returns the same date, not the previous SaturDay...

  • t0pC0der (unregistered)

    Until java can figure out a native date time library that isn't hot garbage, this isn't the real WTF. The real WTF is that we're up to Java 8 and still have these issues

  • (nodebb)

    The method name seems pretty clear. What would you add to the comment? How important is a redundant comment when the method name is clear?

  • MG (unregistered)

    This doesn't look like WTF code for me. Until someone can show a way more sensible way of coding this.

  • H (unregistered)

    public static Date getPreviousSaturDay(Date date){ Calendar calendar = Calendar.getInstance(); calendar.setTime(date); calendar.add(Calendar.DAY_OF_WEEK, -calendar.get(Calendar.DAY_OF_WEEK)); return calendar.getTime(); }

  • Developer Dude (unregistered)

    Welcome to my world.

    I see this kind of code not from beginners, but from "experienced" developers (over five years of experience in the given language), in production code.

    Either they don't have a firm grasp on the language - and probably never will - or they take a perverse pleasure in writing convoluted and usually incorrect code. Depending on the day of the week (pun intended), I vacillate between one and the other assumption.

  • lordofduct (unregistered)

    I find it hilarious that the coder knew 'setTime' existed, but didn't know 'getTime' existed. RTFM much?

    As for the really naive arithmetic via if statements... I mean, I guess they didn't realize that the 'day' returned from DAY_OF_WEEK is ordered, so thusly basic subtraction could be used... but again, RTFM. Or go back to 2nd grade math class!

    But yeah... reading the doc page for this Calendar class (sorry, never used it before, so this is my first time seeing it)... holy god is that the most obtusely designed interface I've ever seen in my life. Who did that? I can totally forgive the author for not knowing what to do with that god awful mess. There's TRWTF, the author of which should be hung by his neck from the ol' oak tree out back, and I ain't talking the neck on his shoulders.

  • Bidem (unregistered)

    Calendar.getTime() wouldn't have given the same result. By using format + parse you get a Date set to 00h00m:00.000 getTime would return a Date with hours, minutes and seconds depending of the time of execution

    What can be used is Calendar.clear(int field) which is a pain too

  • Chris (unregistered) in reply to H

    Actually misusing DAY_OF_WEEK integer value for calculation is far worse, as it will fail on any country that uses a different first day of the week. Using 3rd party libraries can be more hassle in corporate projects depending on license and policies, so this small function might be fine. The only WTF I can spot is to use string conversion in order to strip time, which is not that aweful as the date pattern was explicit.

  • my name (unregistered)

    i know that the true wtf is php and java is next best thing after sliced bread but

    var_dump(date('r', strtotime('last saturday'))); string(31) "Sat, 23 Apr 2016 00:00:00 +0300"

  • (nodebb)

    Not worthy of a code golf entry for sure...

  • _that_guy_ (unregistered) in reply to my name

    The real sadness is that we're up to Java 8 and people still think that Java could potentially still become a useable language.

  • Herby (unregistered)

    Note to self: Date and time calculations should be left to "experts". They have studied the problem and SHOULD know better. Hopefully the language/library has a function you can use. Look at the documentation!

  • Matt Westwood (unregistered) in reply to _that_guy_

    "The real sadness is that we're up to Java 8 and people still think that Java could potentially still become a useable language."

    That's nothing, Windows is up to 11, same problem.

  • Olivier (unregistered) in reply to Chris

    I do hope that the DAY_OF_WEEK is not depending on the locale and Saturday should always result to the same number, whatever the first day of the week can be.

    If not, there is a major WTF.

  • (nodebb) in reply to t0pC0der

    Actually, the fact that it is Java 8 isn't the problem. It will soon be 21 years since Java was released, and this stuff is broken.

    THAT's TRWTF.

    (Observation in passing: how long did it take Firefox to reach version 8 after they disconnected its version numbering from reality? That's why I say that the important part is the time, not the version numbers.)

  • linepro (unregistered)

    The TWTF is not using Joda time.... (which is the new Java 8 date API in any case)

    public static Date getPreviousSaturDay(Date date) {
    DateMidnight midnight = new DateMidnight(date.getTime());

     return midnight.addToCopy(-midnight.getDayOfWeek()).toLocalDate().toDate();
    

    }

    FTFY.

  • Ex-lurker (unregistered) in reply to linepro

    "The TWTF is not using Joda time.... (which is the new Java 8 date API in any case)"

    When you say that you ignore the fact that you just don't know WHEN this code was written. It might be old enough that Joda wasn't but an idea in some developer's mind.

  • wydok (unregistered)

    Seriously, would would look at a function called "withDayofWeek" and know it would return the closest previous Saturday???

  • CrushU (unregistered)

    Oh god this looks like code in an old codebase I used to work in.

    Oh wait, no it isn't. This throws a generic Exception. There was a clean sweep through the project to throw only subclasses of Exception once upon a time, and anyone who tried to commit 'throws Exception' since then was rejected and told to write the code again.

    But yeah, otherwise looks exactly like the code used to find a previous Saturday.

    They at least have the excuse that their codebase was limited to Java 6.

  • Alex (unregistered)

    Our code base also has getStartOfWeek functions that return the date of the start of the week. Once in ExtJS for the front-end, and once in C# for the back-end.

    In ExtJS, it is Ext.Date.parse("W",Ext.Date.format("W",date));

    "W" is the formatter for ISO calendar week, and when parsing that week number again, it returns the start of said week.

    In C# it is datetime.AddDays(-datetime.DayOfWeek).Date

    Not sure which one's good coding style and which one the WTF...

  • mattesno1 (unregistered)

    The only real issue with the code that I see, is that SimpleDateFormat is not thread-safe. Calling this method from different threads at the same time can produce arbitrary results.

  • mattesno1 (unregistered) in reply to mattesno1

    My mistake, I first read it as being a format constant rather than a String constant.

  • Nate Scherer (google) in reply to lordofduct

    And Calendar is the improved API to "fix" the terrible Date class. Yes, it was worse before Calendar was introduced.

  • Jacob (unregistered) in reply to linepro

    Is this example completely unreadable to anyone else? Because I don't think this is any better than the original post.

    public static Date getPreviousSaturDay(Date date) { DateMidnight midnight = new DateMidnight(date.getTime());

    return midnight.addToCopy(-midnight.getDayOfWeek()).toLocalDate().toDate(); }

  • Syntaxerror (unregistered) in reply to Jester

    I also noticed that and wondered, that Remy didn't say something about that. Also, it wasn't even tested properly, as this is something that one WOULD test, right? Right?

  • Syntaxerror (unregistered) in reply to Syntaxerror

    Oh wait, it was Jane.

  • Anonymous (unregistered) in reply to Chris

    Actually misusing DAY_OF_WEEK integer value for calculation is far worse, as it will fail on any country that uses a different first day of the week.

    It won't, because Calendar.SUNDAY through Calendar.SATURDAY don't change. Calendar.getFirstDayOfWeek() will tell you which of them is the first day of the week, if you need to know it.

    It's true, though, that code shouldn't care what the actual numbers are. There's nothing saying that they can't or won't ever change them. But as long as you know they're integers, and can safely assume they're all <= 7, and that they're mapped in order and increasing by 1 each day (it'd be pretty evil of them to map them any way that would violate these assumptions!), you can write: (I think this should work; I don't write Java)

    calendar.add(Calendar.DATE, (Calendar.SATURDAY - day - 7) % 7);

    ... and if you want to find a different day of the week, you can just change Calendar.SATURDAY to the other day, and it'll work.

Leave a comment on “See You Last Saturday”

Log In or post as a guest

Replying to comment #:

« Return to Article