How to get current date time in java

Let’s learn how to get current date time in java.

how to get current date time in java

How to get current date and time in java

How to get current date in java is the one of the frequently asked java program question.

In this post we are going to learn different ways to get current date and time.

GregorianCalendar class with example

Get current date time using Gregorian class,

import java.util.*;

public class DateTime
{
   public static void main(String[] args)
   {
      int year, month, day;
      int hour, minute, second;
      GregorianCalendar dt = new GregorianCalendar();

      year = dt.get(Calendar.YEAR);
      month = dt.get(Calendar.MONTH);
      day = dt.get(Calendar.DAY_OF_MONTH);

      hour = dt.get(Calendar.HOUR);
      minute = dt.get(Calendar.MINUTE);
      second = dt.get(Calendar.SECOND);

      System.out.println("Current date is " + day + "/" + (month + 1) + "/" + year);
      System.out.println("Current time is " + hour + ":" + minute + ":" + second);

   }
}



Output:

Current date is 29/12/2016
Current time is 7:28:17


java.util.Date

Get current date time using java.util.Date class object and System.currentTimeMillis() method.

public class DateTimeDemo 
{
   public static void main(String[] args) 
   {
      // get current date and time using java.util.Date class
      java.util.Date dt = new java.util.Date();
      System.out.println(dt);
      // java get current time in milliseconds
      long milliSeconds = System.currentTimeMillis();  
      java.util.Date date=new java.util.Date(milliSeconds);  
      System.out.println(date);       
   }
}



Output:

Tue Dec 21 20:26:43 IST 2016
Tue Dec 21 20:26:43 IST 2016


java.util.Calendar

Get current date using java.util.Calendar class. Calendar class is used to get object of class Date.

java.util.Calendar.getTime() method returns object of class java.util.Date. java.util.Calendar.getInstance() method returns object of class Calendar.

public class DateTime 
{
   public static void main(String[] args) 
   { 
      java.util.Date date = java.util.Calendar.getInstance().getTime();  
      System.out.println(date);
   }
}



Output:

Tue Dec 21 20:26:43 IST 2016


java.time.LocalDateTime

Get date time using LocalDateTime.now() method. LocalDateTime.now() method returns current date-time using the system clock and instance of LocalDateTime class.

Let’s see java program,

public class CurrentDate
{
   public static void main(String[] args)
   {
      System.out.println(java.time.LocalDateTime.now());
   }
}



Output:

2014-02-15T13:00:01.814


java.time.LocalDate

Get current date using LocalDate.now() method. LocalDate.now() method returns instance of LocalDate class and prints current date using the system clock.

Here’s the java program,

public class LocalDateDemo
{
   public static void main(String[] args)
   {
      System.out.println(java.time.LocalDate.now());
   }
}



Output:

2015-02-26


java.text.SimpleDateFormat

Getting current date time using SimpleDateFormat class. SimpleDateFormat class constructs a SimpleDateFormat using the given pattern.

Let’s see java program,

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

public class Timezone
{
   public static void main(String[] args)
   {
      SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");  
      Date date = new Date();  
      System.out.println(sdf.format(date));
   }
}



Output:

20/02/2012 13:42:40

java.time.format.DateTimeFormatter

Get current date time using LocalDateTime.now() method. class DateTimeFormatter was included in JDK 1.8.

LocalDateTime.now() method gets the current date-time using the system clock and returns the instance of LocalDateTime class.


Also read – arrayList in java


Defining different patterns

LetterDate/time componentDisplayExample
XTime zoneISO 8601 time zone-08; -0800; -08:00
ZTime zoneRFC 822 time zone-0800
zTime zoneGeneral time zonePacific Standard Time; PST; GMT-06:00
SMillisecondNumber978
sSecond in minuteNumber55
mMinute in hourNumber30
hHour in am/pm (1-12)Number12
KHour in am/pm (0-11)Number0
kHour in day (1-24)Number24
HHour in day (0-23)Number0
aAm/pm markerTextPM
uDay number of week (1 = Monday, …, 7 = Sunday)Number1
EDay name in weekTextMonday; Mon
FDay of week in monthNumber2
dDay in monthNumber10
DDay in yearNumber189
WWeek in monthNumber2
wWeek in yearNumber27
MMonth in yearMonthJune; Jun; 06
YWeek yearYear2005; 05
yYearYear1998; 98
GEra designatorTextAD