generically controlling precision of date display

S

SN

hi

I need to control the precision of date display i.e. conditionally
include year info or month info etc.
Something like the following function - the catch is that the
dateFormat is already an input to this method - the dateFormat is
derived from user preferences - but user preferences only allow the
user to choose the appearance of the date viz. mm/dd/yy or dd/mm/yy or
dd-mm-yyyy etc.

What components of date to show (i.e. the date precision - hours, day,
month) is decided by program logic

Can I chop off components from a given DateFormat in a generic way?
i.e. i want a way to say "use this date format but don't include year
information"

String getDateDisplayString(SimpleDateFormat df, java.util.Date d, int
precision)
{
switch(precision)
{
case 1: //return string with only month and year
case 2: //return string with only day and month
case 3: //return string with only day, hour and am/pm
}
//df.format(d) will always spit out all the components of the date
}

thanks
sn
 
V

VisionSet

SN said:
Can I chop off components from a given DateFormat in a generic way?
i.e. i want a way to say "use this date format but don't include year
information"

How about this:

SimpleDateFormat df; // from somewhere

String pat = df.toPattern();

// manipulate pattern - regex?

df.applyPattern(pat);

return df.format(myDate);
 
T

Tony Dahlman

SN said:
hi

I need to control the precision of date display i.e. conditionally
include year info or month info etc.
Something like the following function - the catch is that the
dateFormat is already an input to this method - the dateFormat is
derived from user preferences - but user preferences only allow the
user to choose the appearance of the date viz. mm/dd/yy or dd/mm/yy or
dd-mm-yyyy etc.

What components of date to show (i.e. the date precision - hours, day,
month) is decided by program logic

Can I chop off components from a given DateFormat in a generic way?
i.e. i want a way to say "use this date format but don't include year
information"

String getDateDisplayString(SimpleDateFormat df, java.util.Date d, int
precision)
{
switch(precision)
{
case 1: //return string with only month and year
case 2: //return string with only day and month
case 3: //return string with only day, hour and am/pm
}
//df.format(d) will always spit out all the components of the date
}

thanks
sn

Mike has certainly given you a workable solution. But I wonder about
the design constraints you gave....

By using Java's Locale features, you can predict the actual date format
preference of any user, except perhaps whether they prefer 4- or 2-digit
year representations. You can even predict the correct abbreviations
for month based on their native language!

So if you *only* ask the user whether they want 4- or 2-digit years, then
you could simply create 3 different formatters and use the appropriate
one as required by the "program logic." All your work is done unless
the damn user selected 2-digit years, in which case at least some of
what Mike suggested may be needed.

Best solution, IMHO, is not to ask the user's preference for the date
format but just to proceed based on his/her Locale to handle everything
using 4-digit year representations.

Tony Dahlman
 
S

SN

Thanks Mike, Tony. I guess I'll have to proceed on the lines of Mike's
suggestion. The constraints are something I can do nothing about.
However all this makes me wonder if the design of DateFormat could
have differentiated between the components of a date and the way it is
rendered. That way we could we selectively switch on/off certain
components (year, month etc) while preserving the rendering for the
switched 'on' components.

The regex solution unfortunately is not generic and needs minor tweaks
for different formats.

thoughts?
-sn
 
T

Tony Dahlman

SN said:
Thanks Mike, Tony. I guess I'll have to proceed on the lines of Mike's
suggestion. The constraints are something I can do nothing about.
However all this makes me wonder if the design of DateFormat could
have differentiated between the components of a date and the way it is
rendered. That way we could we selectively switch on/off certain
components (year, month etc) while preserving the rendering for the
switched 'on' components.

The regex solution unfortunately is not generic and needs minor tweaks
for different formats.

thoughts?
-sn

Thoughts? Sure. Your idea is out of line with the decisions made in
designing the Java libraries, but no problem. Instead of using
SimpleDateFormat, you just subclass DateFormat yourself. But I would
suggest subclassing SimpleDateFormat which, when you review the source
code, you will agree is pretty sophisticated. But if your code has
wide applicability it may become part of a future Java release. And
it will certainly manage to deal with your inflexible constraints.

BTW be prepared to deal with the need to applyPattern() and similar
tedious elements of dealing with dates in a language with international
applicability. And please let us know what you come up with.

Tony Dahlman
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top