当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java DateFormat getNumberFormat()用法及代码示例


DateFormat 类的 getNumberFormat() 方法将为此 DateFormat 实例返回一个 NumberFormat 实例。

用法:

public static final NumberFormat getNumberFormat()

参数:此方法不需要任何参数。

返回值:此方法将返回此日期/时间格式化程序使用的数字格式化程序。



下面给出的示例将说明 getNumberFormat() 方法。

范例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);
    }
}
输出
NumberFormat Object:java.text.DecimalFormat@674dc
Current date:Dec 15, 2021
Year:2021

范例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);
    }
}
输出
NumberFormat Object:java.text.DecimalFormat@674dc
Current time:8:35:10 AM
Year:35



相关用法


注:本文由纯净天空筛选整理自GeeksforGeeks大神的英文原创作品 Java DateFormat getNumberFormat() Method with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。