Results 1 to 15 of 15
Like Tree1Likes
  • 1 Post By JosAH

Thread: How to automatically generate all the dates in a year?

  1. #1
    Zarah is offline Senior Member
    Join Date
    Mar 2014
    Posts
    116
    Rep Power
    0

    Default How to automatically generate all the dates in a year?

    I need a list of all the dates in 2014 in "YYYY-MM-DD" format (because I need to load them into a database), and I do not want to type them all myself. So is there a way to actually get it generated automatically?

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,522
    Blog Entries
    7
    Rep Power
    20

    Default Re: How to automatically generate all the dates in a year?

    Read the API documentation for the Calendar class.

    kind regards,

    Jos
    Zarah likes this.
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,602
    Rep Power
    5

    Default Re: How to automatically generate all the dates in a year?

    Well, without knowing exactly how to do this (because of my unfamiliarity with the API), I would look for something which translates a Julian day (day of year, like 143) into a Gregorian date (I think I got those terms correct). Then you just iterate from 1 to 365 or 366 as appropriate.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  4. #4
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,522
    Blog Entries
    7
    Rep Power
    20

    Default Re: How to automatically generate all the dates in a year?

    Read the API documentation and start with 2014/01/01, make the Calendar object add one day until you reach 2014/12/31; no need to keep track of leap years or whatever ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    kneitzel is offline Senior Member
    Join Date
    Feb 2014
    Posts
    447
    Rep Power
    1

    Default Re: How to automatically generate all the dates in a year?

    Hi,

    I am just wondering why you need this kind of information. Maybe you can give more details about your database design. All days in a year is just some static stuff that databases and computers know. It is not really something that I would expect inside a table.

    But in Java you could simply take the Calendar class:
    Java Code:
    		Calendar calendar = Calendar.getInstance();
    		calendar.set(2014, Calendar.JANUARY, 1);
    		while (calendar.get(Calendar.YEAR) == 2014) {
    			System.out.println(
    					String.format("%04d-%02d-%02d", 
    							calendar.get(Calendar.YEAR),
    							calendar.get(Calendar.MONTH)+1,
    							calendar.get(Calendar.DATE)));
    			calendar.add(Calendar.DATE, 1);
    		}
    if something like that is really required in a database, then you might want to do it inside the database itself. The databases that I work with have no problem with stuff like that, too. (Another reason why this shouldn't be required).

    And if you really want to do something like that then you should take care that the new entries are automatically created whenever required. Else your application stops working after 2014 because nobody included the dates for 2015 or so... That would be more than strange :)

    So in short: Rething your database design! (Maybe you want to explain it in more detail).
    Konrad

  6. #6
    Zarah is offline Senior Member
    Join Date
    Mar 2014
    Posts
    116
    Rep Power
    0

    Default Re: How to automatically generate all the dates in a year?

    Quote Originally Posted by kneitzel View Post
    So in short: Rething your database design! (Maybe you want to explain it in more detail).
    In a nutshell, the database has to keep track of the holidays in a year, i.e. public holidays as well as weekends; and have some functionality related to the rest of the days (non-holidays)

    I have stored the holiday dates in a table. Now I want to retrieve them, and do something like:

    Select the dates which are NOT equal to the holiday dates, and then schedule these events on the retrieved dates. I hope I am giving the idea clearly.

    I actually don't want the holiday dates to be entered by hand, but I think that is not possible since the holidays are different in different countries. But I'll appreciate if you have any tips on that.

    I am using MySQL database.
    Last edited by Zarah; 04-24-2014 at 02:40 AM.

  7. #7
    Zarah is offline Senior Member
    Join Date
    Mar 2014
    Posts
    116
    Rep Power
    0

    Default Re: How to automatically generate all the dates in a year?

    Quote Originally Posted by kneitzel View Post
    So in short: Rething your database design! (Maybe you want to explain it in more detail).
    In a nutshell, the database has to keep track of the holidays in a year, i.e. public holidays as well as weekends; and have some functionality related to the rest of the days (non-holidays)

    I have stored the holiday dates in a table. Now I want to retrieve them, and do something like:

    Select the dates which are NOT equal to the holiday dates, and then schedule these events on the retrieved dates. I hope I am giving the idea clearly.

    I actually don't want the holiday dates to be entered by hand, but I think that is not possible since the holidays are different in different countries. But I'll appreciate if you have any tips on that.

    I am using MySQL database.

  8. #8
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,602
    Rep Power
    5

    Default Re: How to automatically generate all the dates in a year?

    I am do not speak MySQL but does it have anything like a locale which might lend itself to foreign holidays? Also, how are the dates being stored? As strings? As some internal format to MySQL?

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  9. #9
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,007
    Rep Power
    6

    Default Re: How to automatically generate all the dates in a year?

    Quote Originally Posted by Zarah View Post
    I actually don't want the holiday dates to be entered by hand, but I think that is not possible since the holidays are different in different countries. But I'll appreciate if you have any tips on that.
    Correct. I'll make it even more interesting for you: some holidays are not on specific dates but rather on specific days ("X sunday of month Y), such as Easter. Those need to be calculated.

    Welcome to the wonderful world of software engineering, where problems are functionally easy ("exclude holidays", so easy to say) but are then technically very difficult to implement. My initial gut feeling would be to look for an online webservice that can produce the information for a specific country.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  10. #10
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,522
    Blog Entries
    7
    Rep Power
    20

    Default Re: How to automatically generate all the dates in a year?

    Quote Originally Posted by gimbal2 View Post
    Correct. I'll make it even more interesting for you: some holidays are not on specific dates but rather on specific days ("X sunday of month Y), such as Easter. Those need to be calculated.
    That phrase made me curious: how to calculate the Sunday for Easter? I googled a bit and ended up in a total mess: http://christianity.about.com/od/eas...lfullmoon.htm; normally I don't give up so easily, but this had made me stupified; I'm longing for a cold Grolsch beer at this hour of day ... religion is silly and I thank god on my bare knees that I'm an atheist ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  11. #11
    SurfMan's Avatar
    SurfMan is online now Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    946
    Rep Power
    2

    Default Re: How to automatically generate all the dates in a year?

    Quote Originally Posted by JosAH View Post
    That phrase made me curious: how to calculate the Sunday for Easter? I googled a bit and ended up in a total mess: http://christianity.about.com/od/eas...lfullmoon.htm; normally I don't give up so easily, but this had made me stupified; I'm longing for a cold Grolsch beer at this hour of day ... religion is silly and I thank god on my bare knees that I'm an atheist ...

    kind regards,

    Jos
    This article made my head explode. I never knew there was so much to it. GM Arts

    Definition of Easter Sunday Date
    Easter Sunday is the Sunday following the Paschal Full Moon (PFM) date for the year.
    In June 325 A.D. astronomers approximated astronomical full moon dates for the Christian church, calling them Ecclesiastical Full Moon (EFM) dates. From 326 A.D. the PFM date has always been the EFM date after March 20 (which was the equinox date in 325 A.D.).
    Wait, what?

    This table is spot on:
    How to automatically generate all the dates in a year?-e_pfm.gif
    2014/19 = 106, remainder 0.
    So the date of the PFM is April 14, next Sunday April 20 is Easter

    2013/19 = 105, remainder 18.
    Easter 2013 was on the Sunday following March 27, which indeed was March 31.

    2015/19 = 106, remainder 1: PFM on April 3, Easter on April 5.

    Another thing learned today.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2™ © 2013

  12. #12
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,007
    Rep Power
    6

    Default Re: How to automatically generate all the dates in a year?

    Quote Originally Posted by JosAH View Post
    That phrase made me curious: how to calculate the Sunday for Easter? I googled a bit and ended up in a total mess: http://christianity.about.com/od/eas...lfullmoon.htm; normally I don't give up so easily, but this had made me stupified; I'm longing for a cold Grolsch beer at this hour of day ... religion is silly and I thank god on my bare knees that I'm an atheist ...
    Holy Jebus, I didn't know it was that tricky!
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  13. #13
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,522
    Blog Entries
    7
    Rep Power
    20

    Default Re: How to automatically generate all the dates in a year?

    Now the question remains: how did those morons calculate that table in Surfman' post? This frustrated me half of the morning ...

    kind regards,

    Jos (<--- nosy)
    cenosillicaphobia: the fear for an empty beer glass

  14. #14
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,007
    Rep Power
    6

    Default Re: How to automatically generate all the dates in a year?

    YOU CAN DO IT!

    ... once you're sober again.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  15. #15
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,522
    Blog Entries
    7
    Rep Power
    20

    Default Re: How to automatically generate all the dates in a year?

    Quote Originally Posted by gimbal2 View Post
    YOU CAN DO IT!

    ... once you're sober again.
    That'll be somewhere in the middle of next week ...

    kind regards,

    Jos ;-)
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Integer Dates to String Dates Problem
    By HelpingIsCaring in forum New To Java
    Replies: 32
    Last Post: 08-23-2013, 12:29 AM
  2. classes that automatically generate return values
    By ajetrumpet in forum New To Java
    Replies: 6
    Last Post: 04-20-2013, 10:43 AM
  3. Replies: 1
    Last Post: 09-20-2011, 02:34 PM
  4. Replies: 26
    Last Post: 02-25-2010, 11:56 AM
  5. Automatically Generate an XML file
    By svpriyan in forum XML
    Replies: 2
    Last Post: 06-25-2009, 05:46 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •