struts-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Andrew Hill" <andrew.david.h...@gridnode.com>
Subject RE: Newbie.... Best Practices for ActionForms
Date Mon, 02 Jun 2003 16:09:55 GMT
Here ya go mate.

Dont think theres anything struts specific, just a case of applying the
appropriate java date classes, but its a pita to try and grok the little
blighters so Ive given you some example code pasted from one of my classes
you can eyeball instead.

Chop it about a bit so teacher cant see you've copied ;->

Second method wont compile as is (I butchered it after pasting to take out
some app specific oddities) and it doesnt really take much notice of the
locale  but shouldnt take you long to get it up to speed.


/**
   * Convienience method that returns a string representation of what time
the specified Date is
   * in the timezone specified using the DateFormat supplied and the
specified locale from which
   * language and local 'cultural rules' are extracted for date display.
   * If the date is null a new one with current time is instantiated as a
throwaway object
   * If the timezone and locale are null will use the defaults.
   * If the format is null will create a throwaway DateFormat object with
the format string.
   * Any timezone encoded in the format will be restored after the
operation - but will not be used
   * even if you pass null for timezone (of course if you pass
tz.getTimezone() thats a different
   * matter...) Note that if you supply your own DateFormat, then the locale
you pass as a parameter
   * is ignored - so if your passing a SimpleDateFormat you will need to set
it yourself.
   * "EEE MMM dd HH:mm:ss zzz yyyy" (which is what Date.toString() uses at
the moment)
   * @param date - date to stringify
   * @param tz - TimeZone in which date is to be used (for display usually)
   * @param locale - Locale for language info
   * @param format - a DateFormat object specifiying the output format to
use
   * @return string representation of date suitable for display
   */
  public static String formatDateInZone( java.util.Date date,
                                  TimeZone tz,
                                  Locale locale,
                                  DateFormat format)
  {
    TimeZone originalZone = null;
    if(date == null) date = new Date();
    if(tz == null) tz = TimeZone.getDefault();
    if(format == null)
    {
      if(locale == null) locale = Locale.getDefault(); //locale only applies
when we create the df
      format = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy",locale);
    }
    else
    { //If they passed in the zone we need to keep track of its original
timeZone so that
      //we can restore it when we are done
      originalZone = format.getTimeZone();
    }
    format.setTimeZone(tz);
    String result = format.format(date);
    if(originalZone != null) format.setTimeZone(originalZone);
    return result;
  }


public static java.util.Date parseDate( String dateString,
                                          TimeZone tz,
                                          Locale locale,
                                          DateFormat format)


    if(dateString == null) return null;
    TimeZone originalZone = null;
    if(tz == null) tz = TimeZone.getDefault();
    if(format == null)
    {
      if(locale == null) locale = Locale.getDefault();
      format = new SimpleDateFormat(DATE_INPUT_PATTERN, locale);
    }
    else


      originalZone = format.getTimeZone();
    }
    format.setTimeZone(tz);
    java.util.Date result = null;
    try
    {
      result = format.parse(dateString);
      return result;
    }
    catch(ParseException pe)
    {
      ; //Swallow and return null
    }
    if(originalZone != null) format.setTimeZone(originalZone);
    return result;
  }

-----Original Message-----
From: Aaron Longwell [mailto:listservs@newmedialogic.com]
Sent: Monday, 2 June 2003 23:57
To: Struts Users Mailing List
Subject: Re: Newbie.... Best Practices for ActionForms


Thanks,

I am planning to use the struts validator.... if I'm hearing you
right... this is the process?

In ActionForm:
String StartDate;
String End Date;

In Struts Validator:
validate the strings against a RegEx datetime mask

In Action:
1) Convert my Event ValueObject (VO)'s date values to strings when
populating the ActionForm.
1) Parse the strings to convert back to date when going from Form to VO....
Is there a struts technology that will help in this conversion... (from
string to java.util.Date).  I always could just use the java.util
classes, but I'm guessing this is a common problem with a common solution.

Thanks again,
Aaron

Andrew Hill wrote:

>1.) Yes (I even do this for true/false checkboxes!)
>2.) See #1.
>
>For my dates Im using several fields (d,m,y etc...) and some javascript to
>extract/concatenate into a hidden field in the format yyyy-MM-dd HH:mm:ss ,
>but its pretty ugly and I dont like it much (luckily only have one place I
>need to enter dates so far!). If you search around you can also find some
>nice js calendar controls (and there should be some opensource tags for it
>somewhere).
>
>If you want to avoid JS, best bet might be a simple text field where the
>user enters it in a certain format which you validate (storing in AF as
>String). If your using the struts validator I believe it has date support -
>and may even extend to some user friendly client side js support as well,
>though Ive not used it so dont know how far it goes.
>
>-----Original Message-----
>From: Aaron Longwell [mailto:listservs@newmedialogic.com]
>Sent: Monday, 2 June 2003 23:39
>To: Struts User List
>Subject: Newbie.... Best Practices for ActionForms
>
>
>I am beginning my first Struts project with this setup:
>
>mySQL Database
>Tomcat 4
>OJB (ObjectRelationalBridge)
>
>My questions:
>
>1) What is the best practice for the properties in an ActionForm bean?
>Because HTTP sends everything from forms as strings, do I make all
>properties Strings, then convert them in the Action (after validating of
>course)?
>
>2) I am using a mySQL DateTime column in the database. What is the best
>practice for editing these values via Struts? Do I put a java.util.Date
>proeprty in the Action form? Or do I use a String (as in Q #1) and then
>do a conversion in the Action?
>
>I am building an event manager... so the StartDate (actually
>StartDateTime) and EndDate (EndDateTime) are central to this
>application. I have not seen any examples of people editing DateTime or
>even Date values in a struts application.
>
>Could someone with experiece in this area give me a brief explanation of
>the recommended workflow for editing dates?
>
>Thanks,
>Aaron Longwell
>
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
>For additional commands, e-mail: struts-user-help@jakarta.apache.org
>
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
>For additional commands, e-mail: struts-user-help@jakarta.apache.org
>
>
>
>
>
>


---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org


Mime
View raw message