• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem converting a String to java.util.Date

 
Nitin Menon
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.)
 
Rob Spoor
Sheriff
Pie
Posts: 20512
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Meet Gaurav
Ranch Hand
Posts: 492
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you very much...!
I understood the problem.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3381
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Nitin Menon
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry...! I did'nt understand...!

I meant "you helped me find a solution".
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic