Java Gold Countermeasures: Format

Introduction

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 **

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) {
	//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("#,#####");
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 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);
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));

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
Java
What I learned with Java Gold
Java
Try using JSON format API in Java
Output Date in Java in ISO 8601 extended format
java Calendar class (time set, comparison, format)