Handling Date

Scenario:

I have news archive html form that'll search news from this date to this date.

I have given option list as day, month, year

I am getting value of month as number and started from 0 (i.e. jan)

2. in servlet file I am getting the parameters of day, month, year

and combining them with a String startDate=day+"-"+month+"-"+year;

but facing problems

Please guide me about the accurate way to deal with dates

thanks & kind regards

[547 byte] By [farakha] at [2007-11-27 2:55:31]
# 1

> but facing problems

Please elaborate "problems".

Meanwhile you can take a look at [url=http://java.sun.com/j2se/1.5.0/docs/api/java/util/Date.html]java.util.Date[/url], [url=http://java.sun.com/j2se/1.5.0/docs/api/java/util/Calendar.html]java.util.Calendar [/url] and [url=http://java.sun.com/j2se/1.5.0/docs/api/java/text/SimpleDateFormat.html]java.text.SimpleDateFormat[/url].

BalusCa at 2007-7-12 3:32:20 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2

> > but facing problems

>

> Please elaborate "problems".

>

> Meanwhile you can take a look at

> [url=http://java.sun.com/j2se/1.5.0/docs/api/java/util

> /Date.html]java.util.Date[/url],

> [url=http://java.sun.com/j2se/1.5.0/docs/api/java/util

> /Calendar.html]java.util.Calendar [/url] and

> [url=http://java.sun.com/j2se/1.5.0/docs/api/java/text

> /SimpleDateFormat.html]java.text.SimpleDateFormat[/url

> ].

the message appears "Not a valid month"

why becuase I am getting month as one digit e.g. 2 for March

but when am making query it gives error as the oracle oracle format is to_char(news_date,'dd-mm-yy');

when am changing the format as to_char(news_date,'dd-m-yy'); it gives the error "date format not recognized"

Question:

Can i change the calendar date 01 despite 1?

or anyother solution please

thanks for your help

farakha at 2007-7-12 3:32:20 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3

Use SimpleDateFormat to convert between String and Date.

Date date = new Date(); // Today's date.

SimpleDateFormat sdf = new SimpleDateFormat("dd-M-yy");

String dateString = sdf.format(date); // 01-5-07

And the reversed way:String dateString = "01-5-07";

SimpleDateFormat sdf = new SimpleDateFormat("dd-M-yy");

Date date = sdf.parse(dateString); // Tue May 01 00:00:00 [zone] 2007

You can find the date format patterns in the SimpleDateFormat API. Kindly click at the links I provided here above and read them. You learns a lot from the API documentation.

BalusCa at 2007-7-12 3:32:20 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4
wow! great!thanks BalusC for guidancethanks again
farakha at 2007-7-12 3:32:20 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 5

this code doesn't show any results. Can you please help me as to why it not sowing?

thanks in advance

String oraStart=rday+"-"+displayMonth+"-"+ryear;

String oraEnd=rday1+"-"+displayMonth1+"-"+ryear1;

rs=stm.executeQuery("select unique a.news_id from news_product a,news b where "+

" a.news_id=b.news_id and product='"+product+"' "+

"and news_date>='"+oraStart+"' and news_date<='"+oraEnd+"'");

while(rs.next()){

news_id=rs.getInt(1);

out.println(news_id);

farakha at 2007-7-12 3:32:20 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...