Solved

Converting a String in Date format

Posted on 2004-04-23
19
2,156 Views
Last Modified: 2010-04-01
How convert a String getting in input from a jsp page and formatting in a date format. I'm using JDev and Oracle9. Thanks  (PS: Sorry for my english!!, I'm italian)
0
Comment
Question by:MissBrucoMela
  • 8
  • 5
  • 4
  • +2
19 Comments
 
LVL 9

Expert Comment

by:Nick_72
ID: 10897386
What date format do you need and what value does the String have (for example)?
0
 

Author Comment

by:MissBrucoMela
ID: 10897574
date format is (DD/MM/YYYY), the value in input is a String, for example: "22/04/2004"
0
 
LVL 35

Expert Comment

by:TimYates
ID: 10897710
java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat( "dd/MM/yyyy" ) ;

String inputDate = "10/05/1974" ;
try
{
    Date date = sdf.parse( inputDate ) ;
}
catch( java.text.ParseException ex )
{
    // invalid date :-)
}
0
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
LVL 9

Expert Comment

by:Nick_72
ID: 10898146
You should use the setLenient() method too, otherwise non-valid dates, or dates which does not match the objects format might be parsed without error:

java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat( "dd/MM/yyyy" ) ;
sdf.setLenient(false); <<-- add this line
0
 
LVL 7

Expert Comment

by:searlas
ID: 10898847
Even with setLenient(false) it still accepts single digit day/month.
java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat( "dd/MM/yyyy" ) ;
sdf.setLenient(false);
sdf.parse("1/5/2004");
sdf.parse("01/5/2004");
sdf.parse("1/05/2004");
sdf.parse("01/05/2004");

I'm undecided whether that's a good thing or a bad thing.
0
 
LVL 9

Expert Comment

by:Nick_72
ID: 10899031
That is just different representation of an int value, however try these invalid dates with and without
setLenient(false);

sdf.parse("01/13/2004");
sdf.parse("29/02/2003");
0
 
LVL 9

Expert Comment

by:Nick_72
ID: 10899046
without the setLenient(false), the parse() call will not throw a parseException.
0
 
LVL 35

Expert Comment

by:TimYates
ID: 10899103
> I'm undecided whether that's a good thing or a bad thing.

Good thing

;)
0
 

Author Comment

by:MissBrucoMela
ID: 10900186
I'm sorry...I have been wrong... I must use java.sql.Date and  not java.util.Date!
How can I do?
0
 
LVL 35

Expert Comment

by:TimYates
ID: 10900205
String inputDate = "10/05/1974" ;
try
{
    java.sql.Date date = new java.sql.Date( sdf.parse( inputDate ).getTime() ) ;
}
catch( java.text.ParseException ex )
{
    // invalid date :-)
}
0
 

Author Comment

by:MissBrucoMela
ID: 10900443
Don't works!
I have a error message:
"java.lang.NullPointerException
      int java.text.SimpleDateFormat.subParse(java.lang.String, int, char, int, boolean, boolean[]) SimpleDateFormat.java:967......"

If I pass the value String from the jsp page don't works, but if I pass the value with the string "String inputDate="16/03/1974", It works... I don't Know...
0
 
LVL 35

Expert Comment

by:TimYates
ID: 10900535
Because you are getting null from getParameter

String param = request.getParameter( "whatever" ) ;

param is null...

they didn't enter a date...
0
 
LVL 35

Accepted Solution

by:
TimYates earned 50 total points
ID: 10900554
If I am not right, you are going to have to post a bit of your jsp from where you get the parameter, to where you format the string into a date..

check you have the parameter name right (if you are using getParameter)

if it is null, then skip the Date creation bit of code, and remind the user to enter a date...
0
 

Author Comment

by:MissBrucoMela
ID: 10900608
I'm so stupid...you're right!!!Thanks a lot!
0
 
LVL 35

Expert Comment

by:TimYates
ID: 10900642
:-)
0
 
LVL 35

Expert Comment

by:TimYates
ID: 10900648
Only a B grade tho *sniff* :-(
0
 

Author Comment

by:MissBrucoMela
ID: 10917771
I'm a newbie for experts-exanchange. next time you'll have the highest grade.I promise
0
 
LVL 35

Expert Comment

by:TimYates
ID: 10917778
:-)  Ta :-)

Good luck!!
0
 

Expert Comment

by:stevenwarner
ID: 11153968
Does anybody have a piece of code that will remove the padded zero from a date that I can place in a JSP page?

Input : May 05, 2004  
Output:  May 5, 2004
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
rebasing and merging used in SCM 1 72
gZip compression filter 2 143
How to Make JSP Pages Responsive ? details are below 3 736
struts hibernate integration example 28 132
One of the biggest threats facing all high-value targets are APT's.  These threats include sophisticated tactics that "often starts with mapping human organization and collecting intelligence on employees, who are nowadays a weaker link than network…
Google Drive is extremely cheap offsite storage, and it's even possible to get extra storage for free for two years.  You can use the free account 15GB, and if you have an Android device..when you install Google Drive for the first time it will give…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

813 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now