Math数学类
Math 类包含用于执行基本数学运算的方法,如初等指数、对数、平方根和三角函数。
数学方法类,所有方法均为静态方法
public static int abs(int a)
public static double ceil(double a)
public static double floor(double a)
public static int max/min(int a,int b)
public static double pow(double a,double b)
public static double random()
生成n-m,包含n但不包含m的数:
第一步算出 m-n的值,假设等于w
第二步Math.random()*w
第三步Math.random()*w+n
public static int round(float a)
// Math 类包含用于执行基本数学运算的方法,
// 如初等指数、对数、平方根和三角函数等。
// 数学方法类,所有方法均为静态方法
//求绝对值方法
System.out.println(Math.abs(-546));
//向上取整
System.out.println(Math.ceil(5.0));
//向下取整
System.out.println(Math.floor(5.5));
//最大值最小值
System.out.println(Math.max(5.5, 6.6));
System.out.println(Math.min(5.5, 6.6));
//求次幂
System.out.println(Math.pow(6,2));
//随机数 返回0~1之间小数不包含1
System.out.println((int)(Math.random()*11));
//四舍五入
System.out.println(Math.round(3.5));
System.out.println(Math.rint(3.8));
Random随机数
伪随机数生成类
伪随机原理:通过不同的种子完成随机数的生成
构造方法:
public Random() 种子为任意一个不重复的数
该构造方法使用一个和当前系统时间对应的相对时间有关的数字作为种子数,然后使用这个种子数构造Random对象
public Random(long seed) 指定种子
种子数只是随机算法的起源数字,和生成的随机数字的区间无关
主要方法
public int nextInt()及其重载方法
该方法的作用是生成一个随机的int值
public int nextInt(int n) 【0,n)
// 伪随机数生成类
// 伪随机原理:通过不同的种子完成随机数的生成
// 种子类似于随机数生成的规则
// 如果在构造方法每天填入不使用规则会随机生成
// 有规则则按照规则生成随机数
//不使用种子规则
Random r =new Random();
//生成负无穷到正无穷之间的任意整数
System.out.println(r.nextInt());
//生成0~指定数据之间的整数 (前包含后不包含)
System.out.println( r.nextInt(5));
//使用种子规则
//不论运行多少次生成规则不变 生成数据不变
//使用固定种子 会获得固定随机数数据
//相当于每个固定种子对应相应范围的一个固定数据
Random rr =new Random(5465464);//5465464 1145173163
//生成负无穷到正无穷之间的任意整数
System.out.println(rr.nextInt());
//生成0~指定数据之间的整数 (前包含后不包含)
System.out.println( rr.nextInt(5));
System类为系统相关类,不能被实例化。
// 系统类
// 使虚拟机停止运行 随意状态码
// System.exit(0);
// 当前运行时间与1900年1月1号0时0分0秒所差的毫秒数。
long start = System.currentTimeMillis();
String str = "";
int n = 100000;
for (int i = 0; i <= n; i++) {
str += i;
}
long end = System.currentTimeMillis();
System.out.println("运行时间:" + (end - start));
long start1 = System.currentTimeMillis();
StringBuffer sbBuffer = new StringBuffer();
for (int i = 0; i <= n; i++) {
sbBuffer.append(i);
}
long end1 = System.currentTimeMillis();
System.out.println("运行时间:" + (end1 - start1));
Date日期类
表示特定的时刻
时刻与时间的区别
时刻表示一个时间点
时间通常表示一段时间
构造方法
public Date()
public Date(long date)
成员方法
public long getTime()
public void setTime(long time)
//date日期类用来表示日期
//无参构造方法
//以当前日期数据创建日期对象
Date d1=new Date();
//有参构造方法
//以指定数据创建日期对象
//与1900年1月1号0时0分0秒所差的毫秒数。
Date d2=new Date(145454);
//日期类的方法大部分都已经被废弃
//但是目前还可以使用(不建议使用)
System.out.println(d1.getYear()+1900);//从1900开始
System.out.println(d1.getMonth()+1);//0~11
System.out.println(d1.getDate());
System.out.println(d1.getDay());
System.out.println(d1.getHours());
System.out.println(d1.getMinutes());
System.out.println(d1.getSeconds());
System.out.println(d1.getTime());//获取1900年1月1日0点0时0分0秒至指定日期的毫秒数
//date提供方法基本已不使用
//date现在使用就是创建当前日期的date对象
//date输出日期的格式不满足日常需求
SimpleDateFormat简单的日期格式化类
用于将date类型与String类型按照一定的格式相互转换
构造方法
public SimpleDateFormat(String pattern)
主要方法
public final String format(Date date)
public Date parse(String source)
//按照指定格式创建格式化类
SimpleDateFormat sdf=new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
Date d=new Date();
//format方法 将date类型按照指定格式格式化为字符串
String format = sdf.format(d);
System.out.println(format);//2019-12-04 11:26:16
//parse方法 将与指定格式相同的字符串转换为date类型
//格式不正确会保存
Date parse = sdf.parse(format);
System.out.println(parse);
Calendar日历类
以日历的形式表示日期
主要方法
public static Calendar getInstance()
public int get(int field)
public void add(int field,int amount)
public final void set(int year,int month,int date)
public final Date getTime()
// 日历信息类
// 不能通过new的形式创建对象
// 而是通过本身提供的静态方法获取对象
// getInstance方法 以当前日期为模板创建日历对象
Calendar c = Calendar.getInstance();
// getTime 返回代码当前日期的date对象
Date time = c.getTime();
// get方法
// 日历类使用通用get方法来获取相应信息
// 在参数中传入相应参数获取不同数据
// 参数为Calendar指定的静态常量
// Calendar.YEAR代表当前年份
System.out.println(c.get(Calendar.YEAR));// 当前年
// Calendar.MONTH代表当前年份
System.out.println(c.get(Calendar.MONTH));// 0~11
// Calendar.DAY_OF_MONTH 当前日期在本月第几天
System.out.println(c.get(Calendar.DAY_OF_MONTH)); // 日期
// Calendar.DAY_OF_WEEK 当前日期在本周第几天
System.out.println(c.get(Calendar.DAY_OF_WEEK));// 从周日开始
// DAY_OF_WEEK_IN_MONTH 当前日期在本月第几周
System.out.println(c.get(Calendar.DAY_OF_WEEK_IN_MONTH));// 第几周
// DAY_OF_YEAR 当前日期在本年第几天
System.out.println(c.get(Calendar.DAY_OF_YEAR));// 本年过了多少天
System.out.println(c.get(Calendar.HOUR));// 12小时制
System.out.println(c.get(Calendar.HOUR_OF_DAY));// 24小时制
System.out.println(c.get(Calendar.MINUTE));//分
System.out.println(c.get(Calendar.SECOND));//秒
System.out.println(c.get(Calendar.MILLISECOND));//毫秒
//set方法与其重载方法用于对日期的设置而出现的方法
//1、只为单一字段设置值
c.set(Calendar.YEAR, 2018);
//2、同时设置年月日 不需要指定属性
c.set(2019,1,1);
//3、设置年月日时分
c.set(2019,2,2,22,22);
//4、设置年月日时分秒
c.set(2019,3,2,23,33);
System.out.println(c.get(Calendar.YEAR));// 当前年
System.out.println(c.get(Calendar.MONTH));// 0~11
System.out.println(c.get(Calendar.DAY_OF_MONTH)); // 日期
//add方法 为某些自动设置延时 为某些字段数据加值(可以为负)
c.add(Calendar.YEAR, -1);
System.out.println(c.get(Calendar.YEAR));// 当前年