BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

How to get current date and time in java

Last Updated: September 11, 2022 by Chaitanya Singh | Filed Under: java

By using SimpleDateFormat and Date/Calendar class, we can easily get current date and time in Java. In this tutorial we will see how to get the current date and time using Date and Calendar class and how to get it in the desired format using SimpleDateFormat class.

Current date and time in Java – Two ways to get it

1) Using Date class

  • Specify the desired pattern while creating the instance of SimpleDateFormat.
  • Create an object of Date class.
  • Call the format() method of DateFormat class and pass the date object as a parameter to the method.
/* This will display the date and time in the format of 
 * 12/09/2017 24:12:35. See the complete program below
 */
DateFormat df = new SimpleDateFormat("dd/MM/yy HH:mm:ss");
Date dateobj = new Date();
System.out.println(df.format(dateobj));

2) Using Calendar class

  • Specify the desired pattern for the date and time. Similar to the step 1 of above method.
  • Create an object of Calendar class by calling getInstance() method of it.
  • Call the format() method of DateFormat and pass the Calendar.getTime() as a parameter to the method.
DateFormat df = new SimpleDateFormat("dd/MM/yy HH:mm:ss");
Calendar calobj = Calendar.getInstance();
System.out.println(df.format(calobj.getTime()));

Complete java code for getting current date and time

import java.util.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;

public class GettingCurrentDate {
   public static void main(String[] args) {
       //getting current date and time using Date class
       DateFormat df = new SimpleDateFormat("dd/MM/yy HH:mm:ss");
       Date dateobj = new Date();
       System.out.println(df.format(dateobj));

       /*getting current date time using calendar class 
        * An Alternative of above*/
       Calendar calobj = Calendar.getInstance();
       System.out.println(df.format(calobj.getTime()));
    }
}

Output:

21/10/17 22:13:06
21/10/17 22:13:06

Every time I run the above code it would fetch the current date and time.

Note: In order to get the output in above format I have specified the date/time pattern in the program (Note the first statement of the program DateFormat df = new SimpleDateFormat("dd/MM/yy HH:mm:ss");.

However if you want the output in any other date format, just modify the pattern accordingly. For e.g. To get the date only, the pattern would be dd-MM-yyyy: replace the statement with this one: DateFormat df = new SimpleDateFormat("dd-MM-yyyy");

While specifying the pattern be careful with the case. For e.g. ‘s’ (small s) represents second while ‘S'(Capital s) represents Millisecond.

At the end of this guide, I have shared the complete chart of symbols that we can use in patterns to get the date and time in desired format

Java – Getting current date and time in other timezone

The example we have seen above shows the date and time in local timezone. However we can get the date and time in different time zone as well such as UTC/GMT etc. In the following example we are displaying the time in GMT time zone.

import java.util.Date;
import java.text.SimpleDateFormat;
import java.util.TimeZone;
public class Example {
   public static void main(String[] args) {
	//"hh" in pattern is for 12 hour time format and "aa" is for AM/PM
	SimpleDateFormat dateTimeInGMT = new SimpleDateFormat("yyyy-MMM-dd hh:mm:ss aa");
	//Setting the time zone
	dateTimeInGMT.setTimeZone(TimeZone.getTimeZone("GMT"));
	System.out.println(dateTimeInGMT.format(new Date()));
   }
}

Output:

2017-Oct-21 06:03:42 PM

Update: To get the current date and time in Java 8 refer this guide.

Here is the complete chart which will help you to define the pattern in SimpleDateFormat.

Letter Date or Time Component Presentation Examples
G Era designator Text AD
y Year Year 1996; 96
Y Week year Year 2009; 09
M Month in year Month July; Jul; 07
w Week in year Number 27
W Week in month Number 2
D Day in year Number 189
d Day in month Number 10
F Day of week in month Number 2
E Day name in week Text Tuesday; Tue
u Day number of week (1 = Monday, …, 7 = Sunday) Number 1
a Am/pm marker Text PM
H Hour in day (0-23) Number 0
k Hour in day (1-24) Number 24
K Hour in am/pm (0-11) Number 0
h Hour in am/pm (1-12) Number 12
m Minute in hour Number 30
s Second in minute Number 55
S Millisecond Number 978
z Time zone General time zone Pacific Standard Time; PST; GMT-08:00
Z Time zone RFC 822 time zone -0800
X Time zone ISO 8601 time zone -08; -0800; -08:00

References:

  • Calendar javadoc
  • Date javadoc
  • SimpleDateFormat javadoc

Related Posts:

  1. Java – Display current time in Milliseconds format
  2. Java – Display time in 12 hour format with AM/PM
  3. Java – Convert Date to String
  4. Java – Convert String to Date
  5. Java – Add days to Date

Top Related Articles:

  1. Convert String to date in Java
  2. Date Formatting In Java With Time Zone
  3. Java 8 – Calculate days between two dates
  4. How to Parse Date in Desired format – Java Date
  5. Date validation in java

Tags: Java-Date

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Java Tutorial

Java Introduction

  • Java Index
  • Java Introduction
  • History of Java
  • Features of Java
  • C++ vs Java
  • JDK vs JRE vs JVM
  • JVM - Java Virtual Machine
  • First Java Program
  • Variables
  • Data Types
  • Operators

Java Flow Control

  • Java If-else
  • Java Switch-Case
  • Java For loop
  • Java while loop
  • Java do-while loop
  • Continue statement
  • break statement

Java Arrays

  • Java Arrays

OOPs Concepts

  • OOPs Concepts
  • Constructor
  • Java String
  • Static keyword
  • Inheritance
  • Types of inheritance
  • Aggregation
  • Association
  • Super Keyword
  • Method overloading
  • Method overriding
  • Overloading vs Overriding
  • Polymorphism
  • Types of polymorphism
  • Static and dynamic binding
  • Abstract class and methods
  • Interface
  • Abstract class vs interface
  • Encapsulation
  • Packages
  • Access modifiers
  • Garbage Collection
  • Inner classes
  • Static import
  • Static constructor

Java Exception Handling

  • Exception handling
  • Java try-catch
  • Java throw
  • Java throws
  • Checked and Unchecked Exceptions
  • Jav try catch finally
  • Exception Examples
  • Exception Propagation

Collections Framework

  • Collections in Java
  • Java ArrayList
  • Java LinkedList
  • Java Vector
  • Java HashSet
  • Java LinkedHashSet
  • Java TreeSet
  • Java HashMap
  • Java TreeMap
  • Java LinkedHashMap
  • Java Queue
  • Java PriorityQueue
  • Java Deque
  • Comparable interface
  • Comparator interface
  • Collections Interview Questions

MORE ...

  • Java Scanner Class
  • Java 8 Features
  • Java 9 Features
  • Java Conversion
  • Java Date
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations
  • Java main method
  • Java Interview Q

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap