Date and Time in Java

The Java also provides the Date class available in java.util package, this class encapsulates the current date with year and time.

The Date class supports two main constructors as shown below :

  • Date(): By this constructor we can easily initialize the object with the current date with year and time.
  • Date(long millisecond): By using this constructor we can easily accept an argument that equals the number of milliseconds that have elapsed since midnight, January 1, 1970.

There are some useful methods of the date class :

  • boolean before(Date date): This method returns only true if the invoking the Date object contains a date that is earlier the one specified by date, otherwise it returns false.
  • boolean after(Date date): This return true if the invoking the Date object contains a date that is later than the one specified by date, otherwise it returns false respectively.
  • Long getTime(): It returns the number of milliseconds that have elapsed since January 1, 1970.
  • Int hashCode(): Only return a hash code for the invoking object.
  • String toString(): It used converts the invoking Date object into a string and returns the result.
  • Object clone(): It only used for duplicates the invoking Date object.

Getting Current Date with Year and Time

Basically, this is the easiest way to get current date with year and time in Java.  Just create a simple Date object with toString() method to print the current date with the year as follows :

Example:

import java.util.Date;


public class DateDemo1 {


   public static void main(String args[]) {

      // Just initialize a Date object

      Date date = new Date();


      // And display time and date using toString()

      System.out.println("Get Now Dates : " +date.toString());

   }}


Output : Get Now Dates :Tue Mar 17 03:13:49 UTC 2020

Date Formatting Using SimpleDateFormat in java

This class SimpleDateFormat is used for formatting and parsing dates in a locale-sensitive and it allows to start by choosing any user-defined patterns for date-time formatting.

Example :

import java.util.*;

import java.text.*;


public class DateDemo {


   public static void main(String args[]) {

      Date dNow = new Date( );

      SimpleDateFormat sr = new SimpleDateFormat ("E yyyy.MM.dd '@' hh:mm:ss a zzz");


      System.out.println("Current Date: " + sr.format(dNow));

   }

}


Output : Current Date: Tue 2020.03.17 @ 03:11:43 AM UTC

DateFormat Codes in java

It helps you to specify the time format use a time pattern string. Basically, all ASCll letters are reserved as pattern letters which are given below :

Character  Uses Example
y Year in four digits 2020
M Month in year March or 03
d Day in Month 17
h Hour in P.M or AM 11
H Hours in day ( 0 ~ 23) 21
m For Minute 30
s For Second 50
S For Millisecond 233
w Week in year 30
W Week in month 1
a A.M./P.M. marker AM
Used for escape text Delimiter
D Day in year 360

The GregorianCalendar Class

The GregorianCalendar which is concrete implementation of a Calendar class which you may be familiar.

The  method getInstance( ) of Calendar which returns a GregorianCalendar initialized with the current date and time in the default locale and time zone which defines two fields: AD and BC.

This represents the two eras AD or BC defined by the Gregorian calendar.

Here is some main Constructor :

1). GregorianCalendar(): Only constructs a default GregorianCalendar using the current time in the default time zone with the default locale.

2). GregorianCalendar(Locale locale): This constructs GregorianCalendar based on the current time in the default time zone with the given locale.

3). GregorianCalendar(TimeZone zone): This constructs GregorianCalendar based on the current time in the given time zone with the default locale.