This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Java in General and the fly likes Code alternative Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Code alternative" Watch "Code alternative" New topic

Code alternative

Sindhu Kodoor
Ranch Hand

Joined: Sep 03, 2010
Posts: 65
Hi ,

I have a method that converts the given input to the required format,:

But "Calendar inputDate" is not the only input to be formatted, it can come in of datatype "Date" or of "String", how do I do that?please suggest
Raymond Tong
Ranch Hand

Joined: Aug 15, 2010
Posts: 230

method overloading
Rob Spoor

Joined: Oct 27, 2005
Posts: 19656

Some suggestions:
- rename your method to "toRequiredFormat" or simply "format". Java supports method overloading, so you don't need to specify twice that this method takes a Calendar (once in the parameter list, once in the name).
- don't throw Exception; throw ParseException instead as that's the actual exception type being thrown.
- don't ever write code like this again:
You will loose all information on the message except the message. Better alternatives:
or even simpler

How To Ask Questions How To Answer Questions
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

Then make it this:
Because then in JDK 7 (8?) the safe re-throw concept will kick in.

"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Matthew Brown

Joined: Apr 06, 2010
Posts: 4344

Wouter Oet wrote:Because then in JDK 7 (8?) the safe re-throw concept will kick in.

Interesting - I've not heard of that. I did a quick search, but didn't find anything very specific. Do you have any good references that will expain what that involves?
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

I'll show an example:
This won't compile under JDK 6 because you throw an exception but don't declare it. Under JDK 7/8 the compiler understands that the value of e is a CheckedException or a Runtime exception thus it's valid to throw it. The key is that e must be final otherwise it's value can change in the catch block.
Rob Spoor

Joined: Oct 27, 2005
Posts: 19656

Looks weird to me. It makes sense, as the compiler realizes the only values for e can be CheckedException or a RuntimeException, but that doesn't make it look better. It just looks like you're throwing Exception and therefore the method should declare to throw Exception. A lot of programmers will get confused when they read such code.

I'd prefer one of the other changes to catching exceptions that were planned: multicatch:
In the end, I agree with the final notes in this article: multicatch is awesome, final rethrow is weird.
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

I agree with you. Multi-catch is great and safe re-throw is nice. Multi-catch shall be used much more often by programmers than the re-throw functionality.
But the re-throw functionality is great for wrapping method calls and I assume that it will mainly be used by frameworks.
I agree. Here's the link:
subject: Code alternative
Similar Threads
Gregorian Calandar
how to set 2011-11-06T14:34:16.679+02:00 into XMLGregorianCalendar
Problem with date formatting
xml date conversion all date conversions
Integer too large?