Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Date Format Problem And Hashtable ......  RSS feed

 
Gabriel Fox
Ranch Hand
Posts: 170
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Guys,On my JSP i ran a query which returns among others a date as : dd/mm/yyyy hr:mins
e.g. 03/12/2001 12:56
1.How do i format this to "ddmmyyyy" i.e. a string
which is the date-format on the database table
i will eventually insert the form parameters into.
2.What are the issues involved in setting the
number of elements of a java.util.Hashtable
like this :Hashtable hs=new Hashtable(30);
then populate hashtable as needed
3.Is it advisable to store thousands of records in a hashtable and keep in session scope what issues are invovled.
4.How do U increase the number of elements of a java.util.Hashtable on an as-needed basis,with an initial number at creation time of hashtable.I was
thinking of the :
hashRef.rehash() , but it allows no arguments.
Please ideas,codes and references are welcomed.
Cheers.
 
Napa Sreedhar
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please see the sample code for your 1st query.
<%@ page ..
import java.util.StringTokenizer;
%>
<%
// date retrieved format dd/mm/yyyy hrs:min
StringTokenizer st1 = new StringTokenizer(date);
String firstToken = st1.nextToken(); // date
String seconToken = st1.nextToken(); // time
StringTokenizer st2 = new StringTokenizer(firstToken. "/");
String dd = st2.nextToken();
String mm = st2.nextToken();
String yyyy = st2.nextToken();
int value = Integer.parseInt(dd)* 1000000 + Integer.parseInt(mm)* 10000 + Integer.parseInt(yyyy);
// convert the value to string
String sDate = new String(value);
// insert sDate your update query to Database
%>
I'll reply to your next queries soon.
Napa
 
Tim Holloway
Bartender
Posts: 18531
61
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually, a better place to ask this sort of question would be the "Intermediate Java" questions forum, since it's not specific to JSPs.
But as long as we're here - On the first question, you can reformat a date string as a string operation, as Napa Sreedhar illustrates. If you plan on doing any sort of computations with it, however, you should create a java.util.Date object from the string.
On the subject of hashes - thousands of items is generally a bit much for a hash table. At that level, you're better off using a database, since you risk running out of memory and/or causing massive amounts of paging that can degrade or crash the machine. If you do want to do large hashtables, however, use a larger number than 30 or you'll see a lot of hash collisions. The default used by Java's own properties objects is 100. To use a hashtable effectively, you want to get as close as you can to the "perfect" hash. That is one where there are no empty slots and no hash overflow chains so neither storage nor access time are sub-optimal. Usually that means you'll need to override the hashCode() method with one that is tuned to avoid collisions.
For detailed information on optimal hashing, you should consult some good texts on algorithms.
 
Paul Stevens
Ranch Hand
Posts: 2823
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
SimpleDateFormat
 
Gabriel Fox
Ranch Hand
Posts: 170
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Guys.
Napa , nice logic.
Tim, i have decided to post this topic to the
the JSP forum,because the Logic for retrieving
database extract as date and hash table performance issues are all embedded in my JSP.
Except U want to tell me JSP is intermediate java
and not used in webapps.
Paul,i was actually thinking of the use of
the DateFormat and Date.But,not sure of how to go about it for this scenario.
Cheers guys.More ideas are welcomed.
[ January 31, 2002: Message edited by: Gabriel Fox ]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!