Sunday, November 17, 2024
Google search engine
HomeLanguagesJavaDateFormat getTimeInstance() Method in Java with Examples

DateFormat getTimeInstance() Method in Java with Examples

DateFormat class of java.text package is an abstract class that is used to format and parse dates for any locale. It allows us to format date to text and parse text to date. DateFormat class provides many functionalities to obtain, format, parse default date/time.

Note: DateFormat class extends Format class that means it is a subclass of Format class. Since DateFormat class is an abstract class, therefore, it can be used for date/time formatting subclasses, which format and parses dates or times in a language-independent manner.  

Package-view:

java.text Package
    DateFormat Class
        getTimeInstance() Method

getTimeInstance() Method of DateFormat class in Java is used to get the time format. This time formatter comes with a default formatting style for a default locale.

Syntax: 

public static final DateFormat getTimeInstance()

Parameter: The method does not take any parameters.

Return Type: The method returns a time formatted in a particular format.

Example 1:

Java




// Java Program to Illustrate getTimeInstance() Method
// of DateFormat Class
 
// Importing required classes
import java.text.*;
import java.util.*;
 
// Main class
public class GFG {
 
    // Main driver method
    public static void main(String[] argv)
    {
 
        // Initializing the first formatter by
        // creating object of DateFormat class
        DateFormat DFormat = DateFormat.getTimeInstance();
 
        System.out.println("Object: " + DFormat);
 
        // Formatting the string using format() method
        String str = DFormat.format(new Date());
 
        // Displaying the formatted string time on console
        System.out.println(str);
    }
}


Output

Object: java.text.SimpleDateFormat@8400729
6:39:49 AM

Example 2:

Java




// Java Program to Illustrate getTimeInstance() Method
// of DateFormat Class
 
// Importing required classes
import java.text.*;
import java.util.*;
 
// Main class
public class GFG {
 
    // Main driver method
    public static void main(String[] args)
        throws InterruptedException
    {
 
        // Initializing simple date format mm/dd/yyyy
        // by creating object of SimpleDateFormat class
        SimpleDateFormat SDFormat
            = new SimpleDateFormat("MM/dd/yyyy");
 
        // Displaying the formats
        Date date = new Date();
        String str_Date1 = SDFormat.format(date);
 
        // Displaying original date
        System.out.println("The Original: " + (str_Date1));
 
        // Initializing the Time formatter
        DateFormat DFormat = DateFormat.getTimeInstance();
 
        System.out.println("Object: " + DFormat);
 
        // Formatting the string using format() method
        String str = DFormat.format(new Date());
 
        // Displaying the formatted string time on console
        System.out.println(str);
    }
}


Output

The Original: 01/11/2022
Object: java.text.SimpleDateFormat@8400729
6:45:24 AM
Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments