JAVA常用类 Math String StringBuffer StringBuilder Date Calender

Math类

math类都是static形式所以可以用Math.来调用
math类提供了常用的数学常量 PI、E等可以用Math.PI、Math.E来调用

三角函数

public static double sin(double a) 返回a的正弦值
public static double cos(double a) 返回a的余弦值
public static double tan(double a) 返回a的正切值
public static double asin(double a) 返回a的反正弦值
public static double acos(double a) 返回a的反余弦值
public static double atan(double a) 返回a的反正切值
public static double toRadians(double angdeg) 将角度转换为弧度
public static double toDegrees(double angrad) 将弧度转化为角度
*通常角度的转换是不精确的


public static void main(String[] args) {
		System.out.println("60度的正弦值"+Math.sin(Math.PI/3));
		System.out.println("180度的余弦值"+Math.cos(Math.PI));
		System.out.println("90度的正切值"+Math.tan(Math.PI/2));
		System.out.println("0的反正弦值"+Math.asin(0));
		System.out.println("1的反余弦值"+Math.acos(1));
		System.out.println("1的反正切值"+Math.atan(1));
		System.out.println("45度的弧度值"+Math.toRadians(45));
		System.out.println("PI/2的角度值"+Math.toDegrees(Math.PI/2));
		
	}

运行结果:
60度的正弦值0.8660254037844386
180度的余弦值-1.0
90度的正切值1.633123935319537E16
0的反正弦值0.0
1的反余弦值0.0
1的反正切值0.7853981633974483
45度的弧度值0.7853981633974483
PI/2的角度值90.0

指数函数

public static double exp(double a) 返回e的a次方
public static double log(double a) 返回a的自然对数,即lna的值
public static double log10(double a) 返回底数为10的a的对数
public static double sqrt(double a) 返回a的平方根
public static double pow(double a, double b) 返回a的b次方


	public static void main(String[] args) {
		System.out.println("e的3次方:"+Math.exp(3));
		System.out.println("3的自然对数:"+Math.log(3));
		System.out.println("以10为底3的对数:"+Math.log10(3));
		System.out.println("9的平方根值:"+Math.sqrt(9));
		System.out.println("2的3次方值:"+Math.pow(2, 3));
	}

运行结果:
e的3次方:20.085536923187668
3的自然对数:1.0986122886681098
以10为底3的对数:0.47712125471966244
9的平方根值:3.0
2的3次方值:8.0

取整函数

public static double ceil(double a) 返回>=a的最小浮点数 x.0
public static double floor(double a) 返回<=a的最大浮点数 x.0
public static double rint(double a) 返回对a四舍五入的浮点数x.0
public static int round(float a) 返回对a四舍五入后最近的整数
public static long round(double a)返回对a四舍五入的最近的长整数


	public static void main(String[] args) {
		System.out.println("Math.ceil(8.3):"+Math.ceil(8.3));
		System.out.println("Math.floor(3.5):"+Math.floor(3.5));
		System.out.println("Math.rint(3.7):"+Math.rint(3.7));
		System.out.println("Math.round(2.5):"+Math.round(2.5));
		System.out.println("Math.round(4.3f):"+Math.round(4.3f));
	}

运行结果:
Math.ceil(8.3):9.0
Math.floor(3.5):3.0
Math.rint(3.7):4.0
Math.round(2.5):3
Math.round(4.3f):4

取最大、最小、绝对值

public static int max(int a, int b) 返回两个int值中较大的一个
public static int min(int a, int b) 返回两个int值中较小的一个
public static int abs(int a) 返回int的绝对值
public static long abs(long a) 返回long值的绝对值
public static float abs(float a) 返回float值的绝对值
public static double abs(double a) 返回double值的绝对值


	public static void main(String[] args) {
		System.out.println("4.8和2.4的较大者:"+Math.max(4.8, 2.4));
		System.out.println("5.3和2.9的较小者:"+Math.min(5.3, 2.9));
		System.out.println("-4的绝对值:"+Math.abs(-4));
	}

运行结果:
4.8和2.4的较大者:4.8
5.3和2.9的较小者:2.9
-4的绝对值:4

String类

String 类代表字符串
字符串是常量
String 对象是不可变的
++++++++++++++++++++++++
构造方法:
String(byte[] bytes) 通过byte数组构造字符串对象
String(char[] value) 通过char数组构造字符串对象
String(String original)
构造一个original的副本,即拷贝一个original
String(StringBuffer buffer)
通过StringBuffer数组构造字符串对象


