Date Time Java Program

Sharing is healthy !!

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 print or display current system date and time in java.

In this program we will be using class GregorianCalendar of java.util package. Let’s get into example,

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

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("Current date and time - " + simpleDate.format(dt));
   }
}

Output:

Current date and time – 2016/12/29 16 : 25 : 52


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("Current date and time - " + df.format(cale.getTime()));
   }
}

Output:

Current date and time – 2016/12/29 19 : 25 : 52


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("Current date and time - " + dateFormat1.format(ldt));

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

Output:

Current date and time – 2016/12/29 21 : 25 : 52
Current date – 2016/12/29


conclusion

So this is all about date time java program. I hope you have understood the java program.

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

Do share this post if you like.

Sharing is healthy !!