/*
Convert date string from one format to another format using SimpleDateFormat
This example shows how to convert format of a string containing date
and time to other formats using Java SimpleDateFormat class.
*/
import java.util.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
public class ConvertDateFormats {
public static void main(String[] args) {
//string containing date in one format
String strDate = "12/12/07";
try
{
//create SimpleDateFormat object with source string date format
SimpleDateFormat sdfSource = new SimpleDateFormat("dd/MM/yy");
//parse the string into Date object
Date date = sdfSource.parse(strDate);
//create SimpleDateFormat object with desired date format
SimpleDateFormat sdfDestination = new SimpleDateFormat("MM-dd-yyyy hh:mm:ss");
//parse the date into another format
strDate = sdfDestination.format(date);
System.out.println("Date is converted from dd/MM/yy format to MM-dd-yyyy hh:mm:ss");
System.out.println("Converted date is : " + strDate);
}
catch(ParseException pe)
{
System.out.println("Parse Exception : " + pe);
}
}
}
/*
Typical output would be
Date is converted from dd/MM/yy format to MM-dd-yyyy hh:mm:ss
Converted date is : 12-12-2007 12:00:00
*/
Bookmark/Search this post with:
Convert Date from string to Date
Hello,
I have a String which holds the Date. the format of the Date is unknown and it can be in different format.
i would like to convert the This from any one format to any Other format.
The format to which i convert is known but i dont know the format in which the string value is.
Please help me resolve.
Hmm
Try to determine the format of the source date format using regular expressions. Once determined, you can convert it to any format you want!
Thanks
Hi,
I had a similar problem. Thanks for helping me out.
Sandesh.S
Post new comment