[Java] Java Gold Measures: Format

2 minute read

Introduction

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

java.text.NumberFormat

// Output result: 1,000
NumberFormat format = NumberFormat.getInstance();
System.out.println(format.format(1000));

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

// Output result: 1000
try {
NumberFormat format = NumberFormat.getInstance(Locale.JAPAN);
String numberStr = "1000";
Number value1 = format.parse(numberStr);
System.out.println(value1);
} 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("#,#####");
System.out.println(formatter.format(111111));

// Output: 01,1111
DecimalFormat formatter = new DecimalFormat("00,0000");
System.out.println(formatter.format(11111));

// Output: ¥11,11,11
DecimalFormat formatter = new DecimalFormat("\u00a5##,##");
System.out.println(formatter.format(111111));

// Remarks (filled with 0)
// DecimalFormat type
// Output: 0100.00
DecimalFormat formatter = new DecimalFormat("####.##");
formatter.setMinimumFractionDigits(2);
formatter.setMaximumFractionDigits(4);
formatter.setMinimumIntegerDigits(4);
formatter.setMaximumIntegerDigits(6);
System.out.println(formatter.format(100));

List of main classes used in date format

java.time.format.DateTimeFormatter

■ 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
G AD
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);
System.out.println(formatter1.format(date));
System.out.println(formatter1.format(dateTime));

// 10:20:30
// UnsupportedTemporalTypeException when formatting date
DateTimeFormatter formatter2 = DateTimeFormatter.ofLocalizedTime(FormatStyle.MEDIUM);
System.out.println(formatter2.format(time));

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