{"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":"
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 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 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 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 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":"\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\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\/\/ 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\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\nFormatting dates with the DateFormat class in java<\/strong><\/h3>\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\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\nFormatting 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\/\/ 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\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\nFormatting 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\/\/ 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\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