Date Time Java Program

Sharing is healthy !!

Hey guys!! Welcome to flower brackets blog. Date time java program is one of the common, rather classic program.

In general date time program is written and executed in many programming languages.

Here in this post we will learn how to get the current date in java.

In this program we will be using class GregorianCalendar of java.util package. Let’s see java program to get current date and time,

Example: Date Time Java Program

Date Time Java Program

Also Read – Convert Date To String Java Program

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


Using simpledateformat class

Let’s see how to get the time and date in java using simpledateformat class,

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

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

Output:

Simpledateformat Time – 2016/12/29 16 : 25 : 52


Using calendar class

Here we are going to learn java calendar current date using calendar class,

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

public class DateTimeExample
{
   public static void main(String[] args)
   {
      DateFormat df = new SimpleDateFormat("yyyy / MM / dd HH : mm : ss");
      Calendar cale = Calendar.getInstance();   
      System.out.println("Today's date calendar - " + df.format(cale.getTime()));
   }
}

Output:

Today’s date calendar – 2016/12/29 19 : 25 : 52


Using localdatetime and localdate class

Let’s learn today date in java using localdatetime and localdate class,

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

public class DateTimeExample
{
   public static void main(String[] args)
   {
      DateTimeFormatter dateFormat1 = DateTimeFormatter.ofPattern("yyyy / MM / dd HH : mm : ss");    
      LocalDateTime ldt = LocalDateTime.now();   
      System.out.println("Java get current time date - " + dateFormat1.format(ldt));

      DateTimeFormatter dateFormat2 = DateTimeFormatter.ofPattern("yyyy / MM / dd");
      LocalDate ld = LocalDate.now();    
      System.out.println("Java current date - " + dateFormat2.format(ld));
   }
}

Output:

Java get current time date – 2016/12/29 21 : 25 : 52
Java current date – 2016/12/29


conclusion

So this is all about how to get time in java. I hope you have understood the how to get the current time in java.

You can subscribe to my blog flower brackets if haven’t already.

Do share this article if you like.

Sharing is healthy !!