Finding a Date
Modules - CSE1110 Structured Programming 1, CSE1120 Structured Programming 2
Teacher’s Notes:
=======================================
This can be a very tricky assignment since the UTC time given is in a different time zone. When I was programming the solution, I consistently had everything right except my day was short by 1 day. This happened because in my algorithm, the first day of the year would have been day 0 instead of 1. It took a while for me to figure out why this happened and I didn't want to arbitrarily add 1 day for no apparent reason.
The rule for leap years is that every year is a leap year if it is divisible by 4 but not by 100 unless also divisible by 400. Not confusing at all. If you want to skip this, it won't make a difference until the year 2100. However, I would encourage students to work out this algorithm.
Assignment:
=======================================
Finding a Date
Most computer programming languages include commands for accessing the computer clock to find the time. In JAVA, the time is measured in milliseconds from a reference time (1 January 1970, at 12:00am). The command to get the time in JAVA is as follows:
long milliseconds = System.currentTimeMillis();
This command is found in java.sql.Date;
Question: Why does the time need to be stored in a long rather than an int?
Write a program that will retrieve the number of milliseconds that have elapse since the reference time. The program should then calculate and output the current date and time.
Note: the program must account for leap years, and should express the answer in 12 hour time format. The time displayed should be in your current time zone.
Sample Input:
There is no input for this problem.
Sample Output:
December 11, 2024, 10:25
Solution (JAVA):
=======================================
import java.sql.Date;
public class DateFinder {
public static void main(String[] args) {
long milliseconds = System.currentTimeMillis();
long seconds = milliseconds/1000;
//System.out.println(seconds);
//System.out.println(milliseconds);
int currentYear = 1970;
final long secondsPerYear = 60*60*24*365;
final long secondsPerDay = 60*60*24;
int numberOfYearsElapsed = (int)(seconds/secondsPerYear);
int numberOfLeapYears = (int)(seconds/(4*secondsPerYear));
/*Note that this is not the actual criteria for determining leap years.
* Since there have been no leap year exceptions in the last 100 years,
* this approximation is acceptable at this time.
*/
currentYear = 1970 + numberOfYearsElapsed;
seconds %= secondsPerYear;
seconds -= (numberOfLeapYears*secondsPerDay);
/*After all years have been counted, we can subtract them from the number of
* seconds that we still need to account for. This includes the extra
* days from leap years.
*/
//you need to add 1 day to this because otherwise the first day would
//be day 0
int daysInThisYear = (int)(seconds/secondsPerDay)+1;
seconds %= secondsPerDay;
/*This is how you actually find the leap years*/
boolean leapYear = false;
if(currentYear%4 == 0)//Leap year happens every 4 years...
{
leapYear = true;
if(currentYear%100 == 0)//...unless the year is divisible by 100...
{
leapYear = false;
if(currentYear%400 == 0)//...but if it's divisible by 400, it is a leap year
leapYear = true;
}
}
if(!leapYear)
{
if(daysInThisYear <= 31)
System.out.println("January ");
else if(daysInThisYear <= 59) {
System.out.println("February ");
daysInThisYear -= 31;
}
else if(daysInThisYear <= 90) {
System.out.println("March ");
daysInThisYear -= 59;
}
else if(daysInThisYear <= 120) {
System.out.println("April ");
daysInThisYear -= 90;
}
else if(daysInThisYear <= 151) {
System.out.println("May ");
daysInThisYear -= 120;
}
else if(daysInThisYear <= 181) {
System.out.print("June ");
daysInThisYear -= 151;
}
else if(daysInThisYear <= 212) {
System.out.print("July ");
daysInThisYear -= 181;
}
else if(daysInThisYear <= 243) {
System.out.print("August ");
daysInThisYear -= 212;
}
else if(daysInThisYear <= 273) {
System.out.print("September ");
daysInThisYear -= 243;
}
else if(daysInThisYear <= 304) {
System.out.print("October ");
daysInThisYear -= 273;
}
else if(daysInThisYear <= 334) {
System.out.print("November ");
daysInThisYear -= 304;
}
else {
System.out.print("December ");
daysInThisYear -= 334;
}
}
else
{
if(daysInThisYear <= 31)
System.out.print("January ");
else if(daysInThisYear <= 60) {
System.out.print("February ");
daysInThisYear -= 31;
}
else if(daysInThisYear <= 91) {
System.out.print("March ");
daysInThisYear -= 60;
}
else if(daysInThisYear <= 121) {
System.out.print("April ");
daysInThisYear -= 91;
}
else if(daysInThisYear <= 152) {
System.out.print("May ");
daysInThisYear -= 121;
}
else if(daysInThisYear <= 182) {
System.out.print("June ");
daysInThisYear -= 152;
}
else if(daysInThisYear <= 213) {
System.out.print("July ");
daysInThisYear -= 182;
}
else if(daysInThisYear <= 244) {
System.out.print("August ");
daysInThisYear -= 213;
}
else if(daysInThisYear <= 274) {
System.out.print("September ");
daysInThisYear -= 244;
}
else if(daysInThisYear <= 305) {
System.out.print("October ");
daysInThisYear -= 274;
}
else if(daysInThisYear <= 335) {
System.out.print("November ");
daysInThisYear -= 305;
}
else {
System.out.print("December ");
daysInThisYear -= 335;
}
}
System.out.print(daysInThisYear + ", " + currentYear +", ");
//There is a -7 here to account for the time zone difference
//between UTC time and MST
long hours = seconds/3600-7;
seconds %= 3600;
long minutes = seconds / 60;
if(minutes < 10)
System.out.println( hours + ":0" + minutes);
else
System.out.println( hours + ":" + minutes);
}
}