Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Convert String to Date

 
Frank Albright
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can someone tell me what method will convert a String to a Date?
Thanks,
Frank
 
Todd M Bush
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The java.util.Date constructor will take a String. It will also take a year, month, day. (hour, min, and sec as well)
Date(String date)
Date(int year, int month, int day)
 
Carl Trusiak
Sheriff
Posts: 3341
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This method has been deprecated. To create a Date object from a String object you have to use SimpleDateFormat parse method.
SimpleDateFormat sdfinDate = new SimpleDateFormat("MM/dd/yyyy");
java.util.Date actualDate = null;

try
{
sdfinDate.setLenient(true);
actualDate = sdfinDate.parse("1/2/99");
}
catch(ParseException pe)
{
//handle exception
}
 
paul wheaton
Trailboss
Pie
Posts: 21598
Firefox Browser IntelliJ IDE Java
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Eh?
Hoooooo Doggey - where ya headed pardner?
We don't seem to have the page yer lookin' fer.
Mebbe we're callin' it somethin else now.
We suggest you mosey on over to the
JavaRanch home page
 
Ajay Kumar
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jim,
I think it has to do with Paul's obsession with punctuation. Just remove the last period after "...common.html" and U are right there.
www.javaranch.com/common.html
Ajay
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic