Convert String to java.sql.Date and java.sql.Time
If I have a method like this:
public static String convertDateTimeToString(DateTime dt) {
return dt.getDate() + " " + dt.getTime();
}
Which takes a DateTime
object of my own which contains a java.sql.Date
and a java.sql.Time
, what is the best way of reversing the process so that I can substring a java.sql.Date
and a java.sql.Time
from a String?
Or if DateTime dt
is a JodaTime DateT开发者_JS百科ime
object?
If this can be done without reference to java.util.Date
.
Look at SimpleDateFormat class. Here's a tutorial.
You'll need something like this:
String datetimeString;
Date result;
SimpleDateFormat formatter = new SimpleDateFormat("EEE d MMM yy hh:mm:ss");
result = formatter.parse (datetimeString);
You can use the valueOf method of java.sql.Time to convert a String into a java.sql.Time, and use the valueOf method of java.sql.Date to convert a String into a java.sql.Date
精彩评论