Common system classes in Java

Posted by nathanblogs on Tue, 04 Jan 2022 08:27:41 +0100

1. Date

java. The util package provides a Date class to encapsulate the current Date and time.

1.1 use of date:

Get time of day:

public class date {
	public static void main(String[] args) {
		Date date = new Date();
		System.out.println(date.toString());
	}
}

Operation results:

Fri Jul 30 09:45:03 CST 2021

1.2 Calendar

Calendar class is an abstract class. It implements the objects of specific subclasses in actual use. The process of creating objects is transparent to programmers. You only need to use getInstance method to create objects.

Setting of Calendar class object information

Setting: set
give an example:

public static void main(String[] args) {
		Calendar cal = Calendar.getInstance();
		cal.set(Calendar.DATE,17);                 //Set the date to the 17th
		System.out.println(cal.get(Calendar.DATE));//Print date
	}

Operation results:

17

Add settings:

public static void main(String[] args) {
		Calendar cal = Calendar.getInstance();
		System.out.println(cal.get(Calendar.DATE) +"||"+ (cal.get(Calendar.MONTH)+1));//
		cal.add(Calendar.DATE, 5);
		System.out.println(cal.get(Calendar.DATE));
	}

Dateformat class 1.3

DateFormat is an abstract class of date / time formatting subclass, which formats and analyzes date or time in a language independent manner. Date / time formatting subclasses (such as SimpleDateFormat) allow formatting (date - > text), analysis (text - > date), and standardization. Represent the date as a date object, or as the number of milliseconds from 00:00:00 GMT, January 1, 1970.

When creating a DateFormat object, you cannot use the new keyword. Instead, you should use the static method getDateInstance() in the DateFormat class. The example code is as follows:

public static void main(String[] args) {
		Date date = new Date();
		DateFormat d = DateFormat.getDateInstance();//Format, accurate to day
		DateFormat t = DateFormat.getDateTimeInstance();//Format, accurate to minutes and seconds
		System.out.println(d.format(date));
		System.out.println(t.format(date));
	}

Operation results:

2021-7-30
2021-7-30 18:52:51

1.4 SimpleDateFormat

SimpleDateFormat is a concrete class that formats and parses dates in a locale related manner. It allows formatting (date → text), parsing (text → date), and normalization. SimpleDateFormat allows you to select a pattern for any user-defined date / time format.
give an example:

public static void main(String[] args) {
		Date date = new Date();
		SimpleDateFormat sdf =new SimpleDateFormat("yyyy/MM/dd HH:mm:ss" );
		System.out.println(sdf.format(date));
	}

Operation results:

2021/07/30 19:00:30

1.5 JDK8 new time API

LocalDate gets the current time:

public static void main(String[] args) {
		LocalDate date = LocalDate.now();
		System.out.println(date);//Get current time
		System.out.println(date.getYear());//Get the current year (the same method as getting month and day)
	}
2021-07-30
2021

LocalTime get current time:

public static void main(String[] args) {
		LocalTime date = LocalTime.now();
		System.out.println(date);
		System.out.println(date.getSecond());
	}

Operation results:

19:11:10.703
10

LocatDateTime gets the current time:

public static void main(String[] args) {
		LocalDateTime date = LocalDateTime.now();
		System.out.println(date);
		System.out.println(date.getYear());
		System.out.println(date.getSecond());
	}

Operation results:

2021-07-30T19:12:52.921
2021
52

DateTimeFormatter gets the current time:

public static void main(String[] args) {
		DateTimeFormatter d = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
		System.out.println(LocalDateTime.now().format(d));
		
	}

Operation results:

2021-07-30 19:26:28

Instant get current time:

public static void main(String[] args) {
		Instant date = Instant.now();
		System.out.println(date);
	}

Operation results:

2021-07-30T11:15:27.855Z

2. Common formatting

2.1 NumberFormat

NumberFormat class is a class for formatting numbers provided by Java. It can convert a string of numbers into the data format you want, or convert a string into a number.
The NumberFormat class contains two important methods, format() and parse(); The format() method is responsible for converting numbers into strings, and parse() is responsible for converting characters

  1. getInstance(),getNumberInstance(). Returns the general numeric format of the current default locale.
  2. getInstance(Locale),getNumberInstance(Locale). Returns the common numeric format for the specified locale.
  3. NumberFormat.setMinimumIntegerDigits(int). Sets the minimum number of digits allowed for the integer part of the number.
  4. NumberFormat.setMaximumIntegerDigits(int). Sets the maximum number of digits allowed for the integer part of the number.
  5. NumberFormat.setMinimumFractionDigits(int). Set the minimum number of decimal places. If the number of decimal places is less than 0, it will be output according to the actual number of decimal places.
  6. NumberFormat.setMaximumFractionDigits(int). Set the maximum number of decimal places to be reserved, and 0 will not be added if it is insufficient.

2.2 DecimalFormat

DecimalFormat is a concrete subclass of NumberFormat used to format decimal numbers.
DecimalFormat contains a pattern and a set of symbols:

'0': a number
'#': a number, excluding 0 and placeholder for decimal separator
",": placeholder for grouping separator
“;”: Delimitation format.

"-": default negative prefix.

"%": multiplied by 100 and displayed as a percentage

“?” : Multiplied by 1000 and displayed as a thousand dollar currency symbol; Replace with currency symbols; If double writing, use the international currency symbol instead
"X": any other character used in the prefix or suffix to refer to a special character in the prefix or suffix.

2.3 BigDecimal

It is used to accurately calculate the number of more than 16 significant bits.

Add (BigDecimal) adds the values in the BigDecimal object, and then returns the object.
Subtract (BigDecimal) subtracts the value in the BigDecimal object and returns the object. Multiply (BigDecimal) multiplies the values in the BigDecimal object and returns the object. Divide (BigDecimal) divides the values in the BigDecimal object and returns the object.

Topics: Java