字符串与字符数组之间的转换
	public static void main(String[] args) {
		String str1 = "hello";			//定义字符串
		char c[] = str1.toCharArray();	//将一个字符串变为字符数组
		for(int i = 0; i < c.length; i++) {
			System.out.println(c[i]+"、");//循环输出
		}
		String str2 = new String(c);	//将全部的字符数组变为String
		String str3 = new String(c,0,3);//将部分字符数组变为String
		System.out.println(str2);		//输出字符串
		System.out.println(str3);		//输出字符串
	}

输出结果:
h、
e、
l、
l、
o、
hello
hel

字符串与字节数组之间的转换
	public static void main(String[] args) {
		String str1 = "hello";			//定义字符串
		byte b[] = str1.getBytes();		//将字符串变为byte数组
		System.out.println(new String(b));//将全部的byte数组变为字符串
		System.out.println(new String(b,1,3));//将部分的byte数组变为字符串
	}

运行结果:
hello
ell

字符串与整型数组之间的转换
	public static void main(String[] args) {
		String s1 = "123456789";
		int n1[] = new int[s1.length()];
		for(int i = 0; i< n1.length; i++) {
			n1[i] = Integer.parseInt(String.valueOf(s1.charAt(i)));
		}
		//整形数组转为字符串
		int n2[] = {1, 2, 3};
		String s2 = "";
		for(int i = 0; i< n2.length; i++) {
			s2 += Integer.toString(n2[i]);
		}
		System.out.println(s2);
	}

运行结果:
123

将字符串与指定对象比较
	public static void main(String[] args) {
		String s1 = "abcd";
		String s2 = "Abcd";
		System.out.println("s1是否等于s2"+s1.equals(s2));	//false
		System.out.println("s1是否等于s2"+s1.equalsIgnoreCase(s2)); //true  忽略大小写
	}

运行结果:
s1是否等于s2false
s1是否等于s2true

StringBuffer类

String类不可更改但StringBuffer类可以进行动态增减
所以StringBuffer类又叫动态字符串
每个字符串缓冲区都有一定的容量
构造方法:
StringBuffer() 构造一个初始容量16个字符的缓冲区
StringBuffer(int iniCapacity) 构造一个指定容量的缓冲区
StringBuffer(String str) 将其内容初始化为指定字符串内容

常用方法

public int length() 返回字符串个数
public append(object obj) 在尾部添加对象
public insert(int StartPos,object obj) 在 StartPos位置插入对象obj
public StringBuffer delete(int start,int end)
删除start 到end-1的字符串并返回当前对象的引用
public StringBuffer deleteCharAt(int index)
删除索引为index的字符串并返回当前对象的引用

StringBuilder类

StringBuilder也是一个可变字符序列
StringBuilder和StringBuffer的区别是StringBuilder不是线程安全的在不需要多线程安全的情况下替换StringBuffer
构造方法:
StringBuilder() 为空默认为16个字节
StringBuilder(CharSequece seq)
使用seq初始化,在此基础上加16个字节
StringBuilder(int capacity)设置特定容量
StringBuilder(String str)使用str初始化在str大小的基础上加16

Date类

Date类表示日期和时间
构造方法:
Date(); 用当前时间初始化实例
Date(long date); 分配Date对象并初始化此对象

常用方法:
public void setTime(long time);
设置此Date对象表示1970/1/1 00:00:00 GMT 以后time毫秒的时间点
public long getTime();
返回1970/1/1 00:00:00 GMT以来此Date对象表示的毫秒数
public static Date valueOf(String s);
转换字符串格式的日期为Date

	public static void main(String[] args) {
		SimpleDateFormat sf = new SimpleDateFormat("yyy-MM-dd HH:mm:ss");
		Date d = new Date();
		String DateString = sf.format(d);	//返回类似的日期格式
		System.out.println(DateString);
	}

运行结果:
2019-09-24 19:35:47

Calender类

Calender类提供了日历功能
Calender类是一个抽象类
可以使用static方法getlnstance()初始化一个日历对象

打印未来几年的黑色星期五
	public static void main(String[] args) {
		Calendar cal = Calendar.getInstance();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd EEEE");//日期格式
		cal.set(Calendar.DAY_OF_MONTH, 13);//设置日期为13
		int n = 1;
		while(n <= 10) {
			if(cal.get(Calendar.DAY_OF_WEEK) == Calendar.FRIDAY) {	//如果是星期五
				Date date = cal.getTime();
				System.out.println(sdf.format(date));
				n++;
			}
			cal.add(Calendar.MONTH, 1);
		}
	}

运行结果:
2019/09/13 星期五
2019/12/13 星期五
2020/03/13 星期五
2020/11/13 星期五
2021/08/13 星期五
2022/05/13 星期五
2023/01/13 星期五
2023/10/13 星期五
2024/09/13 星期五
2024/12/13 星期五

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值