Java Date and Time

Java Date Class

The Date class available in the java.util package, this class encapsulates the current date and time.

The Date class supports the two constructors. In which the first constructor initializes the object with the current date and time. Following is the syntax:

Date( )

The following constructor accepts one argument that equals the number of milliseconds that have elapsed since midnight, January 1, 1970

Date(long millisec)

Java Date and Time Example

Here are some examples on date and time in Java. Use Date object with method named toString() to print the current date/time.

/* Java Program Example - Java Date and Time
 * This program get the current date and time
 * and then display it on the screen
 */

import java.util.Date;
  
class GetCurrentDateTime
{
   public static void main(String args[])
   {       
      Date date = new Date();    
      System.out.println(date.toString());
   }
}

Here is the sample output produced by the above Java program:

java date time

Format Date in Java

Here is an example program, showing how to format date in Java:

/* Java Program Example - Java Date and Time
 * This program format the date using the
 * SimpleDateFormat
 */

import java.util.*;
import java.text.*;

class JavaDateTime
{
   public static void main(String args[])
   {
      Date dob = new Date( );
      SimpleDateFormat sdob = new SimpleDateFormat ("E yyyy.MM.dd 'at' hh:mm:ss a zzz");
      System.out.println("Current Date : " + sdob.format(dob));
   }   
}

Here is the output of the above Java program:

date and time

Here is another program to format date using printf in Java:

/* Java Program Example - Java Date and Time
 * This program format the date using the
 * printf() method
 */

import java.util.Date;

class JavaDateTime
{
   public static void main(String args[])
   {
      Date date = new Date();
      String sob = String.format("Current Date/Time : %tc", date);
      System.out.printf(sob);
   }
}

Here is the output produced by the above Java program:

get current date and time in java

Here is an example showing how to parse strings into dates in Java:

/* Java Program Example - Java Date and Time
 * Parsing Strings into Dates
 */

import java.util.*;
import java.text.*;

class JavaDateTime
{
   public static void main(String args[])
   {
      SimpleDateFormat sdfob = new SimpleDateFormat ("yyyy-MM-dd"); 
      String input = args.length == 0 ? "1818-11-11" : args[0]; 
      System.out.print(input + " Parses as "); 
      Date t; 
      try
      { 
         t = sdfob.parse(input); 
         System.out.println(t); 
      }
      catch (ParseException e)
      { 
         System.out.println("Unparseable using " + sdfob); 
      }
   }
}

It will produce the following result:

java parsing strings into dates

Here is an example showing how to sleep for a while in Java:

/* Java Program Example - Java Date and Time */

import java.util.*;

class JavaDateTime
{
   public static void main(String args[])
   {
      try
      { 
         System.out.println(new Date( ) + "\n"); 
         Thread.sleep(5*60*10); 
         System.out.println(new Date( ) + "\n"); 
      }   
      catch (Exception e)
      {
         System.out.println("Got an exception..!!"); 
      }
   }
}

Here is the sample output of this Java program:

java sleeping for a while

Measure Elapsed Time in Java

Here is an example showing how to measure an elapsed time in Java:

/* Java Program Example - Java Date and Time */

import java.util.*;

class JavaDateTime
{
   public static void main(String args[])
   {
      try
      {
         long start_time = System.currentTimeMillis();
         System.out.println(new Date() + "\n");
         Thread.sleep(5*60*10);
         System.out.println(new Date() + "\n");
         long end_time = System.currentTimeMillis();
         long elapsed_time = end_time - start_time;
         System.out.println("Difference is : " + elapsed_time);
      }
      catch (Exception e)
      {
         System.out.println("Got an exception!");
      }
   }
}

This is the output produced when you run the above Java program:

java measure elapsed time

GregorianCalendar Class in Java

Here is another program uses GregorianCalendar class in Java:

/* Java Program Example - Java Date and Time */

import java.util.*;

class JavaDateTime
{
   public static void main(String args[])
   {
      String months[] = {
            "Jan", "Feb", "Mar", "Apr", "May", "Jun",
            "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
      int year;
      GregorianCalendar gcalendar = new GregorianCalendar();
      System.out.print("Date : ");
      System.out.print(months[gcalendar.get(Calendar.MONTH)]);
      System.out.print(" " + gcalendar.get(Calendar.DATE) + " ");
      System.out.println(year = gcalendar.get(Calendar.YEAR));
      System.out.print("Time : ");
      System.out.print(gcalendar.get(Calendar.HOUR) + ":");
      System.out.print(gcalendar.get(Calendar.MINUTE) + ":");
      System.out.println(gcalendar.get(Calendar.SECOND));
      if(gcalendar.isLeapYear(year))
      {
         System.out.println("This year is a Leap Year.");
      }
      else
      {
         System.out.println("This year is not a Leap Year.");
      }   
   }
}

Following is the output produced by the above Java program:

gregorian calendar

Here is another program that you may like, Print Time and Date in Java.

Java Online Test


« Previous Tutorial Next Tutorial »