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 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.


java.util.Date

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

import java.util.Date;

public class DateTimeDemo 
{
   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.

import java.util.Calendar;
import java.util.Date;

public class DateTime 
{
   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 java program,

import java.time.LocalDateTime;

public class CurrentDate
{
   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 the java program,

import java.time.LocalDate;

public class LocalDateDemo
{
   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 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.

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class DateTimeDemo 
{
   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.

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


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