[Java] Java Gold Measures: Format

2 minute read


This article summarizes the format as a summary of Java Gold learning.

What is ### format?

Data handled in Java is displayed according to the display location. For formatting if you want to change the format.

Format with numbers, dates, etc. Since the method is different, Summarize the method.

List of main classes used in number format

java.text.NumberFormat java.text.DecimalFormat

■ java.text.NumberFormat

NumberFormat cannot be new, so Get the object by static method.

The main methods of NumberFormat are

Method Content
static final NumberFormat getInstance() Returns a number format object corresponding to the default locale
static final NumberFormat getInstance(Locale locale) Returns a number format object corresponding to the locale specified in the argument
static final getCurrencyInstance() Returns a currency format object corresponding to the default locale
static final getCurrencyInstance() Returns a currency format object corresponding to the locale specified in the argument
Number parse(String string) throws ParseException Returns the string specified in the argument as a number

▼ Example of use


// Output result: 1,000
NumberFormat format = NumberFormat.getInstance();

// Output result: ¥1,000
NumberFormat format = NumberFormat.getCurrencyInstance();

// Output result: 1000
try {
NumberFormat format = NumberFormat.getInstance(Locale.JAPAN);
String numberStr = "1000";
Number value1 = format.parse(numberStr);
} catch (ParseException e) {
// When a value different from the specified locale (numberStr = "$1000") is specified

■ java.text.DecimalFormat

DecimalFormat, unlike NumberFormat, gets an object with new. In addition, the following patterns can be used to format freely.

Symbol Content
0 Numerical value (“0” is displayed if there is no numerical value in that digit)
Numerical value (blank if there is no numerical value)
. Digit grouping character
- Minus sign
, Comma separated
% 100 Sell and represent percentage
¥u00a5 Currency symbol

▼ Example of use

// Output: 1,11111
DecimalFormat formatter = new DecimalFormat("#,#####");

// Output: 01,1111
DecimalFormat formatter = new DecimalFormat("00,0000");

// Output: ¥11,11,11
DecimalFormat formatter = new DecimalFormat("\u00a5##,##");

// Remarks (filled with 0)
// DecimalFormat type
// Output: 0100.00
DecimalFormat formatter = new DecimalFormat("####.##");

List of main classes used in date format


■ DateTimeFormatter

List of methods mainly used

Method Content
static DateTimeFormatter of LocalizedDate(FormatStyle dateStyle) Date Format
static DateTimeFormatter of LocalizedTime(FormatStyle timeStyle) Time format
static DateTimeFormatter of LocalizedDate(FormatStyle dateStyle) Date and Time Formats
static DateTimeFormatter ofPattern(String pattern) Create a formatter by specifying an arbitrary pattern character string as an argument (pattern is described below)

List of pattern strings

Pattern Content
y year
M Month
d day of month
E day of the week
a am/pm
h time (12 o’clock notation)
H Hour (24 hour notation)
m minutes
s seconds
SS milliseconds
x Time Zone
LocalDate date = LocalDate.of(2020, Month.JULY, 6);
LocalTime time = LocalTime.of(10, 20, 30);
LocalDateTime dateTime = LocalDateTime.of(date, time);

// Output result below
// 2020/07/06
// 2020/07/06
DateTimeFormatter formatter1 = DateTimeFormatter.ofLocalizedDate(FormatStyle.MEDIUM);

// 10:20:30
// UnsupportedTemporalTypeException when formatting date
DateTimeFormatter formatter2 = DateTimeFormatter.ofLocalizedTime(FormatStyle.MEDIUM);

// 10:20
DateTimeFormatter formatter3 = DateTimeFormatter.ofPattern("hh:mm");
  • See here for FormatStyle