Solved

how to convert date into dd mon yyyy format

Posted on 2003-10-24
24
9,198 Views
Last Modified: 2007-12-19
hi all,
i have a problem in date format i am using date like below .
<%java.util.Date date = new java.util.Date();%>

i am inserting date into a table and its storing like this

insert into tablename (d_date) values (date)

and its inserting date like below

Sun Oct 19 09:05:45 GMT+03:00 2003

i want to fetch date in dd mon yyyy format.
with this format i want to make a select query.i struck with the format conversion.



how to do this.

waiting for expert solution.


0
Comment
Question by:saleemkhan
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 12
  • 11
24 Comments
 
LVL 4

Expert Comment

by:Zonebit
ID: 9614064
saleem,
You would be better off selecting the date from the database as is, then formatting it the way you want it using Java.  You could use the SimpleDateFormat class to accomplish this.

http://java.sun.com/j2se/1.4.2/docs/api/java/text/SimpleDateFormat.html
0
 

Author Comment

by:saleemkhan
ID: 9614087
Hi,
 zonebit,
  yes you are right i am fetching date from database only.
i just go throgh  the link u sent me.i couldnt work it out

can u help me
 convert
Sun Oct 19 09:05:45 GMT+03:00 2003
        to
dd mon yyyy

waiting for expert solution.
0
 
LVL 4

Expert Comment

by:Zonebit
ID: 9614151
I don't really program java but I think this should work.

import java.text.DateFormat;
import java.text.SimpleDateFormat;

// Select the date from the database and store it in a Date variable called yourDate //

String tPattern = "dd MMM yyyy";
SimpleDateFormat sdf = DateFormat.getDateInstance();
sdf.applyPattern(tPattern);
String formattedDate = sdf.format(yourDate);
0
Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

 

Author Comment

by:saleemkhan
ID: 9614261
i tried your comment,

i am getting error here

SimpleDateFormat sdf = DateFormat.getDateInstance();

type mismatch cannot convert from java.text.dateformat to java.text.simpledateformat

0
 
LVL 4

Expert Comment

by:Zonebit
ID: 9614277
We'll try this next:

String tPattern = "dd MMM yyyy";
SimpleDateFormat sdf = new SimpleDateFormat(tPattern);
sdf.applyPattern(tPattern);
String formattedDate = sdf.format(yourDate);
0
 

Author Comment

by:saleemkhan
ID: 9614339
i am getting error

Error 500: Server caught unhandled exception from servlet [JSP 1.2 Processor]: Cannot format given Object as a Date

String yourDate=null;
String tPattern = "dd MMM yyyy";
SimpleDateFormat sdf = new SimpleDateFormat(tPattern);
sdf.applyPattern(tPattern);
String formattedDate =" ";


          if (rs_register.next())             
              {
                    yourDate=rs_register.getString("register_date");
                  }  

 formattedDate=sdf.format(yourDate);
out.print(formattedDate);
0
 
LVL 4

Expert Comment

by:Zonebit
ID: 9614341
saleem,

yourDate needs to be a Date object, not a String object.
0
 

Author Comment

by:saleemkhan
ID: 9614418
i tried like this,

Date yourdate=null;


i am getting the type date is ambiguous.
0
 
LVL 4

Expert Comment

by:Zonebit
ID: 9614431
Try:

java.util.Date yourdate = null;
0
 

Author Comment

by:saleemkhan
ID: 9614460


gettting error here

yourDate=rs_register.getString("register_date");

type mismatch cannot convert from java.lang.string to java.lang.date

0
 
LVL 4

Expert Comment

by:Zonebit
ID: 9614501
try:

yourDate = rs_register.getDate("register_date");
0
 

Author Comment

by:saleemkhan
ID: 9614521
i alreday tried

yourDate =rs_gegister.getDate("register_date")

the database column is varchar2  its not date type

so i have to use

yourDate= rs_register.getString("register_date");

0
 
LVL 4

Expert Comment

by:Zonebit
ID: 9614534
right...

yourDate = Date.valueOf(rs_register.getString("register_date"));
0
 

Author Comment

by:saleemkhan
ID: 9614573
java compile error

Date cannot be resolved

yourDate = Date.valueOf(rs_register.getString("register_date"));




0
 
LVL 4

Expert Comment

by:Zonebit
ID: 9614578
yourDate = java.util.Date.valueOf(rs_register.getString("register_date"));
0
 

Author Comment

by:saleemkhan
ID: 9614605


java compile

the method valueOf java.lang.String is undefined for the type

java.util.Date

0
 
LVL 4

Expert Comment

by:Zonebit
ID: 9614627
yourDate = new java.util.Date(rs_register.getString("register_date"));
0
 

Author Comment

by:saleemkhan
ID: 9614673
java compile

the constructor java.util.date(java.lang.String) is deprecated
0
 
LVL 4

Expert Comment

by:Zonebit
ID: 9614703
yourDate = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL).parse(rs_register.getString("register_date"));
0
 

Author Comment

by:saleemkhan
ID: 9614731
in compile no erros

when i run jsp and print it i am getting like this

Error 500: Unparseable date: "Sun Oct 19 09:05:45 GMT+03:00 2003"
0
 
LVL 4

Accepted Solution

by:
Zonebit earned 125 total points
ID: 9614782
tParsePattern = "EEE MMM dd HH:mm:ss zzz yyyy"
SimpleDateFormat ssdf = new SimpleDateFormat(tParsePattern);
yourDate = ssdf.parse(rs_register.getString("register_date"));
0
 

Author Comment

by:saleemkhan
ID: 9614936
i just tried like this

<%
java.util.Date yourDate = null;
String tParsePattern=null;
tParsePattern = "EEE MMM dd HH:mm:ss zzz yyyy";
SimpleDateFormat ssdf = new SimpleDateFormat(tParsePattern);
yourDate = ssdf.parse("Sun Oct 19 09:05:45 GMT+03:00 2003");
out.print(yourDate);
%>

i am getting error

Error 500: Unparseable date: "Sun Oct 19 09:05:45 GMT+03:00 2003"

0
 
LVL 2

Expert Comment

by:wellilein
ID: 9615713
According to the SDK JavaDoc, java.util.Date has the following methods:
getYear(), getMonth(), getDay()
You might try this:
java.util.Date date = new java.util.Date();
String s = date.getDay() + " " + date.getMonth() + " " + date.getYear();

But all these methods are deprecated. You should have a look at java.util.Calendar, which would be
java.util.Calendar cal = new java.util.Calendar();
String s = cal.get(Calendar.DAY_OF_MONTH) + " " + cal.get(Calendar.MONTH)+ " " +cal.get(Calendar.YEAR);
0
 

Author Comment

by:saleemkhan
ID: 9618958
i understand now where i am wrong.
the below code is not working why because in my server where i am executing code the regional setting month value is in arabic.
i executed the same code in a different server where date and time jones are english its working fine.
All the problem is in regional setting and not the jsp code.


tParsePattern = "EEE MMM dd HH:mm:ss zzz yyyy"
SimpleDateFormat ssdf = new SimpleDateFormat(tParsePattern);
yourDate = ssdf.parse(rs_register.getString("register_date"));
0

Featured Post

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Does your audience prefer people in photos or no people? How can you best highlight what you’re selling? What are your competitors doing, and what can you do that is different and unique from them?  Continue reading to learn how to make your images …
Dramatic changes are revolutionizing how we build and use technology. Every company is automating, digitizing, and modernizing operations. We need a better, more connected way to work together as teams so we can harness the insights from our system…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question