- Feature Articles
- CodeSOD
- Error'd
- Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
As dates go, this one's got to be stoned.
Admin
Love the way getDateString returns a number
Admin
It even includes error checking! The first 11 1/2 days of 1970 are invalid anyway. So let's just return -1...
Admin
For some purposes that calculation might actually be faster than the library function.
One of the more complex features of C++ too that I have seen most coders get wrong.
There is a concept in C++ of streaming to convert between strings and data objects, and dates and times are among those. In order to perform such a conversion you can create a "facet" which then gets used when you parse or print a date.
It is good, of course, to create a facet and reuse it many times, especially in parsing, as it might be more efficient.
Unfortunately I have seen user's code whereby the same facet is created time and again for every single date, then attached to a new locale and imbued. And it is, of course, very inefficient. Thank-you to certain profiling tools that enabled me to discover this inefficiency. I have fixed it at least 2 supposedly reputable companies.
Remember that, when you print with a "printf" style format, something somewhere has to parse that format string. If we're going to use it multiple times, let's parse it just once and "store" the parsed result. But then lifetime management is one of the things that makes programming (in any language) less than trivial.
DateFormat / SimpleDateFormat has been part of Java for a long time and does of course address that situation, but you need to hold on to your DateFormat object as long as you need it.
Admin
That's going to be fun when the code starts to be multithreaded, with the static Calendar instance
Admin
That's the first time I have ever seen someone making a temporary reference holder introduced to keep lines short a static member of a class!
Admin
And if there is no intention to ever introduce multithreading into the application of which the given code is a part, then there is no need to make its classes thread-safe.
Oh, and even if "cal" would not be a static member, the code would not be thread safe because in Java "cal" will hold a reference, not a copy. Especially since Calendar.getInstance() looks like the singleton pattern.
So I assume multithreading will never be an issue with the code.
Alternatively, the coders might still be on a level where they simply don't know about multithreading and its perils. Then of course they will be in for a nice surprise if the code is run as part of a muktithreaded application.
Admin
Of the last five articles here, three of them had something to do with dates. Has this turned into a dating site?
Admin
Turns out that Calendar.getInstance() returns an object with the relevant fields set to the current time and date, according to the docs.
So I assume multiple calls to getInstance() will return references to different instances, although I couldn't find any clarification on that in the docs.
Admin
Now why do I all of a sudden have a craving for the dried fruit of phoenix dactylifera?
Admin
Actually it indicates that even one user at the same time can be troublesome.
MySQL, MySpace, My Verizon, My Little Pony.
Admin
Admin
Wait! I think I'm getting it:
I'll add more lines as soon as I figure out what comes after the millennium (Falcon, perhaps?).Hey! If you tilt your head to the right, all these 0L nearly becomes LOLOLOLOL or something. So clever that is genius.
Admin
Admin
I think that's very inconsiderate for all Bobbys across the world.
Admin
Admin
I'd imagine - that wording leaves for some unfortunate implications.
Admin
Admin
Admin
Admin
Admin
"you could leverage the handy built-in utilities"
Spoken like someone who's never had to use them.
Saying that, it looks as though they've sorted it in Java 8 - we'll see.
Admin
Multithreading is frequently never an issue during testing. All of the Java (and C# etc., for that matter) code I've seen posted here is intended to run in a multi-threaded service container, so multi-threading is almost always an issue.
Admin
Admin
How can Akismet think a link to stack overflow is spam???
Admin
CAPTCHA: ingenium A rare element with a depressingly short half life.
Admin
Interesting, just finished reading the Divine Comedy a few weeks back and (Who am I kidding, I read Inferno, sue me) I was thinking about this programming's hell too.
Admin
Quoth snoofle, "Dates are complicated things."
Yep, they sure are. It starts with "It's not about the nail." http://vimeo.com/66753575 and goes into deeper madness from there.
Admin
Yeah, it happened on the frist of the month.
Admin
In defense of this abomination, I know of no standard library that handles DateTime(zone) stuff in a way that is not completely broken. Staying the hell away from it and rolling your own - even this - is not such a bad thing.
Admin
Admin
He would, but his Calendar Tool is broken 'cause the DateTime calculator backend crashes and he can't figure out when he was going to go where.
Admin
I dread the day when someone will pick up that code, point out that it doesn't support sub-second displays, and change it all to use floating-point.
Admin
The correct format with SimpleDateFormat is "yyyyMMddHHmmss": h: Hour in am/pm (1-12) H: Hour in day (0-23)
It's also a good idea to set the TimeZone, so it doesn't depend on the plaform's timezone. :)
Admin
Admin
I only have few things I want a DateTime object to do. Sadly I know of no DateTime in any lanugage that supports the following operations.
Joda/Noda works, but cumbersome - which is amazing enough apparently - and isn't a standard library. Other than that I wouldn't know.
Admin
While I agree that I agree that there may be no reason to go to great lengths to make code threadsafe if you don't know it will ever be used in multi-threaded code, there are some very simple practices that make code both more threadsafe and better code when not used in multi-threaded apps.
That aside, Calendar.getInstance() is not a singleton pattern - it creates a new instance of Calendar for the given locale (if not locale is given, then it uses the current locale at time of invocation).
The observation that static instance of the Calendar would be an issue with multi-threaded code is correct because the same Calendar object instance would be used by each instance of MyDateString, and setting the current "time" value for that instance could easily conflict with another thread setting that value concurrently.
If the Calendar instance was not static, i.e., a new instance for each instance of MyDateString, and each thread had its own instance of MyDateString, then that would not be an issue.
IMO, the author of the code probably thought he/she was saving memory by creating a static copy of Calendar - a case of premature optimization
Admin
Natural solves most of these, but is mostly used on mainframes: http://documentation.softwareag.com/natural/nat638unx/pg/pg_furth_date.htm
(although there are such things as Natural for Open Systems and Natural for Ajax)
Admin
After writing this I realised I haven't looked at Java 8's new DateTime API yet. It might handle this. It's still a shite state of affairs that these operations are not the norm.
Admin
or...don't use java. it's pure evil!
Admin
Dammit Bobby!
Admin
Admin
True, but it might turn out to be the only reasonable DateTime library in the stdlib. C? no. C++? no. Python? no. Ruby? no. Lua? what stdlib? .NET? no. SQL? lolno. Rust? no.
Maybe Go or Haskell, I wouldn't know about those.
Admin
I'll be happy with Java 8 if the only change they make is the ceasing of attempting to trick users into installing %$#&ing Ask Toolbar. Every damn time Java updates I have to head down to the finance person's PC - they need Java for the bank's 2-factor auth - and remove Ask Toolbar (or perform the update myself).
A runtime for a programming language should not come bundled with adware. Grr.
(Is there any way around this I'm not aware of?)
Admin
I'm hoping to get hooked up with Paula Bean!
Admin
Admin
And usually the "roll your own" one are less functional then the existing libs....so unless you expect people to roll their own with all the functionality you've mentioned above I'm not sure you can make a case for "Staying the hell away from it and rolling your own - even this - is not such a bad thing".
Or perhaps you've been rolling your own.....
Admin
http://download.java.net/jdk8/docs/api/index.html?java/time/package-summary.html
Admin
Is it just me, or are 'long' variables limited to 32 bits. If so, this code has a bunch more problems than being an obtuse method of fancying up a date to a bunch of digits to play with.
In other words: "Use the system routines, they have (we hope) been tested/used by others".
Note to self: Retire before 2038 (maybe before Jan 19, 2038). Pick up the end of year bonus, and wait a week into the new year!
Admin
Try XQuery