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

In general date time program is written and executed in many programming languages. Here in this post we will learn java program to print date and time.

In this program we will be using class GregorianCalendar of java.util package,

Example: date time 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


Java program to print date and time using Date class

Now we will see how to use Date class in java. We can use Date class to display/print date and time and as well as manipulating date and time in java.

In addition to that we will see how we can format date and time in java. First of all we need to import Date class from package java.util.

Also Read – Convert Date To String Java Program

Then we have to specify the date time pattern we want creating instance of SimpleDateFormat. After that create object of Date class and call the format() method of DateFormat class.

Finally pass object of Date class as parameter to format method. Here’s the program,

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


Java program to print date and time using calendar class

Let’s look at how to use calendar class. Java has introduced calendar object to use whenever you would want to do these kind of operation.

So let’s start using the calendar object ,

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

Meanwhile you can change the pattern if you want the date format in any other form. Say for example, DateFormat df = new SimpleDateFormat(“dd/MM/yy HH:mm:ss”);

NOTE : Be aware while using “s” (lowercase “s”) and “S” (uppercase “S”). Because lowercase “s” represent second and uppercase “S” represent millisecond.


Java program to get current date and time

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

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

public class CurrentDate
{
   public static void main(String[] args)
   {
      // "yyyy" pattern for year, MMM pattern for month, "dd" for date
      // "hh" pattern for 12 hour time format, "aa" is for AM/PM and "ss" for second 
      SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MMM-dd hh:mm:ss aa");
      // here we are setting the time zone
      // we set the time zone to UTC/GMT etc.
      sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
      System.out.println(sdf.format(new Date()));
   }
}

Output:

2016-Nov-28 06:54:09 AM


Conclusion

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

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

Do share this article if you like.

Related Posts