Title: Java SimpleDateFormat and DateFormat Example - Java Code Geeks
Open Graph Title: Java SimpleDateFormat and DateFormat Example - Java Code Geeks
Description: In this example we will show how to use java.text.SimpleDateFormat class so as to format date into text or parse text into date. SimpleDateFormat extends
Open Graph Description: In this example we will show how to use java.text.SimpleDateFormat class so as to format date into text or parse text into date. SimpleDateFormat extends
Mail addresses
support@javacodegeeks.com
Opengraph URL: https://examples.javacodegeeks.com/java-development/core-java/text/java-dateformat-example/
Generator: WordPress 6.9.4
Domain: examples.javacodegeeks.com
{"@context":"https://schema.org","@graph":[{"@type":"Article","@id":"https://examples.javacodegeeks.com/java-development/core-java/text/java-dateformat-example/#article","isPartOf":{"@id":"https://examples.javacodegeeks.com/java-development/core-java/text/java-dateformat-example/"},"author":{"name":"Konstantina Dimtsa","@id":"https://examples.javacodegeeks.com/#/schema/person/5fc06d4d50814931c15af68106832a56"},"headline":"Java SimpleDateFormat and DateFormat Example","datePublished":"2014-05-12T08:32:05+00:00","dateModified":"2023-11-09T12:34:57+00:00","mainEntityOfPage":{"@id":"https://examples.javacodegeeks.com/java-development/core-java/text/java-dateformat-example/"},"wordCount":472,"commentCount":1,"publisher":{"@id":"https://examples.javacodegeeks.com/#organization"},"image":{"@id":"https://examples.javacodegeeks.com/java-development/core-java/text/java-dateformat-example/#primaryimage"},"thumbnailUrl":"https://examples.javacodegeeks.com/wp-content/uploads/2012/12/java-logo.jpg","keywords":["DateFormat","SimpleDateFormat"],"articleSection":["text"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https://examples.javacodegeeks.com/java-development/core-java/text/java-dateformat-example/#respond"]}]},{"@type":"WebPage","@id":"https://examples.javacodegeeks.com/java-development/core-java/text/java-dateformat-example/","url":"https://examples.javacodegeeks.com/java-development/core-java/text/java-dateformat-example/","name":"Java SimpleDateFormat and DateFormat Example - Java Code Geeks","isPartOf":{"@id":"https://examples.javacodegeeks.com/#website"},"primaryImageOfPage":{"@id":"https://examples.javacodegeeks.com/java-development/core-java/text/java-dateformat-example/#primaryimage"},"image":{"@id":"https://examples.javacodegeeks.com/java-development/core-java/text/java-dateformat-example/#primaryimage"},"thumbnailUrl":"https://examples.javacodegeeks.com/wp-content/uploads/2012/12/java-logo.jpg","datePublished":"2014-05-12T08:32:05+00:00","dateModified":"2023-11-09T12:34:57+00:00","description":"In this example we will show how to use java.text.SimpleDateFormat class so as to format date into text or parse text into date. SimpleDateFormat extends","breadcrumb":{"@id":"https://examples.javacodegeeks.com/java-development/core-java/text/java-dateformat-example/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https://examples.javacodegeeks.com/java-development/core-java/text/java-dateformat-example/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https://examples.javacodegeeks.com/java-development/core-java/text/java-dateformat-example/#primaryimage","url":"https://examples.javacodegeeks.com/wp-content/uploads/2012/12/java-logo.jpg","contentUrl":"https://examples.javacodegeeks.com/wp-content/uploads/2012/12/java-logo.jpg","width":150,"height":150,"caption":"Bipartite Graph"},{"@type":"BreadcrumbList","@id":"https://examples.javacodegeeks.com/java-development/core-java/text/java-dateformat-example/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https://examples.javacodegeeks.com/"},{"@type":"ListItem","position":2,"name":"Java Development","item":"https://examples.javacodegeeks.com/category/java-development/"},{"@type":"ListItem","position":3,"name":"Core Java","item":"https://examples.javacodegeeks.com/category/java-development/core-java/"},{"@type":"ListItem","position":4,"name":"text","item":"https://examples.javacodegeeks.com/category/java-development/core-java/text/"},{"@type":"ListItem","position":5,"name":"Java SimpleDateFormat and DateFormat Example"}]},{"@type":"WebSite","@id":"https://examples.javacodegeeks.com/#website","url":"https://examples.javacodegeeks.com/","name":"Java Code Geeks","description":"Java Examples and Code Snippets","publisher":{"@id":"https://examples.javacodegeeks.com/#organization"},"alternateName":"JCG","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https://examples.javacodegeeks.com/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https://examples.javacodegeeks.com/#organization","name":"Exelixis Media P.C.","url":"https://examples.javacodegeeks.com/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https://examples.javacodegeeks.com/#/schema/logo/image/","url":"https://examples.javacodegeeks.com/wp-content/uploads/2022/06/exelixis-logo.png","contentUrl":"https://examples.javacodegeeks.com/wp-content/uploads/2022/06/exelixis-logo.png","width":864,"height":246,"caption":"Exelixis Media P.C."},"image":{"@id":"https://examples.javacodegeeks.com/#/schema/logo/image/"},"sameAs":["https://www.facebook.com/javacodegeeks","https://x.com/javacodegeeks"]},{"@type":"Person","@id":"https://examples.javacodegeeks.com/#/schema/person/5fc06d4d50814931c15af68106832a56","name":"Konstantina Dimtsa","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https://examples.javacodegeeks.com/#/schema/person/image/","url":"https://examples.javacodegeeks.com/wp-content/uploads/2013/10/Konstantina-Dimtsa-96x96.jpg","contentUrl":"https://examples.javacodegeeks.com/wp-content/uploads/2013/10/Konstantina-Dimtsa-96x96.jpg","caption":"Konstantina Dimtsa"},"description":"Konstantina has graduated from the Department of Informatics and Telecommunications in National and Kapodistrian University of Athens (NKUA) and she is currently pursuing M.Sc studies in Advanced Information Systems at the same department. She is also working as a research associate for NKUA in the field of telecommunications. Her main interests lie in software engineering, web applications, databases and telecommunications.","sameAs":["http://www.javacodegeeks.com"],"url":"https://examples.javacodegeeks.com/author/konstantina-dimtsa/"}]}
{"@context":"http:\/\/schema.org","@type":"BreadcrumbList","@id":"#Breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"item":{"name":"Home","@id":"https:\/\/examples.javacodegeeks.com\/"}},{"@type":"ListItem","position":2,"item":{"name":"Java Development","@id":"https:\/\/examples.javacodegeeks.com\/category\/java-development\/"}},{"@type":"ListItem","position":3,"item":{"name":"Core Java","@id":"https:\/\/examples.javacodegeeks.com\/category\/java-development\/core-java\/"}},{"@type":"ListItem","position":4,"item":{"name":"text","@id":"https:\/\/examples.javacodegeeks.com\/category\/java-development\/core-java\/text\/"}}]}
{"@context":"http:\/\/schema.org","@type":"Article","dateCreated":"2014-05-12T11:32:05+03:00","datePublished":"2014-05-12T11:32:05+03:00","dateModified":"2023-11-09T14:34:57+02:00","headline":"Java SimpleDateFormat and DateFormat Example","name":"Java SimpleDateFormat and DateFormat Example","keywords":"DateFormat,SimpleDateFormat","url":"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/text\/java-dateformat-example\/","description":"In this example we will show how to use java.text.SimpleDateFormat class so as to format date into text or parse text into date. SimpleDateFormat extends the java.text.DateFormat class which is an abs","copyrightYear":"2014","articleSection":"text","articleBody":"In this example we will show how to use java.text.SimpleDateFormat class so as to format date into text or parse text into date. SimpleDateFormat extends the java.text.DateFormat class which is an abstract class for date\/time formatting subclasses and provides many class methods for obtaining default date\/time formatters based on any given locale. We will see the usage of the class DateFormat, too.\n1. Example of SimpleDateFormat\nCreate a java class named SimpleDateFormatExample.java with the following code:\nSimpleDateFormatExample.java\npackage com.javacodegeeks.corejava.text;\n\nimport java.text.ParseException;\nimport java.text.SimpleDateFormat;\nimport java.util.Date;\nimport java.util.Locale;\n\npublic class SimpleDateFormatExample {\n\tpublic static void main(String[] args) {\n\n\t\tDate curDate = new Date();\n\n\t\tSimpleDateFormat format = new SimpleDateFormat(\"yyyy\/MM\/dd\");\n\n\t\tString DateToStr = format.format(curDate);\n\t\tSystem.out.println(DateToStr);\n\n\t\tformat = new SimpleDateFormat(\"dd-M-yyyy hh:mm:ss\");\n\t\tDateToStr = format.format(curDate);\n\t\tSystem.out.println(DateToStr);\n\n\t\tformat = new SimpleDateFormat(\"dd MMMM yyyy zzzz\", Locale.ENGLISH);\n\t\tDateToStr = format.format(curDate);\n\t\tSystem.out.println(DateToStr);\n\n\t\tformat = new SimpleDateFormat(\"E, dd MMM yyyy HH:mm:ss z\");\n\t\tDateToStr = format.format(curDate);\n\t\tSystem.out.println(DateToStr);\n\n\t\ttry {\n\t\t\tDate strToDate = format.parse(DateToStr);\n\t\t\tSystem.out.println(strToDate);\n\t\t} catch (ParseException e) {\n\t\t\te.printStackTrace();\n\t\t}\n\t}\n}\n\nLet's explain the different formats of SimpleDateFormat class in the above code. Firstly, we create a Date object which is initialized with the current date and time. Then, we create different date formatters with different patterns, such as:\n\n \tyyyy\/MM\/dd, which shows the date in the form of year\/month\/day. As we can observe, the pattern for the year has 4 letters, which means that the full form of the year will be used (e.g. 2014). Otherwise a short or abbreviated form is used if available.\n \tdd-M-yyyy hh:mm:ss, which shows the date in the form of date-month-year (the month will be shown in the abbreviated form, as it has only one letter and not two as in the previous case) and futhermore, it shows the time (hour, minutes and seconds) while the hour is in am\/pm format.\n \tdd MMMM yyyy zzzz, which shows the date and the timezone in full format. We can observe that we also defined the locale of the date\/time: Locale.ENGLISH.\n \tE, dd MMM yyyy HH:mm:ss z, which shows the date, the day name of the week and the time (we can see that the hour is in capital, which means that the hour's values here are between 0 - 23, as we use the 24-hour clock).\n\nUsing all those formatters, we format dates as strings. Finally, we show a reverse example, where we parse a string into date.\n\nFor a detailed explanation of the different existing patterns you can visit the the java doc SimpleDateFormat.\n\nIf we run the above code, we will have the following results:\n\n \tOutput:\n\n2014\/05\/11\n11-5-2014 11:11:51\n11 May 2014 Eastern European Summer Time\nSun, 11 May 2014 23:11:51 EEST\nSun May 11 23:11:51 EEST 2014\n\n2. Example of DateFormat\nCreate a java class named DateFormatExample.java with the following code:\n\nDateFormatExample.java\npackage com.javacodegeeks.corejava.text;\n\nimport java.text.DateFormat;\nimport java.text.ParseException;\nimport java.util.Date;\n\npublic class DateFormatExample {\n\tpublic static void main(String[] args) {\n\n\t\tDate curDate = new Date();\n\n\t\tSystem.out.println(curDate.toString());\n\n\t\tString DateToStr = DateFormat.getInstance().format(curDate);\n\t\tSystem.out.println(DateToStr);\n\n\t\tDateToStr = DateFormat.getTimeInstance().format(curDate);\n\t\tSystem.out.println(DateToStr);\n\n\t\tDateToStr = DateFormat.getDateInstance().format(curDate);\n\t\tSystem.out.println(DateToStr);\n\n\t\tDateToStr = DateFormat.getDateTimeInstance().format(curDate);\n\t\tSystem.out.println(DateToStr);\n\n\t\tDateToStr = DateFormat.getTimeInstance(DateFormat.SHORT)\n\t\t\t\t.format(curDate);\n\t\tSystem.out.println(DateToStr);\n\n\t\tDateToStr = DateFormat.getTimeInstance(DateFormat.MEDIUM).format(\n\t\t\t\tcurDate);\n\t\tSystem.out.println(DateToStr);\n\n\t\tDateToStr = DateFormat.getTimeInstance(DateFormat.LONG).format(curDate);\n\t\tSystem.out.println(DateToStr);\n\n\t\tDateToStr = DateFormat.getDateTimeInstance(DateFormat.MEDIUM,\n\t\t\t\tDateFormat.SHORT).format(curDate);\n\t\tSystem.out.println(DateToStr);\n\n\t\ttry {\n\t\t\tDate strToDate = DateFormat.getDateInstance()\n\t\t\t\t\t.parse(\"July 17, 1989\");\n\t\t\tSystem.out.println(strToDate.toString());\n\t\t} catch (ParseException e) {\n\t\t\te.printStackTrace();\n\t\t}\n\n\t}\n}\n\nLet's explain the above code. Firstly, we create a Date object which is initialized with the current date and time. Then we use the default DateFormat by using the method getInstance() and the default time, date and date-time DateFormats by using the methods getTimeInstance(), getDateInstance(), getDateTimeInstance(), respectively. Then, we can control the length of the time format by using styles such as LONG and MEDIUM in the default Time DateFormat. Also, we define the date and time format in the default date-time DateFormat. Finally, we show an example of parsing a string in date, using the default date DateFormat.\n\nIf we run the above code, we will have the following results:\n\n \tOutput:\n\nSun May 11 23:37:54 EEST 2014\n5\/11\/14 11:37 PM\n11:37:54 PM\nMay 11, 2014\nMay 11, 2014 11:37:54 PM\n11:37 PM\n11:37:54 PM\n11:37:54 PM EEST\nMay 11, 2014 11:37 PM\nMon Jul 17 00:00:00 EEST 1989\n\n3. Download the source code\nThis was an example of how to use the classes DateFormat and SimpleDateFormat.\nYou can download the source code of this example from here: DateFormatExample.zip","publisher":{"@id":"#Publisher","@type":"Organization","name":"Examples Java Code Geeks","logo":{"@type":"ImageObject","url":"\/wp-content\/uploads\/2013\/05\/JavaCodeGeeks-logo.png"},"sameAs":["https:\/\/feeds.feedburner.com\/examplesjavacodegeeks","https:\/\/www.facebook.com\/javacodegeeks","https:\/\/twitter.com\/javacodegeeks","https:\/\/www.linkedin.com\/groups\/3810709\/","https:\/\/www.youtube.com\/channel\/UCxoUc7Rar2q90Gu0nT2ffuQ","https:\/\/github.com\/javacodegeeks\/"]},"sourceOrganization":{"@id":"#Publisher"},"copyrightHolder":{"@id":"#Publisher"},"mainEntityOfPage":{"@type":"WebPage","@id":"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/text\/java-dateformat-example\/","breadcrumb":{"@id":"#Breadcrumb"}},"author":{"@type":"Person","name":"Konstantina Dimtsa","url":"https:\/\/examples.javacodegeeks.com\/author\/konstantina-dimtsa\/"},"image":{"@type":"ImageObject","url":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/java-logo.jpg","width":1200,"height":150}}
| og:locale | en_US |
| og:type | article |
| og:site_name | Examples Java Code Geeks |
| article:publisher | https://www.facebook.com/javacodegeeks |
| article:published_time | 2014-05-12T08:32:05+00:00 |
| article:modified_time | 2023-11-09T12:34:57+00:00 |
| og:image | https://examples.javacodegeeks.com/wp-content/uploads/2012/12/java-logo.jpg |
| og:image:width | 150 |
| og:image:height | 150 |
| og:image:type | image/jpeg |
| author | Konstantina Dimtsa |
| twitter:card | summary_large_image |
| twitter:creator | @javacodegeeks |
| twitter:label1 | Written by |
| twitter:data1 | Konstantina Dimtsa |
| twitter:label2 | Est. reading time |
| twitter:data2 | 3 minutes |
| None | IE=edge |
| msapplication-TileImage | https://examples.javacodegeeks.com/wp-content/uploads/2013/05/JavaCodeGeeks-favIcon.png |
Links:
Viewport: width=device-width, initial-scale=1.0
Robots: index, follow, max-image-preview:large, max-snippet:-1, max-video-preview:-1