{"id":370,"date":"2013-11-23T13:43:37","date_gmt":"2013-11-23T02:43:37","guid":{"rendered":"https:\/\/learn-java-by-example.com\/?p=370"},"modified":"2015-11-23T13:49:33","modified_gmt":"2015-11-23T02:49:33","slug":"iterate-dates-range","status":"publish","type":"post","link":"https:\/\/learn-java-by-example.com\/java\/iterate-dates-range\/","title":{"rendered":"How can I iterate through all dates in a range?"},"content":{"rendered":"

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.<\/p>\n

The following class shows how we can create our own
\nIterator implementation to easily iterate over a range of Date’s, one day at a time.<\/p>\n

\r\npackage com.learnjava.util;\r\n\r\nimport java.util.Calendar;\r\nimport java.util.Date;\r\nimport java.util.Iterator;\r\n\r\npublic class DateIterator\r\n   implements Iterator<Date>, Iterable<Date>\r\n{\r\n\r\n    private Calendar end = Calendar.getInstance();\r\n    private Calendar current = Calendar.getInstance();\r\n\r\n    public DateIterator(Date start, Date end)\r\n    {\r\n        this.end.setTime(end);\r\n        this.end.add(Calendar.DATE, -1);\r\n        this.current.setTime(start);\r\n        this.current.add(Calendar.DATE, -1);\r\n    }\r\n\r\n    @Override\r\n    public boolean hasNext()\r\n    {\r\n        return !current.after(end);\r\n    }\r\n\r\n    @Override\r\n    public Date next()\r\n    {\r\n        current.add(Calendar.DATE, 1);\r\n        return current.getTime();\r\n    }\r\n\r\n    @Override\r\n    public void remove()\r\n    {\r\n        throw new UnsupportedOperationException(\r\n           "Cannot remove");\r\n    }\r\n\r\n    @Override\r\n    public Iterator<Date> iterator()\r\n    {\r\n        return this;\r\n    }\r\n\r\n    public static void main(String[] args)\r\n    {\r\n    \tDate d1 = new Date();\r\n    \tCalendar cal = Calendar.getInstance();\r\n    \tcal.add(Calendar.DATE, 20);\r\n    \tDate d2 = cal.getTime();\r\n\r\n    \tIterator<Date> i = new DateIterator(d1, d2);\r\n    \twhile(i.hasNext())\r\n    \t{\r\n    \t\tDate date = i.next();\r\n    \t\tSystem.out.println(date);\r\n    \t}\r\n    }\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"

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 of Date’s, one day at…<\/p>\n","protected":false},"author":1,"featured_media":0,"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":[108,107,106,6,109],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p6Yyl2-5Y","jetpack-related-posts":[{"id":166,"url":"https:\/\/learn-java-by-example.com\/java\/month-calendar\/","url_meta":{"origin":370,"position":0},"title":"Month Calendar","date":"May 1, 2011","format":false,"excerpt":"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\u2026","rel":"","context":"In "Java"","img":{"alt_text":"","src":"https:\/\/i0.wp.com\/learn-java-by-example.com\/wp-content\/uploads\/2011\/05\/calendar.jpg?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":251,"url":"https:\/\/learn-java-by-example.com\/java\/add-checkbox-items-jlist\/","url_meta":{"origin":370,"position":1},"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":300,"url":"https:\/\/learn-java-by-example.com\/java\/control-decimal-places-displayed-jtable-column\/","url_meta":{"origin":370,"position":2},"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":214,"url":"https:\/\/learn-java-by-example.com\/java\/convert-inches-centimetres\/","url_meta":{"origin":370,"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":370,"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":370,"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\/370"}],"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\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/learn-java-by-example.com\/wp-json\/wp\/v2\/comments?post=370"}],"version-history":[{"count":2,"href":"https:\/\/learn-java-by-example.com\/wp-json\/wp\/v2\/posts\/370\/revisions"}],"predecessor-version":[{"id":372,"href":"https:\/\/learn-java-by-example.com\/wp-json\/wp\/v2\/posts\/370\/revisions\/372"}],"wp:attachment":[{"href":"https:\/\/learn-java-by-example.com\/wp-json\/wp\/v2\/media?parent=370"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/learn-java-by-example.com\/wp-json\/wp\/v2\/categories?post=370"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/learn-java-by-example.com\/wp-json\/wp\/v2\/tags?post=370"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}