Skip to content

How To Get Current Date And Time In Java ?

In this article, you are going to learn how to get current date and time in java programming language.

The availability of Date class in the java.util package enables a programmer to easily get current date and time wherever needed. It includes several methods through which you can access the current date and time.

Also Read:- Regular Expressions in Java Programming Language

It also includes formatting methods to format date and time which we will discuss later in this article.

We have listed below some of the classes and methods used to get current date and time in java programming language.

  1. toString() Method
  2. java.util.Date Class
  3. java.time.LocalDate Class
  4. java.time.LocalTime Class
  5. java.time.LocalDateTime Class

[1] TOSTRING() METHOD

Example:

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

Output:

Tue Jun 06 06:38:32 GMT 2017

[2] JAVA.UTIL.DATE CLASS

Example:

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

Output:

Tue Jun 06 06:46:56 GMT 2017

[3] JAVA.TIME.LOCALDATE CLASS

Example:

class LocalDate {
public static void main(String [] args) {
System.out.println(“Current Date is :” + java.time.LocalDate.now()) ;
}
}

Output:

Current Date is : 2017-06-06

[4] JAVA.TIME.LOCALTIME CLASS

Example:

class LocalTime {
public static void main(String [] args) {
System.out.println(“Current Time is :“ + java.time.LocalTime.now()) ;
}
}

Output:

Current Time is : 06:57:46.618

[5] JAVA.TIME.LOCALDATETIME CLASS

Example:

class LocalDateTime {
public static void main(String [] args) {
System.out.println(“Current Date and Time is :“ + java.time.LocalDateTime.now()) ;
}
}

Output:

Current Date and Time is :2017-06-06T07:08:07.476

DATE AND TIME FORMATTING IN JAVA

We have shown below two methods to format date and time in java programming language.

(1) SIMPLEDATEFORMAT METHOD

Example:

class DateFormat {

public static void main(String args[]) {
Date date = new Date( ) ;
SimpleDateFormat dateft = new SimpleDateFormat (“G dd.MM.yyyy hh:mm:ss a”) ;

System.out.println(“Current Date and Time : “ + dateft.format(date)) ;
}
}

Output:

Current Date and Time: AD 07.06.2017 08:04:44 AM

(2) PRINTLN METHOD

Example:

class DateFormat {
public static void main(String args[]) {
Date date = new Date() ;
String st = String.format(“Current Date : %tD”, date ) ;
System.out.printf(st) ;
}
}

Output:

Current Date : 06/07/17

DATE/ TIME FORMATTING CODES

        a        d

 

D

E

G

h

H

m

M

s

y

w

W

z

                      A.M/P.M                  Day in Month

 

Day in Year

Day in Week

Era Determiner(AD/BC)

Hour(AM/PM)

Hour(Day)

Minute in hour

Month in year

Seconds in minutes

Year(four digits)

Week in year

Week in month

Time Zone

DATE/ TIME CONVERSION CODES

        a        b

 

B

c

C

D

e

F

H

I

j

k

L

m

M

N

P

p

Q

r

R

s

S

T

y

Y

z

Z

                      Week day(three letter)                      Month(three letter)

 

Month(full)

Date and Time(full)

Year(first two digits)

Date(mon/day/year)

Day(two digit,no leading zeroes)

Date(ISO 8601)

Hour(two digit, leading zeroes, 00-23)

Hour(two digit, leading zeroes, 01-12)

Day of Year(three digit, leading zeroes)

Hour(two digit, no leading zeroes, 0-23)

Milliseconds(three digits, leading zeroes)

Month(two digit, leading zeroes)

Minutes(two digits, leading zeroes)

Nanoseconds(nine digit, leading zeroes)

AM/PM(uppercase)

AM/PM(lowercase)

Milliseconds(since 1970-01-01 00:00:00 GMT)

Time(12-hour, with seconds)

Time(24-hour, no seconds)

Seconds(since 1970-01-01 00:00:00 GMT)

Seconds(two digit, leading zeroes)

Time(24-hour, with seconds)

Year(last two digits, leading zeroes)

Year(four digit, leading zeroes)

RFC 822 numeric offset(GMT)

Time Zone

Also Read: Nested Classes in Java Programming Language
We have provided you the description on How to get Current Date and Time in Java Programming Language. Hope you like this post. For more updates and related information, stay connected to our blogs on java programming language.

Facebook
Twitter
LinkedIn
Pinterest