Convert date string from one format to another format using SimpleDateFormat

  1. /*
  2.   Convert date string from one format to another format using SimpleDateFormat
  3.   This example shows how to convert format of a string containing date
  4.   and time to other formats using Java SimpleDateFormat class.
  5. */
  6.  
  7. import java.util.Date;
  8. import java.text.ParseException;
  9. import java.text.SimpleDateFormat;
  10.  
  11. public class ConvertDateFormats {
  12.  
  13. public static void main(String[] args) {
  14.  
  15. //string containing date in one format
  16. String strDate = "12/12/07";
  17.  
  18. try
  19. {
  20. //create SimpleDateFormat object with source string date format
  21. SimpleDateFormat sdfSource = new SimpleDateFormat("dd/MM/yy");
  22.  
  23. //parse the string into Date object
  24. Date date = sdfSource.parse(strDate);
  25.  
  26. //create SimpleDateFormat object with desired date format
  27. SimpleDateFormat sdfDestination = new SimpleDateFormat("MM-dd-yyyy hh:mm:ss");
  28.  
  29. //parse the date into another format
  30. strDate = sdfDestination.format(date);
  31.  
  32. System.out.println("Date is converted from dd/MM/yy format to MM-dd-yyyy hh:mm:ss");
  33. System.out.println("Converted date is : " + strDate);
  34.  
  35. }
  36. catch(ParseException pe)
  37. {
  38. System.out.println("Parse Exception : " + pe);
  39. }
  40. }
  41. }
  42.  
  43. /*
  44. Typical output would be
  45. Date is converted from dd/MM/yy format to MM-dd-yyyy hh:mm:ss
  46. Converted date is : 12-12-2007 12:00:00
  47. */

Visit Java Example Forums to request Java examples or ask Java questions!
OR
Try out our new Java Search Engine




Suggested Reading

Oracle Magazine
Contains technology strategy articles, sample code, tips, Oracle and partner news, how to articles for developers and DBAs, and more.
Cost: FREE

View/Subscribe
NASA Tech Briefs
Features exclusive reports of innovations developed by NASA and its industry partners/contractors that can be applied to develop new/improved products and solve engineering or manufacturing problems.
Cost: FREE

View/Subscribe
FierceBiotech IT
Is a free, easy to read weekly email service that brings must read biotech IT news to senior biotech, pharma, and IT executives.
Cost: FREE

View/Subscribe
Simply SQL
Simply SQL is a practical step-by-step guide to writing SQL.
Cost: FREE

View/Subscribe
Simply JavaScript
Packed with full-color examples, Simply JavaScript is all you need to start programming in JavaScript the right way.
Cost: FREE

View/Subscribe
PCMag.com's What's New Now
Lance Ulanoff, Editor in Chief of the PC Magazine Network, brings you this twice-weekly roundup of the latest top tech stories, the best new product reviews, plus special offers from Ziff Davis and its partners.
Cost: FREE

View/Subscribe

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


Could not find what you are looking for? Search Java Examples




Feel Tired? Read Jokes & Inspirational Stories, Play Games!