Thursday, October 23, 2025
HomeLanguagesJavaJava DateFormat getNumberFormat() Method with Examples

Java DateFormat getNumberFormat() Method with Examples

The getNumberFormat() method of DateFormat class will return an instance of NumberFormat for this DateFormat instance.

Syntax:

public static final NumberFormat getNumberFormat()

Parameter: This method won’t require any parameter.

Return value: This method will return the number formatter which this date/time formatter uses.

The examples given below will illustrate the getNumberFormat() method.

Example 1:

Java




// Java program to illustrate
// getNumberFormat() method
  
// importing the required packages
import java.text.DateFormat;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
  
class Testclass {
    public static void main(String[] args)
    {
  
        // initializing the DateFormat
        DateFormat df = DateFormat.getDateInstance();
  
        // extracting the year using SimpleDateFormat
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
  
        // initializing the NumberFormat
        NumberFormat nf = df.getNumberFormat();
  
        // printing the NumberFormat return value as object
        System.out.println("NumberFormat Object : " + nf);
  
        // formatting the current date into a string
        String str = df.format(new Date());
  
        // printing the current date
        System.out.println("Current date : " + str);
  
        // formatting the current year into a string
        String st = sdf.format(new Date());
  
        // converting the string to integer
        int i = Integer.parseInt(st);
  
        // NumberFormat.format() method
        // accepts only integer and double
        // variables as arguments
  
        // formatting the return value into a string
        String s = nf.format(i);
  
        // printing the formatted value
        System.out.println("Year : " + s);
    }
}


Output

NumberFormat Object : java.text.DecimalFormat@674dc
Current date : Dec 15, 2021
Year : 2021

Example 2 : 

Java




// Java program to illustrate
// getNumberFormat() method
  
// importing the required packages
import java.text.DateFormat;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
  
class Testclass {
    public static void main(String[] args)
    {
  
        // initializing the DateFormat
        DateFormat df = DateFormat.getTimeInstance();
  
        // extracting the minutes using SimpleDateFormat
        SimpleDateFormat sdf = new SimpleDateFormat("mm");
  
        // initializing the NumberFormat
        NumberFormat nf = df.getNumberFormat();
  
        // printing the NumberFormat return value as object
        System.out.println("NumberFormat Object : " + nf);
  
        // formatting the current time into a string
        String str = df.format(new Date());
  
        // printing the current time
        System.out.println("Current time : " + str);
  
        // formatting the current minutes into a string
        String st = sdf.format(new Date());
  
        // converting the string to integer
        int i = Integer.parseInt(st);
  
        // NumberFormat.format() method
        // accepts only integer and double
        // variables as arguments
  
        // formatting the return value into a string
        String s = nf.format(i);
  
        // printing the formatted value
        System.out.println("Year : " + s);
    }
}


Output

NumberFormat Object : java.text.DecimalFormat@674dc
Current time : 8:35:10 AM
Year : 35
Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS