Introducing the Date type - 1

"Hi, Amigo. I want to tell you about an interesting type called Date. This type stores the date and time, and can also measure time intervals."

"Sounds interesting. Please go on."

"Every Date object stores a time in a rather interesting form: the number of milliseconds since January 1, 1970, GMT."

"Whoa!"

"Yeah. This number is so big there's not enough room for it in an int, so it has to be stored in a long. But it's really handy for calculating the difference between any two dates. You just do subtraction to find the difference with accuracy to the millisecond. It also solves the problem of the date line and daylight saving time."

"The most interesting part is that each object is initialized with the current time at its creation. To know the current time, you just need to create a Date object."

"How do you work with it?"

"Here are some examples:"

Get the current date:
public static void main(String[] args) throws Exception
{
     Date today = new Date();
     System.out.println("Current date: " + today);
}
Calculate the difference between the two dates
public static void main(String[] args) throws Exception
{
    Date currentTime = new Date();           // Get the current date and time
    Thread.sleep(3000);                      // Wait 3 seconds (3000 milliseconds)
    Date newTime = new Date();               // Get the new current time

    long msDelay = newTime.getTime() - currentTime.getTime(); // Calculate the difference
    System.out.println("Time difference is: " + msDelay + " in ms");
}
Check whether a certain amount of time has passed:
public static void main(String[] args) throws Exception
{
    Date startTime = new Date();

    long endTime = startTime.getTime() + 5000;  //    +5 seconds
    Date endDate = new Date(endTime);

    Thread.sleep(3000);              // Wait 3 seconds

    Date currentTime = new Date();
    if(currentTime.after(endDate))// Check whether currentTime is after endDate
    {
        System.out.println("End time!");
    }
}
Determine how much time has passed since the beginning of the day:
public static void main(String[] args) throws Exception
{
    Date currentTime = new Date();
    int hours = currentTime.getHours();
    int mins = currentTime.getMinutes();
    int secs = currentTime.getSeconds();

    System.out.println("Time since midnight " + hours + ":" + mins + ":" + secs);
}
Determine how many days have passed since the beginning of the year:
public static void main(String[] args) throws Exception
{
    Date yearStartTime = new Date();
    yearStartTime.setHours(0);
    yearStartTime.setMinutes(0);
    yearStartTime.setSeconds(0);

    yearStartTime.setDate(1);      // First day of the month
    yearStartTime.setMonth(0);     // January (the months are indexed from 0 to 11)

    Date currentTime = new Date();
    long msTimeDifference = currentTime.getTime() - yearStartTime.getTime();
    long msDay = 24 * 60 * 60 * 1000;  // The number of milliseconds in 24 hours

    int dayCount = (int) (msTimeDifference/msDay); // The number of full days
    System.out.println("Days since the start of the year: " + dayCount);
}
undefined
2
Task
Java Syntax, level 8, lesson 4
Locked
Code entry
Your attention, please! Now recruiting code entry personnel for CodeGym. So turn up your focus, let your fingers relax, read the code, and then... type it into the appropriate box. Code entry is far from a useless exercise, though it might seem so at first glance: it allows a beginner to get used to and remember syntax (modern IDEs seldom make this possible).

"The getTime() method returns the number of milliseconds stored in a Date object."

"The after() method checks whether the date we called the method on comes after the date passed to the method."

"The getHours(), getMinutes(), getSeconds() methods return the number of hours, minutes, and seconds, respectively, for the object on which they were called."

"Moreover, in the last example you can see that you can change the date/time stored in a Date object. We get the current time and date and then reset the hours, minutes, and seconds to 0. We also set January as the month and 1 as the day of the month. Thus, the yearStartTime object stores the date January 1 of the current year and the time 00:00:00."

"After that, we again get the current date (currentTime), calculate the difference between the two dates in milliseconds, and store it in msTimeDifference."

"Then we divide msTimeDifference by the number of milliseconds in 24 hours to get the number of full days from the beginning of the current year until today."

"Wow! This is so cool!"