{"id":1123,"date":"2010-08-13T13:01:46","date_gmt":"2010-08-13T07:01:46","guid":{"rendered":"http:\/\/aasims.wordpress.com\/?p=1123"},"modified":"2025-05-05T08:48:14","modified_gmt":"2025-05-05T08:48:14","slug":"date-formatting-in-java","status":"publish","type":"post","link":"https:\/\/aasimnaseem.com\/blog\/date-formatting-in-java\/","title":{"rendered":"Date Formatting in Java"},"content":{"rendered":"<p>Hi all&#8230;<\/p>\n<p>Hope you&#8217;re enjoying your time&#8230;<img decoding=\"async\" class=\"alignright\" src=\"http:\/\/www.thejavaarcade.com\/img\/java_logo.png\" alt=\"\" \/><\/p>\n<p>Today I will explain how to use date-time (date manipulation) in the Java programming language. I was working on a task where I needed to play with time\/date. In my other post, I explained the\u00a0<a href=\"https:\/\/AasimNaseem.com\/wp-content\/uploads\/2010\/06\/15\/date-formatting-in-c\/\">date formatting\u00a0in C#<\/a>. Today you will learn the same concept in Java.<\/p>\n<p>The Java Calendar class (java.util.Calendar) is a very useful and handy class in Java date-time manipulation. Here I will demonstrate how to modify date and time with the <code>Calendar<\/code> class. First get the current date and time with Calendar().<\/p>\n<p>[code language=&#8221;java&#8221;]<br \/>\nDateFormat dateFormat = new SimpleDateFormat(&quot;yyyy\/MM\/dd HH:mm:ss&quot;);<br \/>\nCalendar cal = Calendar.getInstance();<br \/>\nSystem.out.println(&quot;Current Date Time : &quot; + dateFormat.format(cal.getTime()));<br \/>\n[\/code]<\/p>\n<p>now you can use following calender date time manipulation function as per your need<\/p>\n<p>[code language=&#8221;java&#8221;]<\/p>\n<p>\/\/Add one day to current date time<br \/>\ncal.add(Calendar.DATE, 1);<\/p>\n<p>\/\/Add one month to current date time<br \/>\ncal.add(Calendar.MONTH, 1);<\/p>\n<p>\/\/Add one year to current date time<br \/>\ncal.add(Calendar.YEAR, 1);<\/p>\n<p>\/\/Add one hour to current date time<br \/>\ncal.add(Calendar.HOUR, 1);<\/p>\n<p>\/\/Add one minute to current date time<br \/>\ncal.add(Calendar.MINUTE, 1);<\/p>\n<p>\/\/Add one second to current date time<br \/>\ncal.add(Calendar.SECOND, 1);<\/p>\n<p>\/\/Subtract one day from current date<br \/>\ncal.add(Calendar.DATE, -1);<\/p>\n<p>\/\/Subtract one month from current date<br \/>\ncal.add(Calendar.MONTH, -1);<\/p>\n<p>\/\/Subtract one year from current date<br \/>\ncal.add(Calendar.YEAR, -1);<\/p>\n<p>\/\/Subtract one hour from current date<br \/>\ncal.add(Calendar.HOUR, -1);<\/p>\n<p>\/\/Subtract one minute from current date<br \/>\ncal.add(Calendar.MINUTE, -1);<\/p>\n<p>\/\/Subtract one second from current date<br \/>\ncal.add(Calendar.SECOND, -1);<\/p>\n<p>[\/code]<\/p>\n<p>Here is the full source code to show\u00a0how to modify date time in Java<\/p>\n<p>[code language=&#8221;java&#8221;]<\/p>\n<p>import java.text.DateFormat;<br \/>\nimport java.text.SimpleDateFormat;<br \/>\nimport java.util.Calendar;<\/p>\n<p>public class DateTimeManipulation {<br \/>\npublic static void main(String[] args) {<\/p>\n<p>DateFormat dateFormat = new SimpleDateFormat(&quot;yyyy\/MM\/dd HH:mm:ss&quot;);<br \/>\n\/\/get current date time with Calendar()<br \/>\nCalendar cal = Calendar.getInstance();<br \/>\nSystem.out.println(&quot;Current Date Time : &quot; + dateFormat.format(cal.getTime()));<\/p>\n<p>cal.add(Calendar.DATE, 1);<br \/>\nSystem.out.println(&quot;Add one day to current date : &quot; + dateFormat.format(cal.getTime()));<\/p>\n<p>cal = Calendar.getInstance();<br \/>\ncal.add(Calendar.MONTH, 1);<br \/>\nSystem.out.println(&quot;Add one month to current date : &quot; + dateFormat.format(cal.getTime()));<\/p>\n<p>cal = Calendar.getInstance();<br \/>\ncal.add(Calendar.YEAR, 1);<br \/>\nSystem.out.println(&quot;Add one year to current date : &quot; + dateFormat.format(cal.getTime()));<\/p>\n<p>cal = Calendar.getInstance();<br \/>\ncal.add(Calendar.HOUR, 1);<br \/>\nSystem.out.println(&quot;Add one hour to current date : &quot; + dateFormat.format(cal.getTime()));<\/p>\n<p>cal = Calendar.getInstance();<br \/>\ncal.add(Calendar.MINUTE, 1);<br \/>\nSystem.out.println(&quot;Add one minute to current date : &quot; + dateFormat.format(cal.getTime()));<\/p>\n<p>cal = Calendar.getInstance();<br \/>\ncal.add(Calendar.SECOND, 1);<br \/>\nSystem.out.println(&quot;Add one second to current date : &quot; + dateFormat.format(cal.getTime()));<\/p>\n<p>cal = Calendar.getInstance();<br \/>\ncal.add(Calendar.DATE, -1);<br \/>\nSystem.out.println(&quot;Subtract one day from current date : &quot; + dateFormat.format(cal.getTime()));<\/p>\n<p>cal = Calendar.getInstance();<br \/>\ncal.add(Calendar.MONTH, -1);<br \/>\nSystem.out.println(&quot;Subtract one month from current date : &quot; + dateFormat.format(cal.getTime()));<\/p>\n<p>cal = Calendar.getInstance();<br \/>\ncal.add(Calendar.YEAR, -1);<br \/>\nSystem.out.println(&quot;Subtract one year from current date : &quot; + dateFormat.format(cal.getTime()));<\/p>\n<p>cal = Calendar.getInstance();<br \/>\ncal.add(Calendar.HOUR, -1);<br \/>\nSystem.out.println(&quot;Subtract one hour from current date : &quot; + dateFormat.format(cal.getTime()));<\/p>\n<p>cal = Calendar.getInstance();<br \/>\ncal.add(Calendar.MINUTE, -1);<br \/>\nSystem.out.println(&quot;Subtract one minute from current date : &quot; + dateFormat.format(cal.getTime()));<\/p>\n<p>cal = Calendar.getInstance();<br \/>\ncal.add(Calendar.SECOND, -1);<br \/>\nSystem.out.println(&quot;Subtract one second from current date : &quot; + dateFormat.format(cal.getTime()));<\/p>\n<p>}<br \/>\n}<br \/>\n[\/code]<\/p>\n<h3>Output<\/h3>\n<p><em>Current Date Time : 2010\/08\/13 10:24:53<\/em><br \/>\n<em>Add one day to current date : 2010\/08\/14 10:24:53<\/em><br \/>\n<em>Add one month to current date : 2010\/09\/13 10:24:53<\/em><br \/>\n<em>Add one year to current date : 2011\/08\/13 10:24:53<\/em><br \/>\n<em>Add one hour to current date : 2010\/08\/13 11:24:53<\/em><br \/>\n<em>Add one minute to current date : 2010\/08\/13 10:25:53<\/em><br \/>\n<em>Add one second to current date : 2010\/08\/13 10:24:54<\/em><br \/>\n<em>Subtract one day from current date : 2010\/08\/12 10:24:53<\/em><br \/>\n<em>Subtract one month from current date : 2010\/07\/13 10:24:53<\/em><br \/>\n<em>Subtract one year from current date : 2009\/08\/13 10:24:53<\/em><br \/>\n<em>Subtract one hour from current date : 2010\/08\/13 09:24:53<\/em><br \/>\n<em>Subtract one minute from current date : 2010\/08\/13 10:23:53<\/em><br \/>\n<em>Subtract one second from current date : 2010\/08\/13 10:24:52<\/em><\/p>\n<p>Thanks for your time.<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/s07.flagcounter.com\/count\/n0k\/bg=FFFFFF\/txt=000000\/border=FFFFFF\/columns=6\/maxflags=200\/viewers=0\/labels=1\/pageviews=1\/\" alt=\"free counters\" border=\"0\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hi all&#8230; Hope you&#8217;re enjoying your time&#8230; Today I will explain how to use date-time (date manipulation) in the Java programming language. I was working on a task where I needed to play with time\/date. In my other post, I explained the\u00a0date formatting\u00a0in C#. Today you will learn the same&#8230;<\/p>\n","protected":false},"author":1,"featured_media":5258,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1154],"tags":[250,309,310,311],"class_list":["post-1123","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java","tag-calendar-java","tag-date-formatting-in-c","tag-date-formatting-in-java","tag-dateformat"],"_links":{"self":[{"href":"https:\/\/aasimnaseem.com\/blog\/wp-json\/wp\/v2\/posts\/1123","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/aasimnaseem.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/aasimnaseem.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/aasimnaseem.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/aasimnaseem.com\/blog\/wp-json\/wp\/v2\/comments?post=1123"}],"version-history":[{"count":4,"href":"https:\/\/aasimnaseem.com\/blog\/wp-json\/wp\/v2\/posts\/1123\/revisions"}],"predecessor-version":[{"id":5259,"href":"https:\/\/aasimnaseem.com\/blog\/wp-json\/wp\/v2\/posts\/1123\/revisions\/5259"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/aasimnaseem.com\/blog\/wp-json\/wp\/v2\/media\/5258"}],"wp:attachment":[{"href":"https:\/\/aasimnaseem.com\/blog\/wp-json\/wp\/v2\/media?parent=1123"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/aasimnaseem.com\/blog\/wp-json\/wp\/v2\/categories?post=1123"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/aasimnaseem.com\/blog\/wp-json\/wp\/v2\/tags?post=1123"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}