Date and Time Functions in Java

Java provides the Date class available in java.util package, this class encapsulates the current date and time. Java Dates are LocalDate, Represents a date (year, month, day (yyyy-MM-dd)). Java Time are LocalTime, Represents a time (hour, minute, second and nanoseconds (HH-mm-ss-ns)). Calendar class in Java is an abstract class that provides methods for converting date between a specific instant in time and a set of calendar fields such as MONTH, YEAR, HOUR, etc

This program demonstrates the usage of Date and Calendar classes in Java.

  • The program begins by printing the current system time in milliseconds using the System.currentTimeMillis() method. It then calculates the number of years that have passed since the epoch time in seconds, by dividing the current system time by the number of milliseconds in a second, minute, hour, day and year. This value is printed to the console.
  • Next, a Date object is created using the default constructor, which sets the date to the current system time. Various methods are called on this object to print its various components such as date, day, month, year, time, hours, minutes and seconds.
  • Then, a GregorianCalendar object is created which represents the current date and time in the default time zone. The isLeapYear method is called on this object to check if the current year is a leap year or not. Various methods are called on this object to print the date, month, year, day of the week, and day of the year.

Overall, this program demonstrates the usage of Date and Calendar classes to work with dates and times in Java.

Source Code

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
public class DataTimeDemo {
    public static void main(String[] args) {
        long year=(System.currentTimeMillis()/1000/60/60/24/365);
        //Date d =new Date(System.currentTimeMillis());
        //Java 8
        Date d =new Date();//MM-DD-YYYY
        System.out.println("Date    : "+d.getDate());
        System.out.println("Day     : "+d.getDay()); //0 Sunday to 7-Saturday
        System.out.println("Month   : "+d.getMonth());//0 Jan to 11 Dec
        System.out.println("Year    : "+(d.getYear()+1900));
        System.out.println("Time    : "+d.getTime());
        System.out.println("Hours   : "+d.getHours());
        System.out.println("Minutes : "+d.getMinutes());
        System.out.println("Seconds : "+d.getSeconds());
        GregorianCalendar o =new GregorianCalendar();
        System.out.println("Date  : "+o.get(Calendar.DATE));
        System.out.println("Month : "+o.get(Calendar.MONTH));
        System.out.println("Year  : "+o.get(Calendar.YEAR));
        System.out.println("Day of Week  : "+o.get(Calendar.DAY_OF_WEEK)); //1-Sun to 7-Sat
        System.out.println("Day of Year  : "+o.get(Calendar.DAY_OF_YEAR));


Wed Mar 16 14:43:33 IST 2022
Date    : 16
Day     : 3
Month   : 2
Year    : 2022
Time    : 1647422013422
Hours   : 14
Minutes : 43
Seconds : 33
Date  : 16
Month : 2
Year  : 2022
Day of Week  : 4
Day of Year  : 75
To download raw file Click Here

Basic Programs