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

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

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

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

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

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

Get current date time using LocalDateTime.now() method. class DateTimeFormatter 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


without time

To get current date without time use LocalDate class. This class represent a date without a time-zone in the ISO-8601 calendar system.

Let’s see an example to get current date without time.

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
public class DateWithoutTime 
{
   public static void main(String[] args) 
   {
      LocalDate ld = LocalDate.now();
      DateTimeFormatter format = DateTimeFormatter.ofPattern("MM/dd/yyyy");
      System.out.println(ld.format(format));
   }
}


Output:

23/05/2005


java get current time in milliseconds

To get current time in milliseconds use currentTimeMillis() method of System class.

Also read – ArrayList in java

currentTimeMillis() method returns the difference, measured in milliseconds, between the current time and midnight, January 1, 1970 UTC.

Let’s see an example on java get current time in milliseconds.

public class CurrentTimeInMilliseconds
{
   public static void main(String[] args) 
   {
      System.out.print("java get current time in milliseconds: ");
      System.out.println(System.currentTimeMillis());
   }
}


Output:

java get current time in milliseconds: 1585665774620


get current time based on timezone in java

Let’s learn to get current time based on timezone in java.

import java.time.LocalTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
public class BasedOnTimezone 
{
   public static void main(String[] args) 
   {
      System.out.println("get current time based on timezone in java: ");
      ZoneId zoneId = ZoneId.of("America/New_York");
      LocalTime lt = LocalTime.now(zoneId);
      DateTimeFormatter dtf = DateTimeFormatter.ofPattern("HH:mm:ss");
      String strFormatted = lt.format(dtf);
      System.out.println("Current time in Los Angeles: " + strFormatted);
   }
}


Output:

get current time based on timezone in java:
Current time in Los Angeles: 11:00:05


java 8 get current date

Let’s learn java 8 get current date.

import java.time.LocalDate;
public class CurrentDateJava8
{
   public static void main(String[] args)
   {
      System.out.println("java 8 get current date: " + LocalDate.now());
      System.out.println("java 8 get current Time: " + LocalTime.now());
      System.out.println("java 8 get current date & time: " + LocalDateTime.now());
   }
}


Output:

java 8 get current date: 2005-05-05
java 8 get current time: 14:23:05.243202900
java 8 get current date & time: 2005-05-23T14:23:05.244200700


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


java get current timestamp

Here’s how to get current timestamp.

import java.sql.Timestamp;
public class GetTimestamp
{
   public static void main(String[] args)
   {
      Timestamp ts = new Timestamp(System.currentTimeMillis());
      System.out.println("java get current timestamp: " + ts);
   }
}


Output:

java get current timestamp: 2005-05-14 14:05:23.483