Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dtae format for view

Status
Not open for further replies.

strutsprog06

Programmer
Jul 26, 2006
5
US
I have date saved in my oracle table as:7/24/2006 12:16:53
When i Run a query like this:
select date_created from post;

It returns result like: 2006-07-24 12:16:53

I want to display the date like Mon, Jul 24, '06.
In my view of this line:

<bean:write name='browseall2' property="value(date_created)" filter='true' />

How should i do it.
i used simple format java function like this:


public String formatDate(String dates){

String returnDate = "";
Date tempDate=null;
if(dates != null && dates.length() > 0){

try{
//from timestamp date replace - with /
String repDate= dates.replace('-','/');

//System.out.println(repDate);
tempDate = new SimpleDateFormat("yyyy/MMM/DD hh:mm:ss").parse(repDate);
// System.out.println("tempDate " + tempDate);
returnDate = new SimpleDateFormat("EEE, MMM d, ''yy").format(tempDate);
// System.out.println("returnDate " + returnDate);
}catch(Exception e){
e.printStackTrace();
}
}
return returnDate;
}
}

But it id getting confuse on month and 07 always became Month Jan.

If I use mm/dd/yyyy instaed of this yyyy/MMM/DD hh:mm:ss
It messes up more
date is taken as month and other wrong things.

I tried this also:

<bean:write name="topic" property='value(date_created)' formatKey="my.own.format.key" />
It gived no error but does not do anything:

Date displayed on view is like this:
2006-07-24 12:16:53
I want to dispaly Mon Jul 24, '06

Please guide.
 
Hi

If you have a date from the database, why do you handle it like String in Java ? That is not a good idea.

I suggest you to format that value in Oracle instead of Java. I find this easier to include in JSP. Of course, is not easier at all if you need to do some calculations too.
Code:
select to_char(date_created,'Dy Mon DD, ''YY') date_created from post;

Feherke.
 
Hi

That "tHIS" means my suggested [tt]select[/tt] ? Could you post the actual table structure and the [tt]select[/tt] statement you used ? And would help to see the Java code which does the database operation.

Feherke.
 
no the error was using two tables so it got confused which table is used:

The Query is now:
Which is no error but date is not displayed.
"select HLPOST.POST_ID, HLPOST.CAT_ID,HLPOST.TITLE,HLPOST.PRICE,to_char(HLPOST.DATE_CREATED,'Dy Mon DD, ''YY'),CATEGORY.CATEGORY_NAME from HARVIELIST.HLPOST, HARVIELIST.CATEGORY where HLPOST.cat_id = CATEGORY.cat_id order by CATEGORY.category_name, HLPOST.date_created desc"


Some part of cade:
request.setAttribute("posts", post_Hash1);

post_Hash1 is ArrayList

and Post is bean.


Display method:

<bean:write name='browseall2' property="value(date_created)" filter='true' />

This java and struts:



 
Hu

In my example there was an alias to that column. After using a ny operations on the value, in this case formatting with [tt]to_char[/tt], the column will have a generated name, unless you give it an alias. Otherwise refering to it by name will probably fail.
Code:
select HLPOST.POST_ID, HLPOST.CAT_ID,HLPOST.TITLE,HLPOST.PRICE,to_char(HLPOST.DATE_CREATED,'Dy Mon DD, ''YY') [red]date_created[/red],CATEGORY.CATEGORY_NAME from HARVIELIST.HLPOST, HARVIELIST.CATEGORY where HLPOST.cat_id = CATEGORY.cat_id order by CATEGORY.category_name, HLPOST.date_created  desc

Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top