Open In App

Java Program to Display Current Hour and Current Minute

Last Updated : 06 Jul, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

The difference between Date and Calendar is that the Date class operates with a specific instant in time and Calendar operates with a difference between two dates. The Calendar class gives you the possibility for converting between a specific instant in time and a set of calendar fields such as HOUR, YEAR, MONTH, DAY_OF_MONTH. Manipulation over the calendar fields taken for instance getting backdate when the organization was formed or birthdays.

Calendar Class

It is used to display the date and time and manipulate date and time in java and in addition to this it is also used for formatting date and time class in java across time zone associated data. So in order to import this class from a package called “java.utils”. After importing this class one can create an object of the Date class in order to print the current date and time. Now in order to print the default date and time simply call the print command using toString() method to get the current date and time.

java.util.Calendar.get() method is a method of java.util.Calendar class. The Calendar class provides some methods for implementing a concrete calendar system outside the package. Some examples of Calendar fields are : YEAR, DATE, MONTH, DAY_OF_WEEK, DAY_OF_YEAR, WEEK_OF_YEAR, MINUTE, SECOND, HOUR, AM_PM, WEEK_OF_MONTH, DAY_OF_WEEK_IN_MONTH, HOUR_OF_DAY.

Syntax :

public int get(int field)

Here, field represents the given calendar 
field and the function returns the value of 
given field.
Approaches: 

  1. Using Calendar class with Format specifiers
  2. Using Calendar class without Format specifier

Approach 1: Using Calendar class with Format specifiers

java.util.Calendar.get() method is a method of java.util.Calendar class. The Calendar class provides some methods for implementing a concrete calendar system outside the package. Format specifiers begin with a percent character (%) and terminate with a “type character” which indicates the type of data (int, float, etc.) that will be converted in the basic manner in which the data will be represented (decimal, hexadecimal, etc.)

SPECIFIER CONVERSION APPLIED
%% Inserts a % sign
%t %T Time and Date
%tM Minutes

Implementation: Below is the implementation of the program using the above format specifiers.

Java




// Java Program to Display
// current hour and minute
 
// Importing java Date class libraries
import java.util.Calendar;
import java.util.Formatter;
 
class GFG {
 
    // Main driver method
    public static void main(String args[])
    {
 
        // Create Formatter class object
        Formatter format = new Formatter();
 
        // Creating a calendar
        Calendar gfg_calender = Calendar.getInstance();
 
        // Displaying hour using Format class using  format
        // specifiers
        // '%tl' for hours and '%tM' for minutes
        format = new Formatter();
        format.format("%tl:%tM", gfg_calender,
                       gfg_calender);
 
        // Printing the current hour and minute
        System.out.println(format);
    }
}


 
 

Output

9:04

Approach 2: Using Calendar class without Format specifiers

 

java.util.Calendar.get() method is a method of java.util.Calendar class. The Calendar class provides some methods for implementing a concrete calendar system outside the package. Some examples of Calendar fields are: YEAR, DATE, MONTH, DAY_OF_WEEK, DAY_OF_YEAR, WEEK_OF_YEAR, MINUTE, SECOND, HOUR, AM_PM, WEEK_OF_MONTH, DAY_OF_WEEK_IN_MONTH, HOUR_OF_DAY.

 

Below is a java example to display the current date and time without using format specifiers: 

 

Java




// Java Program to Display
// current hour and minute
 
// Importing java Date class libraries
import java.util.Calendar;
import java.util.Formatter;
 
class GFG {
 
    // Main driver method
    public static void main(String args[])
    {
 
        // Creating a calendar using getInstance method
        Calendar now = Calendar.getInstance();
 
        // Get the current hour and minute as parameters
        System.out.println(now.get(Calendar.HOUR_OF_DAY)
                           + ":"
                           + now.get(Calendar.MINUTE));
        // Printing the current hour and minute using now
    }
}


 
 

Output

9:4

 

 



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads