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 time in java

In the below java program we will be using class GregorianCalendar of java.util package.

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

import java.sql.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;

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



Output:

2017/09/25 10:07:48


java.util.Calendar

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

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



Output:

2017/09/25 10:07:48


java.time.LocalDateTime

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

public class CurrentDate
{
   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:

2017/09/25 10:07:48


java.time.LocalDate

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

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



Output:

2017/09/25


Getting different timezone

Here we can get different time zone like GMT/UTC using getTimeZone() method which returns the specified TimeZone, or the GMT zone if the given ID cannot be understood.

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

public class Timezone
{
   public static void main(String[] args)
   {
      SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MMM-dd hh:mm:ss aa");
      // setting time zone
      sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
      System.out.println(sdf.format(new Date()));
   }
}



Output:

2015-Jan-26 06:05:00 AM


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