1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
/* 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 */ |
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.
Try to determine the format of the source date format using regular expressions. Once determined, you can convert it to any format you want!
Hi,
I had a similar problem. Thanks for helping me out.
Sandesh.S
hey thanks a lot..this example was very useful.
I know of a few values I can pass into this method that will never throw an exception AND will never convert to the right date. As long as you have the two forward slashes in your string date variable, then MM, dd and yyyy can be of any numeric value.
SimpleDateFormat class has a list of date/time formats.Select one among them which is perfect match to you.
thanks a lot
thanks so helpful
thanxx for the help
Thanks !!!! This example helped me a lot !
Thank you.. It was very helpfull..
Thank you Sir!!
I really needed this .worked as per my need .Thanks
Thank U Sir!!
I really needed the Same coding for my project,It really solved my problem.
Thank you so much. Really useful.
Thank you… its very use full to java learners like me……