views:

53

answers:

2

Eclipse is warning that I'm using a depricated method:

eventDay = event.getEvent_s_date().getDate();

So I rewrote it as

eventDay = DateUtil.toCalendar(event.getEvent_s_date()).get(Calendar.DATE);

It seems to work but it looks ugly. My question is did I refactor this the best way? If not how would you refactor? I need the day number of a date stored in a bean.

I ended up adding a method in my DateUtils to clean it up

eventDay = DateUtil.getIntDate(event.getEvent_s_date());

public static int getIntDate(Date date) {
    return DateUtil.toCalendar(date).get(Calendar.DATE);
}
+3  A: 

It's fine. To me the uglier bit is the underscore in the method name. Java conventions frown upon underscores there.

You may want to take a look at joda-time. It is the de-facto standard for working with date/time:

new DateTime(date).getDayOfMonth();
Bozho
I have to agree, re: the underscores in the method name. This looks like someone trying to re-write a block of PHP code in Java.
Steve Perkins
The underscores come from my database field names. At one point along the way I had to reference the fields with all caps and it got hard to read EVENTSDATE as event start date. So I got in the habit of using the underscore
jeff
@jeff the fact that your database names use one convention does not mean your java code can't use another convention. Actually, it is a common practice.
Bozho
thanks I'll consider this for the future.
jeff
A: 

If you want a more elegant Date/Time api, use Joda Time.

highlycaffeinated
then tell him how to do what he wants with joda-time
Bozho
@Bozho you beat me to it!
highlycaffeinated
:) true (15chrs)
Bozho