Commonly used data conversions in Java programming: String and int conversion, Date and String conversion, BigDecimal and int comparison

1. Conversion between String and Int

When you want to convert the String type into the int type in Java, you need to use the parseInt() method or valueOf() method in the Integer class for conversion.

String str = "555555555";
int a = Integer.parseInt(str);  // 方式1
int b = Integer.valueOf(str).intValue()   // 方式2

You need to pay attention during the conversion process: because non-digits may appear in the string, exceptions need to be caught and handled during conversion.

1. Integer.parseInt(String) method: parseInt() is a method in the Integer wrapper class, which can parse strings into signed integers.

Note:
(1) When using the parseInt() method, all characters in the string must be numbers, but the first character can be the minus sign "-". Example: String
str="-1234"; can be converted to -1234
(2) When not all characters in the String type are numbers, an exception will also be thrown: NumberFormatException.

2. Integer.valueOf(String) method: valueOf() is also a method of the Integer wrapper class, which can convert a String type value into an int type value. This is similar to the parseInt() method, and their converted output results are the same.
3. But there are still differences between Integer.valueOf() and Integer.parseInt():

The valueOf(String) method returns an object of Integer class, while the parseInt(String) method returns the original int value.

4. Convert int to string

String s = String.valueOf(i); 
String s = Integer.toString(i); 
String s= i + '';

Two, Date and String conversion

1. Convert Date to String

SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String time = f.format(examApply.getCreateTime())

2. Convert String to Date

参数String类型的格式必须与注释中的日期格式对齐,否则会报错
DateFormat format = new SimpleDateFormat("yyyy-MM-dd"); //日期格式
Date date = format.parse(time);

3. Use the database class to convert String to Date.
  The format does not change after conversion, and the String type is Date type. The source code of this class only supports - as the delimiter, so you must use - as the delimiter. After determining - as the delimiter, this method will be relatively simple.

Date date =java.sql.Date.valueOf("2021-06-16");

3. Comparison of BigDecimal and int (error operator > cannot be applied to java.math.BigDecimal,int)

1. Here is an error: operator > cannot be applied to java.math.BigDecimal, int. BigDecimal cannot be compared with >, <, = and so on. If used, this error will be reported.

2. BigDecimal must be compared using the compareTo() method, which returns negative numbers, positive numbers and 0 respectively according to the size of the two values, indicating less than, greater than and equal to respectively.

3. And you also need to use new BigDecimal(0) to convert to BigDecimal before comparison can be performed.

if(examRoom.getPrice().compareTo(new BigDecimal(0)) == 0){
    
    
  user.setStatus(ExamApplyStatus.PAYED);
  message = "----------";
}

Insert image description here

Guess you like

Origin blog.csdn.net/weixin_54514751/article/details/132083572