« Previous Page Next Page »

Introduction to Java Numbers Class (Integer, Long, Byte, Double, Float, Short).



In this tutorial we are going to learn about Java Date Class and methods of Java Date Class. In this this tutorial we will also learn how to get the current date and time in java. At last, we will discuss examples of Java Date and time i.e. java.util.

Java Date Class:

Java Date class is available in the java.util package which also encapsulates the current date and time.

The Date class supports two constructors as shown in the following table.

Date() This constructor initializes the current Java date and time.
Date(long millisec) It is used to count the number of milliseconds elapsed.

Java Date Class Methods:

boolean after(Date date) It returns true when true when the invoked Date object is later than specified date.
boolean before(Date date) It is same as the above with the only difference that it is true when the date is earlier than the Date object.
Object clone( ) It duplicates the invoked object.
int compareTo(Date date) It compares the value of the specified object to the invoking object, if it is equal then returns zero, if later then than positive and negative when it is earlier.
int compareTo(Object obj) It throws a ClassCastException if obj is not a Date class.
boolean equals(Object date) It is true only when the invoking Date object returns the same time and date, otherwise it returns false.
long getTime( ) It returns the number of milliseconds elapsed.
int hashCode( ) For am invoking object, it returns a hash code.
void setTime(long time) It sets the date and time as specified by Time.
String toString( ) Converts the Date object into string.

Getting Current Date and Time in Java:

To get current Date and Time in Java, you You can use a simple Date object with toString() method to print the current date and time as follows -


import java.util.Date;
public class DateDemo {

   public static void main(String args[]) {
      // Instantiate a Date object
      Date date = new Date();

      // display time and date using toString()


Tue Oct 09 23:46:01 IST 2018

Date Formatting Using SimpleDateFormat:

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


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

public class DateDemo {

   public static void main(String args[]) {
      Date dNow = new Date( );
      SimpleDateFormat ft = 
      new SimpleDateFormat ("E yyyy.MM.dd 'at' hh:mm:ss a zzz");

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


Thu 2018.10.11 at 11:56:29 PM IST

Simple DateFormat Format Codes:

To specify the time format, use a time pattern string. In this pattern, all ASCII letters are reserved as pattern letters, which are defined as the following -

Character Description Example
G Era designator AD
y Year in four digits 2001
M Month in year July or 07
d Day in month 10
h Hour in A.M./P.M. (1~12) 12
H Hour in day (0~23) 22
m Minute in hour 30
s Second in minute 55
S Millisecond 234
E Day in week Tuesday
D Day in year 360
F Day of week in month 2 (second Wed. in July)
w Week in year 40
W Week in month 1
a A.M./P.M. marker PM
k Hour in day (1~24) 24
K Hour in A.M./P.M. (0~11) 10
z Time zone Eastern Standard Time
' Escape for text Delimiter
" Single quote `

The End:

Thank you for reading this article, and if you have any problem, have a another better useful solution about this article, please write message in the comment section. We will be very happy to hear that.

If you like my tutorial, please follow us on facebook, Google + and Twitter. I need your support to continue.

« Previous Page Next Page »