*
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 | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
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: 79
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: 24183
    
  34

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: 19684
    
  20

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 - OCEEJBD 6
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: 79
thank you very much...!
I understood the problem.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

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: 3344

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: 79
Sorry...! I did'nt understand...!

I meant "you helped me find a solution".
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problem converting a String to java.util.Date