• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Parsing Date Values for Database Search

 
Zubin Wadia
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 130
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 130
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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..

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic