{"id":166,"date":"2011-05-01T16:22:31","date_gmt":"2011-05-01T06:22:31","guid":{"rendered":"https:\/\/learn-java-by-example.com\/?p=166"},"modified":"2011-07-05T16:49:14","modified_gmt":"2011-07-05T06:49:14","slug":"month-calendar","status":"publish","type":"post","link":"https:\/\/learn-java-by-example.com\/java\/month-calendar\/","title":{"rendered":"Month Calendar"},"content":{"rendered":"

The following application shows how to format displaying a month in a way that would be suitable for including in a calendar.<\/p>\n

This involves making sure days of the week are all vertically aligned, and that the first day of the week appears in the first column.<\/p>\n

The day that is considered the first day of the week is not the same everywhere in the world. Instead of hard coding the first day of the week as either Sunday, Monday (or some other day) we instead use the Locale of the user to determine the first day of the week in that region.<\/p>\n

The Locale is also used to get the day names to use as column headings on the first row.<\/p>\n

\r\n\r\npackage com.learnjava.util;\r\n\r\nimport java.util.Calendar;\r\nimport java.util.Locale;\r\n\r\npublic class MonthCalendar {\r\n\r\n\tprivate static void showMonth(Calendar cal) {\r\n\t\tint month = cal.get(Calendar.MONTH);\r\n\t\tint firstDayOfWeek = cal.getFirstDayOfWeek();\r\n\r\n\t\t\/\/ Display day names as headers\r\n\r\n\t\tcal.set(Calendar.DAY_OF_WEEK, cal.getFirstDayOfWeek());\r\n\t\tfor (int i=0; i<7; i++) {\r\n\r\n\t\t\tSystem.out.print(cal.getDisplayName(\r\n\t\t\t\tCalendar.DAY_OF_WEEK, Calendar.SHORT, Locale.getDefault()));\r\n\t\t\tSystem.out.print(" ");\r\n\t\t\tcal.add(Calendar.DATE, 1);\r\n\t\t}\r\n\t\tSystem.out.println();\r\n\r\n\t\t\/\/ Display dates in month\r\n\r\n\t\tcal.set(Calendar.DATE, cal.getMinimum(Calendar.DATE));\r\n\r\n\t\t\/\/ Now display the dates, one week per line\r\n\r\n\t\tStringBuilder week = new StringBuilder();\r\n\r\n\t\twhile (month==cal.get(Calendar.MONTH)) {\r\n\r\n\t\t\t\/\/ Display date\r\n\r\n\t\t\tweek.append(String.format("%3d ", cal.get(Calendar.DATE)));\r\n\r\n\t\t\t\/\/ Increment date\r\n\r\n\t\t\tcal.add(Calendar.DATE, 1);\r\n\r\n\t\t\t\/\/ Check if week needs to be printed\r\n\r\n\t\t\tif (cal.get(Calendar.MONTH)!=month) {\r\n\r\n\t\t\t\t\/\/ end of month\r\n\t\t\t\t\/\/ just need to output the month\r\n\r\n\t\t\t\tSystem.out.println(week);\r\n\r\n\t\t\t} else if (cal.get(Calendar.DAY_OF_WEEK)==firstDayOfWeek) {\r\n\r\n\t\t\t\t\/\/ new week so print out the current week\r\n\t\t\t\t\/\/ first check if any padding needed\r\n\r\n\t\t\t\tint padding = 28-week.length();\r\n\t\t\t\tif (padding>0) {\r\n\r\n\t\t\t\t\t\/\/ pad out start of week\r\n\r\n\t\t\t\t\tweek.insert(0, \r\n\t\t\t\t\t\tString.format("%"+padding+"s", " "));\r\n\t\t\t\t}\r\n\t\t\t\tSystem.out.println(week);\r\n\t\t\t\tweek.setLength(0);\r\n\t\t\t}\r\n\t\t}\r\n\t}\r\n\t\r\n\tpublic static void main(String[] args) {\r\n\t\t\r\n\t\tCalendar today = Calendar.getInstance();\r\n\t\tshowMonth(today);\r\n\r\n\t}\r\n\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"

The following application shows how to format displaying a month in a way that would be suitable for including in a calendar. This involves making sure days of the week are all vertically aligned, and that the first day of the week appears in the first column. The day that is considered the first day…<\/p>\n","protected":false},"author":3,"featured_media":168,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true,"jetpack_social_options":[]},"categories":[4],"tags":[38,41,39,40],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/learn-java-by-example.com\/wp-content\/uploads\/2011\/05\/calendar.jpg","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p6Yyl2-2G","jetpack-related-posts":[{"id":370,"url":"https:\/\/learn-java-by-example.com\/java\/iterate-dates-range\/","url_meta":{"origin":166,"position":0},"title":"How can I iterate through all dates in a range?","date":"November 23, 2013","format":false,"excerpt":"Ever wanted to iterate through a range of Date's? We can use Iterator's for looping through the elements of a Collection, but for Date's we need to implement the Date arithmetic required. The following class shows how we can create our own Iterator implementation to easily iterate over a range\u2026","rel":"","context":"In "Java"","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":300,"url":"https:\/\/learn-java-by-example.com\/java\/control-decimal-places-displayed-jtable-column\/","url_meta":{"origin":166,"position":1},"title":"How to control decimal places displayed in JTable column?","date":"September 10, 2015","format":false,"excerpt":"Rendering of table cells is handled by instances of TableCellRenderer. By default JTable uses a DefaultTableCellRenderer to render all of its cells. To control the number of decimal places used we just need to subclass DefaultTableCellRenderer and format the cell double value before passing the (formatted) value to to the\u2026","rel":"","context":"In "Java"","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":251,"url":"https:\/\/learn-java-by-example.com\/java\/add-checkbox-items-jlist\/","url_meta":{"origin":166,"position":2},"title":"How do add a checkbox to items in a JList?","date":"June 30, 2015","format":false,"excerpt":"We often get asked about how to implement a list of checkboxes using Swing. Using a JList filled with JCheckbox's seems the obvious solution, however JList does not support cell editors so this does not work. One possible solutions is to use a single column JTable and store boolean's as\u2026","rel":"","context":"In "Java"","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":214,"url":"https:\/\/learn-java-by-example.com\/java\/convert-inches-centimetres\/","url_meta":{"origin":166,"position":3},"title":"Convert Inches to Centimetres","date":"October 14, 2011","format":false,"excerpt":"The next example was kindly shared by one of our members. She had a Java Swing assignment to create a Swing GUI for converting Inches to Centimetres and had no idea where to start. We helped her break down the problem and come up with a solution. The resulting code\u2026","rel":"","context":"In "Java"","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":98,"url":"https:\/\/learn-java-by-example.com\/java\/calculator-keypad\/","url_meta":{"origin":166,"position":4},"title":"Calculator Keypad","date":"August 14, 2010","format":false,"excerpt":"This problem involves understanding some of the basics of building a Swing application including how individual components are laid out inside a window, and how your application can react to the user interacting with the application. In this case we need to update a text field whenever a button is\u2026","rel":"","context":"In "Java"","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":205,"url":"https:\/\/learn-java-by-example.com\/java\/counting-numbers\/","url_meta":{"origin":166,"position":5},"title":"Counting numbers","date":"September 12, 2011","format":false,"excerpt":"The assignment here is to read a text file containing a list of numbers. We need to report how many numbers were in the file, the sum of all the numbers, the average of all numbers, and the minimum and maximum number. First step is to read the file and\u2026","rel":"","context":"In "Featured"","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"_links":{"self":[{"href":"https:\/\/learn-java-by-example.com\/wp-json\/wp\/v2\/posts\/166"}],"collection":[{"href":"https:\/\/learn-java-by-example.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/learn-java-by-example.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/learn-java-by-example.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/learn-java-by-example.com\/wp-json\/wp\/v2\/comments?post=166"}],"version-history":[{"count":7,"href":"https:\/\/learn-java-by-example.com\/wp-json\/wp\/v2\/posts\/166\/revisions"}],"predecessor-version":[{"id":171,"href":"https:\/\/learn-java-by-example.com\/wp-json\/wp\/v2\/posts\/166\/revisions\/171"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/learn-java-by-example.com\/wp-json\/wp\/v2\/media\/168"}],"wp:attachment":[{"href":"https:\/\/learn-java-by-example.com\/wp-json\/wp\/v2\/media?parent=166"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/learn-java-by-example.com\/wp-json\/wp\/v2\/categories?post=166"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/learn-java-by-example.com\/wp-json\/wp\/v2\/tags?post=166"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}