Java Gold Countermeasures: Format


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

What is a format?

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

Format by number, date, etc. Because the method is different, the format is below Summarize the method.

List of main classes used in numeric 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 as follows

Method Contents
static final NumberFormat getInstance() Returns a numeric format object corresponding to the default locale
static final NumberFormat getInstance(Locale locale) Returns a numeric format object corresponding to the locale specified in the argument
static final getCurrencyInstance() Return currency format object corresponding to default locale
static final getCurrencyInstance() Returns the currency format object corresponding to the locale specified in the argument
Number parse(String string) throws ParseException Return the character string specified by the argument as a numerical value

** ▼ Usage example **


//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) {
	//A value different from the specified locale (numberStr= "$1000") Is specified

■ java.text.DecimalFormat

DecimalFormat, unlike NumberFormat, gets an object with new. In addition, it can be freely formatted by using the following patterns.

symbol Contents
0 Number (if there is no number in that digit)"0"Show)
Number (blank if there is no number in that digit)
. Number digit delimiter
- Minus sign
, Comma separated
% Sell 100 and represent a percentage
¥u00a5 Currency symbol

** ▼ Usage example **

//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 mainly used methods

Method Contents
static DateTimeFormatter ofLocalizedDate(FormatStyle dateStyle) Date format
static DateTimeFormatter ofLocalizedTime(FormatStyle timeStyle) Time format
static DateTimeFormatter ofLocalizedDate(FormatStyle dateStyle) Date and time format
static DateTimeFormatter ofPattern(String pattern) Create a formatter by specifying an arbitrary pattern character string as an argument (pattern is described below)

Pattern string list

pattern Contents
G Year
y Year
M Month
d Moon day
E Day of the week
a a.m./afternoon
h Time (12 o'clock notation)
H Time (24 o'clock notation)
m Minutes
s Seconds
SS millisecond
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");

Recommended Posts

Java Gold Countermeasures: Format
Java Gold Countermeasures: Localization
google java format
java error countermeasures
Format XML in Java
Countermeasures for Java OutOfMemoryError
Java8 Gold exam memorandum
I started Java Gold (Chapter 1-1)
On passing Java Gold SE 8
I took Java SE8 Gold.
Oracle Certified Java Programmer, Gold SE 8
What I learned with Java Gold
Try using JSON format API in Java
Output Date in Java in ISO 8601 extended format
java Calendar class (time set, comparison, format)