{"id":69173,"date":"2020-11-07T07:07:34","date_gmt":"2020-11-07T07:07:34","guid":{"rendered":"https:\/\/www.fita.in\/?p=69173"},"modified":"2023-10-09T12:44:54","modified_gmt":"2023-10-09T12:44:54","slug":"date-format-in-java","status":"publish","type":"post","link":"https:\/\/www.fita.in\/date-format-in-java\/","title":{"rendered":"Date class In Java: Formatting dates in java"},"content":{"rendered":"
<\/div>\r\n
<\/div>\r\nIn this blog, we will learn about the date class of the java, customizing the date formats along with the example programs, with this table of content\u2026\r\n
\r\n
\r\n\r\n<\/i> Date Class in java\r\n
\r\n
<\/i> Constructors of the date class<\/div>\r\n
<\/i> Methods of the date class<\/div>\r\n<\/div>\r\n<\/div>\r\n
\r\n\r\n<\/i> Formatting dates with the DateFormat class\r\n
\r\n
<\/i> Converting date to a string in java<\/div>\r\n<\/div>\r\n<\/div>\r\n
\r\n\r\n<\/i> Formatting dates with SimpleDateFormat class\r\n
\r\n
<\/i> Converting a string to date in java<\/div>\r\n<\/div>\r\n<\/div>\r\n
\r\n\r\n<\/i> Formatting dates with DateTimeFormatter class\r\n
\r\n
<\/i> Converting date to custom format with off pattern() method<\/div>\r\n<\/div>\r\n<\/div>\r\n<\/div>\r\nLet us start with understanding the Date class and Date objects in java\r\n

Date Class in java<\/strong><\/h3>\r\n

Date in java can be represented as an object of the class, Date, which is present at the java.util <\/em>package implementing the comparable interface, serializable interface, and cloneable interface.<\/p>\r\nThere are methods and constructors available to customize the data and time in java.\r\n\r\nConstructors of the date class<\/strong>\r\n

\r\n
<\/i> Date()<\/strong>: Creates an instance of the Date Class for representing the current date and time.<\/div>\r\n
<\/i> Date(long milliseconds)<\/strong>: Creates an instance of the Date Class for the given milliseconds.<\/div>\r\n<\/div>\r\nLet us now see these constructors in action, by implementing them in an example program.\r\n
\r\n
\r\n\/\/ Java program for demonstrating the constructors of the Date Class\r\nimport java.util.*;\r\n \r\npublic class Main {\r\npublic static void main(String[] args) {\r\nDate dt1 = new Date();\r\nSystem.out.println(\"Today's date is \" + dt1);\r\nDate dt2 = new Date(2323223233L);\r\nSystem.out.println(\"GIven data is represented as \" + dt2);\r\n}\r\n}\r\n<\/code><\/pre>\r\n<\/div>\r\nOutput for the above program will be\r\n
\r\n
\r\nToday's date is Fri Oct 09 13:27:13 UTC 2020\r\nGIven data is represented as Tue Jan 27 21:20:23 UTC 1970\r\n<\/code><\/pre>\r\n<\/div>\r\nMethods of the date class<\/strong>\r\n
\r\n
<\/i> boolean after(Date date): <\/strong>Checks for the given date is earlier than the current data.<\/div>\r\n
<\/i> boolean before(Date date): <\/strong>Checks for the given date if it comes after the current data.<\/div>\r\n
<\/i> int compareTo(Date date): <\/strong>Compares the given date with the current date.<\/div>\r\n
\r\n
<\/i> Returns 0, if the passed Date is equal to the\u00a0 Date object.<\/div>\r\n
<\/i> A positive value, if the passed Date comes before the\u00a0 Date object.<\/div>\r\n
<\/i> A negative value, if the passed Date comes after the\u00a0 Date object.<\/div>\r\n<\/div>\r\n
<\/i> void setTime(long time)<\/strong>: Changes the current date and time to a given time.<\/div>\r\n<\/div>\r\nLet us now see these methods in action, by implementing them in an example program.\r\n
\r\n
\r\n\/\/ Example program for demonstrating methods of Date class\r\nimport java.util.*;\r\n \r\npublic class Main {\r\npublic static void main(String[] args) {\r\n\/\/ Creating dates\r\nDate dt1 = new Date(2003, 12, 22);\r\nDate dt2 = new Date();\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \/\/ Today's date\r\nDate dt3 = new Date(2010, 12, 7);\r\n \r\nboolean afr = dt1.after(dt2);\r\nSystem.out.println(\"Date 2003\/12\/22 comes after \" + \"today's date: \" + afr);\r\n \r\nboolean bfr = dt2.before(dt3);\r\nSystem.out.println(\"Today's date comes before \" + \"date 2010\/12\/7: \" + bfr);\r\n \r\nint com = dt3.compareTo(dt2);\r\nSystem.out.println(com);\r\n \r\nSystem.out.println(\" Date Before setting: \" + dt2);\r\ndt2.setTime(204587433443L);\r\nSystem.out.println(\"Date After setting with setTime: \" + dt2);\r\n}\r\n}\r\n<\/code><\/pre>\r\n<\/div>\r\nOutput for the above program will be\r\n
\r\n
\r\nDate 2003\/12\/22 comes after today's date: true\r\nToday's date comes before date 2010\/12\/7: true\r\n1\r\nDate Before setting: Fri Oct 09 13:38:52 UTC 2020\r\nDate After setting with setTime: Fri Jun 25 21:50:33 UTC 1976\r\n<\/code><\/pre>\r\n<\/div>\r\n

Now customizing these date formats will require us to use the DateFormat class, the SimpleDateFormat class, or the offpattern() method of the DateTimeFormatter class. So let us see each of them one by one in the following sequence<\/p>\r\n\r\n

\r\n
<\/i> Formatting dates with the DateFormat class.<\/div>\r\n
<\/i> Formatting dates with SimpleDateFormat class.<\/div>\r\n
<\/i> Formatting dates with DateTimeFormatter class.<\/div>\r\n<\/div>\r\nLet us format dates with each of these classes, starting with the DateFormat class<\/em>.\r\n

Formatting dates with the DateFormat class in java<\/strong><\/h3>\r\n

The DateFormat class is an asynchronous class,\u00a0 present at java.text.DateFormat.This class provides various methods to format dates and also to parse the dates to a String.<\/p>\r\nLet us see an example for implementing the DateFormat class.\r\n\r\nConverting Date to a String in java\r\n\r\nLet use the DateFormat class, for converting a given date to a string.\r\n

\r\n
\r\n\/\/ Example Program to format date with the DateFormat class\r\nimport java.util.*;\r\nimport java.text.*;\r\nimport java.util.Calendar;\r\n \r\npublic class Main {\r\npublic static void main(String[] args) {\r\n \r\nDateFormat Date = DateFormat.getDateInstance();\/\/ date formatter\r\n \r\nCalendar cal = Calendar.getInstance(); \/\/ calender object\r\n \r\nSystem.out.println(\"The actual Date: \" + cal.getTime());\r\n \r\n\/\/ Using format() method of DateFormat for converting date to string\r\nString ForD = Date.format(cal.getTime());\r\nSystem.out.println(\"Formatted Date: \" + ForD);\r\n}\r\n}\r\n<\/code><\/pre>\r\n<\/div>\r\nOutput for the above program\r\n
\r\n
\r\nThe actual Date: Fri Oct 09 13:55:28 UTC 2020\r\nFormatted Date: Oct 9, 2020\r\n<\/code><\/pre>\r\n<\/div>\r\nAfter learning to format dates with the DateFormat <\/em>class, let us now learn to format dates with the SimpleDateFormat <\/em>class in java.\r\n

Check out this Complete Java Online Training<\/a> by FITA<\/a>. FITA provides a complete Java course including core java and advanced java J2EE, and SOA training, where you will be building real time applications using Servlets, Hibernate Framework,<\/em> and Spring <\/em>with Aspect-Oriented Programming (AOP) architecture, Struts through JDBC<\/em> bundled with, placement support, and certification at an affordable price with an active placement cell, by expert software developers with over 10 years of experience in the field to make you an industry required certified java developer.<\/p>\r\n\r\n

Formatting dates with the SimpleDateFormat class in java<\/strong><\/h3>\r\nThis is the child class of DateFormat class which takes a String argument that should specify the pattern or formatting pattern of the date. The years should be represented by \u2018yyyy<\/em>\u2019,\u00a0 month by \u2018MM<\/em>\u2019 and the date by \u2018dd\u2019.\r\n\r\nLet us implement the SimpleDateFormat class with an example program.\r\n

Converting String to a Date in java<\/strong><\/h3>\r\n
\r\n
\r\n\/\/ Example Program to format date with the DateFormat class\r\nimport java.util.*;\r\nimport java.text.ParseException;\r\nimport java.text.SimpleDateFormat;\r\nimport java.util.Date;\r\n \r\npublic class Main {\r\npublic static void main(String[] args) {\r\nString pattern = \"dd\/MM\/yyyy\";\r\nSimpleDateFormat DateFor = new SimpleDateFormat(pattern);\r\ntry {\r\nDate date = DateFor.parse(\"20\/05\/2016\");\r\nSystem.out.println(\"Date : \" + date);\r\n} catch (ParseException e) {\r\ne.printStackTrace();\r\n}\r\n}\r\n}\r\n<\/code><\/pre>\r\n<\/div>\r\nOutput for the above program\r\n
\r\n
\r\nDate : Fri May 20 00:00:00 UTC 2016\r\n<\/code><\/pre>\r\n<\/div>\r\nAfter learning to format dates with the SimpleDateFormat <\/em>class, let us now learn to format dates with the DateTimeFormatter <\/em>class in java.\r\n

Formatting dates with DateTimeFormatter class in java<\/strong><\/h3>\r\nThe ofpattern() method of the DateTimeFormatter class can be used on LocalDateTime instance, to format or parse the date or time object of the class.\r\n
\r\n
\r\n\/\/ Example Program to format date with the DateTimeFormatter class\r\nimport java.util.*;\r\nimport java.time.LocalDateTime;\r\nimport java.time.format.DateTimeFormatter;\r\n \r\npublic class Main {\r\npublic static void main(String[] args) {\r\nLocalDateTime dtobj = LocalDateTime.now();\r\nSystem.out.println(\"Date before formatting: \" + dtobj);\r\nDateTimeFormatter myFormatObj = DateTimeFormatter.ofPattern(\"dd-MM-yyyy, HH-mm-ss\");\r\n \r\nString fdt = dtobj.format(myFormatObj);\r\nSystem.out.println(\"Date after formatting: \" + fdt);\r\n}\r\n}\r\n<\/code><\/pre>\r\n<\/div>\r\nOutput for the above program\r\n
\r\n
\r\nDate before formatting: 2020-10-09T14:21:51.602193\r\nDate after formatting: 09-10-2020, 14-21-51\r\n<\/code><\/pre>\r\n<\/div>\r\n

This was all about strings, arrays, and string arrays in java along with practice programs. To get in-depth knowledge of the core Java <\/em>and advanced java, J2EE\u00a0 SOA training <\/em>along with its various applications and real-time projects using Servlets, Spring <\/em>with Aspect-Oriented Programming (<\/em>AOP) <\/em>architecture,<\/em> Hibernate<\/em> Framework, and Struts <\/em>through JDBC <\/em>you can enroll in Certified Java Training in Chennai<\/a> or Certified Java Training in Bangalore<\/a> by FITA <\/a>or a virtual class for this course, at an affordable price, bundled with real-time projects, certification, support, and career guidance assistance and an active placement cell, to make you an industry required certified java developer.<\/p>\r\n

FITA\u2019s courses training is delivered by professional experts who have worked in the software development and testing industry for a minimum of 10+ years, and have experience of working with different software frameworks and software testing designs.<\/p>\r\n

<\/p>","protected":false},"excerpt":{"rendered":"In this blog, we will learn about the date class of the java, customizing the date formats along with the example programs, with this table of content\u2026 Date Class in java Constructors of the date class Methods of the date class Formatting dates with the DateFormat class Converting date to a string in java Formatting […]","protected":false},"author":1,"featured_media":89077,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-69173","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java"],"acf":[],"yoast_head":"\nDate class In Java: Formatting dates in Java | Date Format in JAVA | FITA Academy<\/title>\n<meta name=\"description\" content=\"Date in java can be represented as an object of the class, Date, which is present at the java.util package implementing the comparable interface, serializable interface, and cloneable interface.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.fita.in\/date-format-in-java\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Date class In Java: Formatting dates in Java | Date Format in JAVA | FITA Academy\" \/>\n<meta property=\"og:description\" content=\"Date in java can be represented as an object of the class, Date, which is present at the java.util package implementing the comparable interface, serializable interface, and cloneable interface.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.fita.in\/date-format-in-java\/\" \/>\n<meta property=\"og:site_name\" content=\"FITA Academy\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/fitaAcademy\" \/>\n<meta property=\"article:published_time\" content=\"2020-11-07T07:07:34+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-10-09T12:44:54+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.fita.in\/wp-content\/uploads\/2020\/11\/date-format-in-java.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"800\" \/>\n\t<meta property=\"og:image:height\" content=\"400\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@fitaacademy\" \/>\n<meta name=\"twitter:site\" content=\"@fitaacademy\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"6 minutes\" \/>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Date class In Java: Formatting dates in Java | Date Format in JAVA | FITA Academy","description":"Date in java can be represented as an object of the class, Date, which is present at the java.util package implementing the comparable interface, serializable interface, and cloneable interface.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.fita.in\/date-format-in-java\/","og_locale":"en_US","og_type":"article","og_title":"Date class In Java: Formatting dates in Java | Date Format in JAVA | FITA Academy","og_description":"Date in java can be represented as an object of the class, Date, which is present at the java.util package implementing the comparable interface, serializable interface, and cloneable interface.","og_url":"https:\/\/www.fita.in\/date-format-in-java\/","og_site_name":"FITA Academy","article_publisher":"https:\/\/www.facebook.com\/fitaAcademy","article_published_time":"2020-11-07T07:07:34+00:00","article_modified_time":"2023-10-09T12:44:54+00:00","og_image":[{"width":800,"height":400,"url":"https:\/\/www.fita.in\/wp-content\/uploads\/2020\/11\/date-format-in-java.jpg","type":"image\/jpeg"}],"author":"admin","twitter_card":"summary_large_image","twitter_creator":"@fitaacademy","twitter_site":"@fitaacademy","twitter_misc":{"Written by":"admin","Est. reading time":"6 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.fita.in\/date-format-in-java\/#article","isPartOf":{"@id":"https:\/\/www.fita.in\/date-format-in-java\/"},"author":{"name":"admin","@id":"https:\/\/www.fita.in\/#\/schema\/person\/bb54ebe12aae8299727b8582c44347fb"},"headline":"Date class In Java: Formatting dates in java","datePublished":"2020-11-07T07:07:34+00:00","dateModified":"2023-10-09T12:44:54+00:00","mainEntityOfPage":{"@id":"https:\/\/www.fita.in\/date-format-in-java\/"},"wordCount":839,"publisher":{"@id":"https:\/\/www.fita.in\/#organization"},"image":{"@id":"https:\/\/www.fita.in\/date-format-in-java\/#primaryimage"},"thumbnailUrl":"https:\/\/www.fita.in\/wp-content\/uploads\/2020\/11\/date-format-in-java.jpg","articleSection":["Java"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/www.fita.in\/date-format-in-java\/","url":"https:\/\/www.fita.in\/date-format-in-java\/","name":"Date class In Java: Formatting dates in Java | Date Format in JAVA | FITA Academy","isPartOf":{"@id":"https:\/\/www.fita.in\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.fita.in\/date-format-in-java\/#primaryimage"},"image":{"@id":"https:\/\/www.fita.in\/date-format-in-java\/#primaryimage"},"thumbnailUrl":"https:\/\/www.fita.in\/wp-content\/uploads\/2020\/11\/date-format-in-java.jpg","datePublished":"2020-11-07T07:07:34+00:00","dateModified":"2023-10-09T12:44:54+00:00","description":"Date in java can be represented as an object of the class, Date, which is present at the java.util package implementing the comparable interface, serializable interface, and cloneable interface.","breadcrumb":{"@id":"https:\/\/www.fita.in\/date-format-in-java\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.fita.in\/date-format-in-java\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.fita.in\/date-format-in-java\/#primaryimage","url":"https:\/\/www.fita.in\/wp-content\/uploads\/2020\/11\/date-format-in-java.jpg","contentUrl":"https:\/\/www.fita.in\/wp-content\/uploads\/2020\/11\/date-format-in-java.jpg","width":800,"height":400},{"@type":"BreadcrumbList","@id":"https:\/\/www.fita.in\/date-format-in-java\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.fita.in\/"},{"@type":"ListItem","position":2,"name":"Date class In Java: Formatting dates in java"}]},{"@type":"WebSite","@id":"https:\/\/www.fita.in\/#website","url":"https:\/\/www.fita.in\/","name":"FITA Academy","description":"","publisher":{"@id":"https:\/\/www.fita.in\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.fita.in\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.fita.in\/#organization","name":"FITA Academy","url":"https:\/\/www.fita.in\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.fita.in\/#\/schema\/logo\/image\/","url":"https:\/\/www.fita.in\/wp-content\/uploads\/2019\/07\/site-logo-2.png","contentUrl":"https:\/\/www.fita.in\/wp-content\/uploads\/2019\/07\/site-logo-2.png","width":206,"height":100,"caption":"FITA Academy"},"image":{"@id":"https:\/\/www.fita.in\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/fitaAcademy","https:\/\/x.com\/fitaacademy","https:\/\/www.instagram.com\/fita_Academy\/","https:\/\/www.linkedin.com\/company\/9223636\/admin\/","https:\/\/www.youtube.com\/channel\/UCIbVc86kI5Jnq4Ez_8_hhIg?view_as=subscriber"]},{"@type":"Person","@id":"https:\/\/www.fita.in\/#\/schema\/person\/bb54ebe12aae8299727b8582c44347fb","name":"admin","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/1eb033f2b18b94065fabbcf6c8d8766b4ce3e554fea0cd7e183a04d46a52a42c?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/1eb033f2b18b94065fabbcf6c8d8766b4ce3e554fea0cd7e183a04d46a52a42c?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/1eb033f2b18b94065fabbcf6c8d8766b4ce3e554fea0cd7e183a04d46a52a42c?s=96&d=mm&r=g","caption":"admin"}}]}},"_links":{"self":[{"href":"https:\/\/www.fita.in\/wp-json\/wp\/v2\/posts\/69173","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.fita.in\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.fita.in\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.fita.in\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.fita.in\/wp-json\/wp\/v2\/comments?post=69173"}],"version-history":[{"count":6,"href":"https:\/\/www.fita.in\/wp-json\/wp\/v2\/posts\/69173\/revisions"}],"predecessor-version":[{"id":94636,"href":"https:\/\/www.fita.in\/wp-json\/wp\/v2\/posts\/69173\/revisions\/94636"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.fita.in\/wp-json\/wp\/v2\/media\/89077"}],"wp:attachment":[{"href":"https:\/\/www.fita.in\/wp-json\/wp\/v2\/media?parent=69173"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.fita.in\/wp-json\/wp\/v2\/categories?post=69173"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.fita.in\/wp-json\/wp\/v2\/tags?post=69173"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}