• Post Reply Bookmark Topic Watch Topic
  • New Topic

building a calendar using only System.currentTimeMillis  RSS feed

 
brian watkins
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In this project i am only allowed to use System.currentTimeMillis(). No java methods that come with java related to dates, times, calendars, etc. I have got it to show the current day, week, month, year, the day of the week, and the time down to the second. Now i need to build something like this


                                                        
            September
            ---------

Sun Mon Tue Wed Thu Fri Sat
  1   2   3
  4   5   6   7   8   9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 *29* 30

Any help on this would be helpful i can provide the code if need be but I am just really looking for some direction on how to go about starting it.



 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Interesting problem. The first thing you should know is that per the API docs, currentTimeMillis() is "the difference, measured in milliseconds, between the current time and midnight, January 1, 1970 UTC."  You'd have to base all your calculations on this and what you already know about converting milliseconds to seconds, minutes, hours, days, months, and years. You'll also need to factor in the rules for determining leap years.
 
brian watkins
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, I have all the calculations down already. I just need to know how to convert Friday, October 14, 2016 into the calendar type above. I have no idea where to begin.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37513
554
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You said you know what day of the week, Sept 14th is, right? That means you can figure out what day of the week the first of the month is. (That's just subtracting 1 or 7 until you get there).

From there, you can make the calendar as long as you know what date the month ends. For Sept that is easy because it doesn't change. For February, there are leap year rules you can code.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
BTW, Welcome to the Ranch!
 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how to convert Friday, October 14, 2016 into the calendar type

Can you explain what data types you are talking about? For example is the data to be converted a String: "Friday, October 14, 2016"?
What is  "the calendar type"
 
brian watkins
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is what i have to build


Invoking System.currentTimeMillis() returns a long integer giving the elapsed time in milliseconds since midnight of January 1, 1970.  The value returned is based on UTC time, which is 5 hours ahead of Central Daylight Time.  Using the value returned by System.currentTimeMillis(), compute the current date and time, adjusting for Central Daylight Time and truncating to the second. 

You can use any operators or any methods of the Math class, but do NOT call any methods that come with Java related to dates, times, calendars, etc. other than System.currentTimeMillis().  You may wish to use the long data type for all integer variables (otherwise be careful to use int only when you are sure the values will fit), and note that literal numeric values can be designated as long by appending an L, i.e., 5L is a long but 5 is an int.  Your computations will need to take leap years into account.  A leap year is any year that:
a.  is divisible by 400, OR
b.  is divisible by 4 but not divisible by 100

Display the current date and time in the following format.

Current date and time is September 29, 2016 at 10:10:01 am.

Next, using the fact that January 1, 1970 was a Thursday, compute the current day of the week.  (Hint: For the computations, represent Sunday as 0, Monday as 1, …, Saturday as 6.)  Do NOT use Zeller’s congruence (if you have heard of it) or call any methods that come with Java related to dates, times, calendars, etc. Then modify the above output format so that the new output format becomes:

Current date and time is Thursday, September 29, 2016 at 10:10:01 am.


Next, using the values you have computed for the current date and day of the week, compute the day of the week for the first day of the current month.  Then write one or more loops that display a calendar for the current month in the following format with the leftmost column representing Sunday.  (By coincidence, I ran my program on a Thursday and the first day of September was also a Thursday.)  Do NOT call any methods that come with Java related to dates, times, calendars, etc.

Current date and time is Thursday, September 29, 2016 at 10:10:01 am.

  1   2   3
  4   5   6   7   8   9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30

Next, add headers to the calendar for the name of the current month and the names of the days of the week.  Add an asterisk before and after the current date on the calendar.  The final output format should be (of course, the actual date and time you run the program will be different!):

Current date and time is Thursday, September 29, 2016 at 10:10:01 am.

            September
            ---------

Sun Mon Tue Wed Thu Fri Sat
  1   2   3
  4   5   6   7   8   9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 *29* 30

You are highly encouraged (but not required) to break up this program by writing “helper” methods, as described in Chapter 6!


This is my code so far.




 
Norm Radder
Rancher
Posts: 2240
28
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lots of redundant code.  Lines 24 to 63 could be done by just subtracting 12.
The names of the days and months could be obtained by indexing an array.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Way too much code that's specific to date calculation in main. You don't want that; it gets messy as you can see from what you have now.  Ideally, your high-level code should look something like this:

I probably would think about renaming the class to something a little more generic though. Ideally, you would call methods on an object of this class to get other instances that represent other Date/Time moments. So, you could write something like this:

By putting the logic for printing a calendar for a single month in a separate method, this allows you to do something like this:

This is kind of how the old standard Date/Calendar API works.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You won't have to throw away much of the code you have right now to get the kind of API I described. You just have to tease apart some of the code that you wrote and put them into their own separate instance methods.  However, it appears that you're still not doing object-oriented programming because of all the static methods you have. Shifting your thinking to this kind of design may be a stretch for you at the moment. The example is something you might want to keep in mind though as you get into proper OO programming.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can still do some refactoring (Extract method) with your current code that will make it easier for you to eventually write the code to produce the calendar display you need.  For example, this section of code in main:

can be extracted so that you have:

And from there, it's easier to see that the switch-case can be replaced by a single statement:

The same technique can be used for a method like dayOfWeek() / DAY_OF_WEEK_NAMES.

Lastly, with a more object-oriented design like I illustrated before, you could write things like this:
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!