How to get current date and time in java

Let’s learn how to get current date and 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.

how to get current date and time in java

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

java.util.Date class

Get current date time using java.util.Date class object and System.currentTimeMillis() method. Here’s how to get current date and time using Date class and System.currentTimeMillis() method.

import java.util.Date;
public class UsingDateClass
{
   public static void main(String[] args) 
   {
      Date dt = new Date();
      System.out.println(dt);
      long milliSeconds = System.currentTimeMillis();  
      Date date = new 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 class

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

Calendar.getTime() method returns object of class Date. Calendar.getInstance() method returns object of class Calendar. Let’s learn how to get current date and time using Calendar.getTime() method.

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


Output:

Tue Dec 21 20:26:43 IST 2016


java.time.LocalDateTime class

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 how to get current date and time using LocalDateTime.now() method.

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


Output:

2014-02-15T13:00:01.814


java.time.LocalDate class

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 how to get current date and time using LocalDate.now() method.

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


Output:

2015-02-26


java.text.SimpleDateFormat class

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

Let’s see how to get current date and time using SimpleDateFormat class.

import java.text.SimpleDateFormat;
import java.util.Date;
public class UsingSimpleDateFormat
{
   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 class

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

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

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class UsingDateTimeFormatter
{
   public static void main(String[] args)
   {
      DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");
      LocalDateTime ldt = LocalDateTime.now();
      System.out.println(dtf.format(ldt));
   }
}


Output:

2005/05/23 18:23:14


how to get only time in java

To get only time in java use SimpleDateFormat class. Here’s how to get only time in java.

import java.text.SimpleDateFormat;
import java.util.Date;
public class GetOnlyTime
{
   public static void main(String[] args)
   {
      Date date = new Date();
      SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
      System.out.println("Get only time in java- " + sdf.format(date));
   }
}


Output:

Get only time in java- 14:23:05


Also read – HashSet in java