java基础知识点06_数字、字符串、日期

java基础知识点06_数字、字符串、日期


所有的基本类型,都有对应的类类型:

抽象类Number的子类:	Byte	Short	Integer		Long	Float	Double
Character	Boolean

装箱和拆箱

装箱:值类型转换为object类型
拆箱:object转化为值类型

int i = 5;
Integer it = new Integer(i);
int i2 = it.intValue();

自动装箱和自动拆箱

不调用构造方法,而是通过 = 完成 基本类型 和 类类型 转换叫自动装箱和自动拆箱

int i = 5;
Integer it = i;
int i2 = it;

数字转字符串

方法1int i = 5;
String str = String.valueOf(i);

方法2int i = 5;
Integer it = i;
String str = it.toString();

字符串转数字

String str = "999";
int i= Integer.parseInt(str);

java.lang.Math类

四舍五入运算,中间值0.5向右取整
System.out.println(Math.round(6.67));		7
System.out.println(Math.round(3.14));		3
System.out.println(Math.round(-1.5));		-1

随机浮点数 [0,1)
System.out.println(Math.random());
随机整数 [0,10)
System.out.println((int)( Math.random()*10));

开方和幂运算
System.out.println(Math.sqrt(9));
System.out.println(Math.pow(2,4));

圆周率pi:3.141592653589793
System.out.println(Math.PI);
自然常数e:2.718281828459045
System.out.println(Math.E);

Character常见方法

判断是否为字母、数字、空白、大写、小写
System.out.println(Character.isLetter('a'));
System.out.println(Character.isDigit('a'));
System.out.println(Character.isWhitespace(' '));
System.out.println(Character.isUpperCase('a'));
System.out.println(Character.isLowerCase('a'));

大小写转换
System.out.println(Character.toUpperCase('a')); 
System.out.println(Character.toLowerCase('A')); 

char 转 String
String str = Character.toString('a');

字符串方法

扫描二维码关注公众号,回复: 11712821 查看本文章
String a = "abcdefghijklmnopqrstuvwxyz";
String str = "abcd,efg,hijk,lmn,opqrst";
String str2 = "     abcdef    ";

计算长度
System.out.println(a.length());

获取字符
char c = a.charAt(3);

获取字符数组
char[] array = a.toCharArray();

截取字符串 d--z
String b = a.substring(3);
截取字符串 d--e	左闭右开
String c = sentence.substring(3,5);

分割字符串
String[] split = str.split(",");
for(String s : split) {
    
    
	System.out.println(s);
}

去除首尾空格
String trim = str2.trim();

大小写转换
System.out.println(a.toLowerCase());
System.out.println(a.toUpperCase());

出现位置
System.out.println(a.indexOf('g'));

替换第一个,
String str2 = str.replaceFirst(",", "。");
全部替换
String str2 = str.replaceAll(",", "。");

是否是同一个字符串对象
System.out.println(str1==str2);
字符串内容比较
System.out.println(str1.equals(str2));
System.out.println(str1.equalsIgnoreCase(str2));

是否以abc开始,是否以xyz结尾
System.out.println(str1.startsWith("abc"));
System.out.println(str1.endsWith("xyz"));

日期

日期时间原点:1970年1月1日 8点0分0秒

获取当前日期时间
Date now = new Date();
Thu Aug 27 22:26:59 CST 2020


获取日期原点
Date dstart = new Date(0);
Thu Jan 01 08:00:00 CST 1970

获取从日期原点开始100秒后的时间
Date d1 = new Date(100000);
Thu Jan 01 08:01:40 CST 1970

获取从日期原点到当前时间所经历的毫秒数
Date now = new Date();
System.out.println(now.getTime());
1598538768888

和上面一样,也是获取到当前时间经历的毫秒数
System.out.println(System.currentTimeMillis());

日期格式化 SimpleDateFormat

日期转字符串

SimpleDateFormat sdf =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS" );
Date d= new Date();
String str = sdf.format(d);
System.out.println(str);

2020-08-27 22:39:52 982

字符串转日期

SimpleDateFormat sdf =new SimpleDateFormat("yyyy/MM/dd HH:mm:ss" );
String str = "2020/8/27 23:00:00";
try {
    
    
	Date d = sdf.parse(str);
	System.out.println(d);
} catch (ParseException e) {
    
    
	e.printStackTrace();
}

Thu Aug 27 23:00:00 CST 2020

猜你喜欢

转载自blog.csdn.net/BLU_111/article/details/108145297