Date time java program

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

Date Time Java Program

Here in this post we will learn java program to get current date/time. In the below 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


Current date and time in default format

Also read – convert date to string java program

import java.time.LocalDateTime;

public class DateTimeDemo 
{
   public static void main(String[] args) 
   {
      // storing current date and time
      LocalDateTime current = LocalDateTime.now();
      // converted from LocalDateTime object to string internally
      System.out.println("Current date/time : " + current);
   }
}

Output:

Current date/time : 2017-05-01T18:43:00.352


Current date and time with pattern

Here DateTimeFormatter object define a pattern of format Year-Month-Day Hours:Minutes:Seconds.Milliseconds. Then using LocalDateTime’s format() method to print formatted string output.

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

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

Output:

Current date/time : 2017-05-01 19:01:21.064


Current date and time using predefined constants

To get current ISO date as output use BASIC_ISO_DATE. It’s a predefined format constant.

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

public class CurrentDateDemo
{
   public static void main(String[] args)
   {
      LocalDateTime current = LocalDateTime.now();
      DateTimeFormatter dtf = DateTimeFormatter.BASIC_ISO_DATE;
      String strFormatted = current.format(dtf);
      System.out.println("Current date : " + strFormatted);
   }
}

Output:

Current date : 20170501


Current date and time in local style

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

public class DateTime 
{
   public static void main(String[] args) 
   { 
      LocalDateTime current = LocalDateTime.now();
      DateTimeFormatter dtf = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM);
      String strFormatted = current.format(dtf);
      System.out.println("Current date/time : " + strFormatted);
   }
}

Output:

Current date/time : 5 Jan, 2017 7:19:23 PM

Related Posts