1、 DecimalFormat
public class TestDecimalFormat {
public static void main(String
[] args
) {
DecimalFormat df
=new DecimalFormat("##,###,###.##");
String format
= df
.format(1231231.2351231);
System
.out
.println(format
);
df
=new DecimalFormat("##.00000");
String format1
= df
.format(123.1);
System
.out
.println(format1
);
}
}
2、BigDecimal
(java.math.BigDecimal)主要处理财务相关的数据 他是一个引用数据类型所以不能直接+/-
public class TestBigDecimal {
public static void main(String
[] args
) {
BigDecimal a
=new BigDecimal("1231230000");
BigDecimal b
=new BigDecimal("123123");
BigDecimal add
= a
.add(b
);
System
.out
.println(add
);
BigDecimal subtract
= a
.subtract(b
);
System
.out
.println(subtract
);
BigDecimal multiply
= a
.multiply(b
);
System
.out
.println(multiply
);
BigDecimal divide
= a
.divide(b
);
System
.out
.println(divide
);
}
}
3、Random
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-pfQW3OBO-1593848759427)(upload/image-20200702163031785.png)]
public class TestRandom {
public static void main(String
[] args
) {
Random random
= new Random();
int i
= random
.nextInt(11);
System
.out
.println(i
);
}
}
4、Date
java.util.Date;java.text.SimpleDateFormat;
package com
.xd
.api
;
import java
.text
.ParseException
;
import java
.text
.SimpleDateFormat
;
import java
.util
.Date
;
public class TestDate {
public static void main(String
[] args
) throws ParseException
{
Date date
=new Date();
System
.out
.println("1、获取系统当前时间:"+date
);
SimpleDateFormat sdf
=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss (a)(EE)");
String format
= sdf
.format(date
);
System
.out
.println("2、格式化后的输出:"+format
);
String time
= "2020-10-10 08:08:07";
SimpleDateFormat sdf2
=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
Date parse
= sdf2
.parse(time
);
String format1
= sdf
.format(parse
);
System
.out
.println("3、字符串转时间后的格式"+format1
);
long l
= System
.currentTimeMillis();
System
.out
.println("4、获取当前时间的时间戳:"+new Date(l
));
l
=l
-1000*60*60*24;
System
.out
.println("5、获取昨天现在的时间:"+new Date(l
));
}
}
5、Calendar
package com
.xd
.api
;
import java
.util
.Calendar
;
import java
.util
.Date
;
public class TestCalender {
public static void main(String
[] args
) {
Calendar calendar
=Calendar
.getInstance();
calendar
.setTime(new Date());
String year
=String
.valueOf(calendar
.get(Calendar
.YEAR
));
String month
=String
.valueOf(calendar
.get(Calendar
.MONTH
)+1);
String day
=String
.valueOf(calendar
.get(Calendar
.DATE
));
String week
=String
.valueOf(calendar
.get(Calendar
.DAY_OF_WEEK
)-1);
System
.out
.println("1、今天是:"+year
+"-"+month
+"-"+day
+" 星期:"+week
);
int hour
=calendar
.get(Calendar
.HOUR
);
int minute
=calendar
.get(Calendar
.MINUTE
);
int second
=calendar
.get(Calendar
.SECOND
);
System
.out
.println("2、时间:"+hour
+":"+minute
+":"+second
);
calendar
.set(2020,5,5);
long timeInMillis
= calendar
.getTimeInMillis();
calendar
.set(2020,7,3);
long timeInMillis1
= calendar
.getTimeInMillis();
long days
=Math
.abs(timeInMillis1
-timeInMillis
)/(24*60*60*1000);
System
.out
.println("3、相隔:"+days
+"天");
}
}
6、String
https://www.runoob.com/java/java-regular-expressions.html 正则语法
分隔需要转义的字符:
| \ $ * + . ? ^ ( ) [ ] { }
package com
.xd
.api
;
import java
.util
.Arrays
;
public class TestString {
public static void main(String
[] args
) {
String str
=new String("I love Java");
char[] chars
= str
.toCharArray();
System
.out
.println("1、字符串转化为字符数组:"+ Arrays
.toString(chars
));
String s
= String
.valueOf(chars
);
System
.out
.println("2、数组转化为字符串:"+s
);
char j
= str
.charAt(str
.indexOf("J"));
System
.out
.println("3、获取J:"+j
);
System
.out
.println("4、最后一次的索引值:"+str
.lastIndexOf("a"));
System
.out
.println(str
.endsWith("ava"));
System
.out
.println(str
.startsWith("a"));
System
.out
.println("5、正则匹配"+str
.matches("(.*)ava"));
String replaceStr
=new String(str
);
replaceStr
.replace("L", "l");
System
.out
.println("6、替换前:"+str
);
System
.out
.println("7、替换后:"+replaceStr
);
String str2
="i love Java";
System
.out
.println(str
.equals(str2
));
System
.out
.println(str
.equalsIgnoreCase(str2
));
System
.out
.println("8、切割后的:"+str
.substring(5));
String s1
= str
.toLowerCase();
System
.out
.println("9、小写:"+s1
);
String s2
= str
.toUpperCase();
System
.out
.println("10、大写:"+s2
);
StringBuilder sb
=new StringBuilder(str
);
sb
.reverse();
System
.out
.println("11、字符串反转:"+sb
);
StringBuffer sb2
=new StringBuffer(str
);
System
.out
.println("12、字符串反转:"+sb2
.reverse());
System
.out
.println("13、分隔:当前字符串:"+str
);
String
[] s3
= str
.split(" ");
System
.out
.println("14、分隔后的字符串:"+Arrays
.toString(s3
));
String test
="1231231@qq.com";
String
[] split
= test
.split("\\.");
System
.out
.println("15、分隔后的字符串:"+Arrays
.toString(split
));
String str3
=" xd ";
System
.out
.println("16、开始长度:"+str3
.length());
System
.out
.println(str3
.trim()+"删除空格后的长度:"+str3
.trim().length());
}
}
7、StringBuffer
线程安全
8、StringBuilder
线程不安全
9、正则表达式
https://www.runoob.com/java/java-regular-expressions.html
11、Math
public class TestMath {
public static void main(String
[] args
) {
int a
=10;
int b
=2;
int c
=-2;
double sqrt
= Math
.sqrt(a
);
System
.out
.println("a的平方"+a
);
int max
= Math
.max(a
, b
);
int max1
= Math
.max(max
, c
);
System
.out
.println("最大值:"+max1
);
double pow
= Math
.pow(b
, a
);
System
.out
.println("2的10次方:"+pow
);
for (int i
= 0; i
< 20; i
++) {
System
.out
.println(Math
.random());
}
}
}
12、System
public class TestSystem {
public static void main(String
[] args
) {
long l
= System
.currentTimeMillis();
char[] a
=new char[]{'1','3','3','4'};
System
.arraycopy(a
,1,a
,3,1);
System
.out
.println(Arrays
.toString(a
));
}
}
13、包装类
14、Arrays
public class TestArrays {
public static void main(String
[] args
) {
int[] a
=new int[]{1,2,3,2,2,3,4,5,6,7,6,5,4};
Arrays
.sort(a
);
System
.out
.println(Arrays
.toString(a
));
int i
= Arrays
.binarySearch(a
, 7);
System
.out
.println("7的位置:"+i
);
}
}
15、int、Integer、String的相互转化
public class 相互转化
{
public static void main(String
[] args
) {
String strNum
="123";
int intNum
=123;
Integer integer
=123;
System
.out
.println(integer
.equals(intNum
));
int i
= Integer
.parseInt(strNum
);
Integer integer1
= Integer
.valueOf(strNum
);
String s
= String
.valueOf(intNum
);
String s1
= String
.valueOf(integer
);
int i1
= integer
.intValue();
}
}
16、StringTokenizer
public class TestStringTokenizer {
public static void main(String
[] args
) {
String ip
="192.168.1.0";
StringTokenizer st
=new StringTokenizer(ip
,"\\.");
int num
=st
.countTokens();
while(st
.hasMoreTokens()){
System
.out
.println(st
.nextToken());
}
}
}