generically controlling precision of date display

Discussion in 'Java' started by SN, Nov 10, 2003.

  1. SN

    SN Guest

    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
    SN, Nov 10, 2003
    #1
    1. Advertising

  2. SN

    VisionSet Guest

    "SN" <> wrote in message
    news:...
    >
    > 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);


    --
    Mike W
    VisionSet, Nov 10, 2003
    #2
    1. Advertising

  3. SN

    Tony Dahlman Guest

    SN wrote:
    >
    > 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
    ---------------------------------------
    a(nospace)dahlman(at)attglobal(dot)net
    Tony Dahlman, Nov 11, 2003
    #3
  4. SN

    SN Guest

    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
    SN, Nov 12, 2003
    #4
  5. SN

    Tony Dahlman Guest

    SN wrote:
    >
    > 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
    ---------------------------------------
    a(nospace)dahlman(at)attglobal(dot)net
    Tony Dahlman, Nov 13, 2003
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Peter Grison

    Date, date date date....

    Peter Grison, May 28, 2004, in forum: Java
    Replies:
    10
    Views:
    3,183
    Michael Borgwardt
    May 30, 2004
  2. VisionSet

    generics - overriding generically

    VisionSet, Jan 8, 2006, in forum: Java
    Replies:
    14
    Views:
    738
    Oliver Wong
    Jan 9, 2006
  3. Replies:
    0
    Views:
    496
  4. moroder
    Replies:
    1
    Views:
    429
    David Carlisle
    Oct 15, 2004
  5. Marco Aschwanden

    How to generically transform a list?

    Marco Aschwanden, Aug 26, 2004, in forum: Python
    Replies:
    4
    Views:
    275
    Arthur Rambo
    Aug 27, 2004
Loading...

Share This Page