aspose file tools
The moose likes Beginning Java and the fly likes Problem converting a String to java.util.Date Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "Problem converting a String to java.util.Date" Watch "Problem converting a String to java.util.Date" New topic
Author

Problem converting a String to java.util.Date

Nitin Menon
Ranch Hand

Joined: Jun 13, 2007
Posts: 78
Hi all,
I am facing some problem with converting a String to java.util.Date format. I gave the below code and I got the error :


C:\Users\Nitin Ramachandran\Java Pgms>javac DateFormat1.java
DateFormat1.java:17: unreported exception java.lang.Exception; must be caught or declared to be thrown
new DateFormat1();
^
1 error

The Code I gave is :

import java.text.*;
import java.util.*;

public class DateFormat1
{
public DateFormat1() throws Exception
{
String dateString = "2001/03/09";

SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/mm/dd");
Date convertedDate = dateFormat.parse(dateString);
System.out.println("Converted string to date : " + convertedDate);
}

public static void main(String[] argv)
{
new DateFormat1();
}
}

Can anyone help me please? Thank you in advance...
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24061
    
  13

Your DateFormat1 constructor says "throws Exception". That means* that you have to use it inside a "try" block like this:

[code]
try {
new DateFormat1();
} catch (Exception ex) {
ex.printStackTrace();
}

(* oversimplified, yes, I know.)


[Jess in Action][AskingGoodQuestions]
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19216

But you shouldn't just throw Exception. java.text.ParseException is the actual exception that can be thrown inside your constructor, so you should also declare to throw that one.


SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
Meet Gaurav
Ranch Hand

Joined: Oct 08, 2008
Posts: 492
You are suppose to throw the same exception or super class of that one in the main method also. Try this one

import java.text.*;
import java.util.*;

public class DateFormat1
{
public DateFormat1() throws Exception
{
String dateString = "2001/03/09";

SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/mm/dd");
Date convertedDate = dateFormat.parse(dateString);
System.out.println("Converted string to date : " + convertedDate);
}

public static void main(String[] argv) throws Exception
{
new DateFormat1();
}
}



cheers
Nitin Menon
Ranch Hand

Joined: Jun 13, 2007
Posts: 78
thank you very much...!
I understood the problem.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24061
    
  13

Originally posted by Meet Gaurav:
You are suppose to throw the same exception or super class of that one in the main method also.


That's an ugly hack; please ignore this suggestion.
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3327

Originally posted by Ernest Friedman-Hill:


That's an ugly hack; please ignore this suggestion.


Yes, because the terms 'Exception Handling' makes more sense rather than 'Exception rethrowing'. Isn't it?


Everything has got its own deadline including one's EGO!
[CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
Nitin Menon
Ranch Hand

Joined: Jun 13, 2007
Posts: 78
Sorry...! I did'nt understand...!

I meant "you helped me find a solution".
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Problem converting a String to java.util.Date
 
Similar Threads
string to date
Converting From String to java.sql.Date
Parsing dates
Convert date in difference format
SimpleDateFormat not returning expectec output?