File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes Parsing Date Values for Database Search Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Parsing Date Values for Database Search" Watch "Parsing Date Values for Database Search" New topic
Author

Parsing Date Values for Database Search

Zubin Wadia
Greenhorn

Joined: May 23, 2001
Posts: 8
Hello Everyone!
I have a problem - I have a dateinstock field that is a text field. it is a SQL 7.0 DB -
it is in MM/DD/YYYY format
I need to know how to retrieve those dates and parse them into string for Month and Year.
Once I do that - I use the values and compare them to the values inputted by the user.
Has anybody done it like this?? Basically it is a Date Range function which has values in 2 text boxes - Year1 and Year2 - how does one delve into a DB text field and retrieve values based on the year range inputted.
Thanks, all help is appreciated.
Completely Clueless.
Byron Bignell
Greenhorn

Joined: Sep 29, 2001
Posts: 13
Hi,
Everyone here has been very supportive of my trashing about looking for solutions. This is THE BEST resource for Java/JSP, BAR NONE.
So...based on what I've learned here this is what I'm doing to compare dates from SQL and such...
This is what I did...
// Get the amount of time between the two dates
long msecs = date2.getTime() - date1.getTime();
// Create a GregorianCalendar and set its time zone to GMT
GregorianCalendar delta = new GregorianCalendar(
new SimpleTimeZone(5, ""));
// Set the calendar's time to the difference between the two
delta.setTime(new Date(Math.abs(msecs)));
// The "base" year is 1970, so subtract that number
out.println("Years: " +(delta.get(Calendar.YEAR) - 1970) + "<br>");
out.println("Months: " + delta.get(Calendar.MONTH) + "<br>");
// The "base" date is 1, so subtract that number
out.println("Days: " + (delta.get(Calendar.DATE) - 1) + "<br>");
out.println("Hours: " + delta.get(Calendar.HOUR) + "<br>");
out.println("Minutes: " + delta.get(Calendar.MINUTE) + "<br>");
out.println("Seconds: " + delta.get(Calendar.SECOND) + "<br>");
if(delta.get(Calendar.DATE) >= 3){
out.print("FLAGGED");
}
Question I have now is...can it be done more efficiently??
Abraham Jacob
Greenhorn

Joined: Oct 16, 2001
Posts: 25
There are lot of ways I guess Here is something that might help u
private String changeDateFormat(String _oldDate, String _fromFormat, String _toFormat) {
if (!((_oldDate.equals("")) | | (_oldDate == null))) {
Date d = new Date();
SimpleDateFormat df = new SimpleDateFormat(_fromFormat);
try {
d = df.parse(_oldDate);
} catch(Exception ex) {
System.out.println("Unable to parse date " + _oldDate);
}
df.applyPattern(_toFormat);
return df.format(d);
} else
return "";
}
Pranit Saha
Ranch Hand

Joined: Sep 09, 2001
Posts: 130
Hi,
It seems to that, u want to retrieve the parts of the date.. like date, month and year.. If it's so.. in SQL date is retrieved in yyyy-mm-dd 00:00:00 format.. to retrieve the parts of the date u can Use this method..
String dt = rs.getString("datefield");
StringTokenizer st = new StringTokenizer(dt," ");
if(st.hasMoreElement())
{
String date=st.nextElement();
String time=st.nextElement();
}
StringTokenizer sz=new StringTokenizer(date,"-");
if(sz.hasMoreElement())
{
String year=sz.nextElement();
String month=sz.nextElement();
String day=sz.nextElement();
}
Hope this will help u..
Pranit..
Originally posted by Zubin Wadia:
Hello Everyone!
I have a problem - I have a dateinstock field that is a text field. it is a SQL 7.0 DB -
it is in MM/DD/YYYY format
I need to know how to retrieve those dates and parse them into string for Month and Year.
Once I do that - I use the values and compare them to the values inputted by the user.
Has anybody done it like this?? Basically it is a Date Range function which has values in 2 text boxes - Year1 and Year2 - how does one delve into a DB text field and retrieve values based on the year range inputted.
Thanks, all help is appreciated.
Completely Clueless.

Pranit Saha
Ranch Hand

Joined: Sep 09, 2001
Posts: 130
Sorry.. there is a mistake in my previous post.. it should be hasMoreTokens() insteadof hasMoreElement() and nextToken() insteadof nextElement()
Pranit..
Originally posted by Pranit Saha:
Hi,
It seems to that, u want to retrieve the parts of the date.. like date, month and year.. If it's so.. in SQL date is retrieved in yyyy-mm-dd 00:00:00 format.. to retrieve the parts of the date u can Use this method..
String dt = rs.getString("datefield");
StringTokenizer st = new StringTokenizer(dt," ");
if(st.hasMoreElement())
{
String date=st.nextElement();
String time=st.nextElement();
}
StringTokenizer sz=new StringTokenizer(date,"-");
if(sz.hasMoreElement())
{
String year=sz.nextElement();
String month=sz.nextElement();
String day=sz.nextElement();
}
Hope this will help u..
Pranit..

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Parsing Date Values for Database Search