java.text package

1. DateFormat abstract class

DateFormat is an abstract time formatting class. Formatting is a string that becomes a specified format.

DateFormat is an abstract class of date/time formatting. It formats and parses date or time in a language-independent manner.

  DateFormat can help format and parse dates in any locale. For months, weeks, and even calendar formats (lunar and solar calendars), the codes can be completely independent of the locale conventions.

1.1. SimpleDateFormat类

In the process of formatting and parsing date and time, its subclass SimpleDateFormat is more commonly used.

The SimpleDateFormat class is a concrete class for formatting and parsing dates in a language environment-related way.

It provides methods for formatting date and time (date time  string) and parsing date and time (string  date time).

1.2. SimpleDateFormat class date and time mode

Insert picture description here

1.3. SimpleDateFormat construction method

SimpleDateFormat()。

SimpleDateFormat(String pattern) is created according to the specified pattern.

SimpleDateFormat object.

1.4. SimpleDateFormat() common methods

String format(Date date); Format a java.util.Date object as a date according to the specified mode.

Date parse(String date); Parse a date string into a java.util.Date type object according to the specified pattern.

Insert picture description here

1.5. Program example

Convert the date into a string according to the specified pattern and print it

Insert picture description here

Convert string to date

Insert picture description here

2. NumberFormat class

NumberFormat is an abstract class for number formatting, which provides methods for formatting and parsing values.

2.1. NumberFormat common methods

String format (double d) format the number as a string

Number parse(String s) parses the string into numbers

void setMaximumFractionDigits(int newValue) Set the maximum decimal places

void setMinimumFractionDigits(int newValue) Set minimum decimal places

Under normal circumstances, its subclass DecimalFormat is used to format or parse numbers.

2.2. DecimalFormat子类

  DecimalFormat is a specific subclass of NumberFormat, used to format decimal numbers. This class is designed with various functions that enable it to parse and format numbers in any language environment, including support for Western, Arabic, and Hindi numbers.

2.3. DecimalFormat construction method

DecimalFormat()

DecimalFormat(String pattern) constructs the object according to the specified pattern

2.4. DecimalFormat number pattern character

Insert picture description here

2.5. Commonly used construction methods of DecimalFormat class

String format(double d); format numbers as strings

Number parse(String str); parse string numbers into Number objects

Insert picture description here

2.6. Program example

Convert the data into a string according to the specified mode and print it out

Insert picture description here

 1. By comparing the two data, we can know that the format method will start from the decimal point to the left according to the specified pattern.

Parse the string according to the specified pattern

Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_43088443/article/details/112791798