//了解Math的常用方法
//了解Math的常用方法 //1.public static int abs(int n) ,取绝对值 System.out.println(Math.abs(-1212)); System.out.println(Math.abs(13)); //2.ceil()。向上取整数 System.out.println(Math.ceil(4.00000001));//5.0 System.out.println(Math.ceil(4.0000000));//4.0 //3.floor()向下取整数 System.out.println(Math.floor(4.999999999));//4.0 System.out.println(Math.floor(4.00000000));//4.0 //5.round() 是舍五入(看小数点后一位哦) System.out.println(Math.round(3.4999999));//3 System.out.println(Math.round(3.50000001));//4 //6.max()取较大值 min()取较小值 System.out.println(Math.max(10, 123)); System.out.println(Math.min(2, 3333)); //7.pow()取次方 System.out.println(Math.pow(3, 2));//3的2次方 //random()取随机数(0.0-1.0)(包前不包后) System.out.println(Math.random());
//Runtime //代表程序的运行环境 //Runtime就是一个单例类 //1.getRuntime() 返回与当前java应用程序相关联的运行对象 Runtime s=Runtime.getRuntime(); // Runtime f=Runtime.getRuntime(); //2.exit()终止当前的运行虚拟机,非零状态表示异常停止 //s.exit(0); //3.availableProcessors()获取虚拟机能够使用的处理器数 System.out.println(s.availableProcessors()); //4。totalMemory()返回java虚拟机中的内存总量 System.out.println(s.totalMemory()/1024/1024+"MB"); //5.freeMemory() 返回java虚拟机的可用内存容量 System.out.println(s.freeMemory()/1024/1024+"MB"); //6.exct() 启动某个程序,并返回该程序的对象 Process r=s.exec("\"C:\\Program Files (x86)\\Tencent\\QQ\\Bin\\QQScLauncher.exe\""); Thread.sleep(500);//让程序暂停5s中 r.destroy();//关闭程序 }
//了解System的常见方法
//1.exit()终止当前的java的虚拟机
// System.exit(0);
System.out.println("--------------------");
//2.currentTimeMillis()返回当前的系统时间 通常看计算所用的时间
System.out.println(System.currentTimeMillis());
long time1=System.currentTimeMillis();
for (int i = 0; i <1000000 ; i++) {
System.out.println("输出了"+i);
}
long time2=System.currentTimeMillis();
System.out.println((time2-time1)/1000+"s");
//
//掌握bigeDecimal的使用:解决小数失真的问题
//1,把他们编成字符串封装成bigDecimal对象来运算 // BigDecimal a1=new BigDecimal(Double.toString(a)); // BigDecimal b1=new BigDecimal(Double.toString(b)); //推荐以下方式:把小数转换成字符串在得到bigdDecimal的对象来使用 BigDecimal a1 = BigDecimal.valueOf(a); BigDecimal b1= BigDecimal.valueOf(b); //加法 BigDecimal c1=a1.add(b1); System.out.println(c1); //减法 BigDecimal c2=a1.subtract(b1); System.out.println(c2); //乘法 BigDecimal c3=a1.multiply(b1); System.out.println(c3); //除法 BigDecimal c4=a1.divide(b1); System.out.println(c4); //面对1/3的时候,这时精度不够会报错, BigDecimal j=BigDecimal.valueOf(0.1); BigDecimal i=BigDecimal.valueOf(0.3); BigDecimal k=j.divide(i,2, RoundingMode.HALF_UP);//(对象,精确几位,四舍五入)
//掌握date的日期使用
//1.创建一个date的对象,代表系统当前的时间信息
Date d=new Date();
System.out.println(d);
//2.拿到时间的毫秒值
long time=d.getTime();
System.out.println(time);
//3.把时间毫秒值转化成日期对象,2秒之后的时间
time+=2*1000;
Date d2=new Date(time);
System.out.println(d2);
//4.把日期对象的时间通过setTime方法进行修改
Date s=new Date();
s.setTime(time);
System.out.println(s);
/掌握simpledeteformate(日期格式化)的使用:
//1.准备一些时间
Date d=new Date();
System.out.println(d);
long time=d.getTime();
System.out.println(time);
//2.格式化日期时间对象,和时间毫秒值
//创建简单的日期格式化对象,封装日期格式化对象 星期几 上下午
SimpleDateFormat dsf=new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss EEE a");
//将日期格式化成字符串
String g=dsf.format(d);
//将时间毫秒值格式化成字符串
String hh=dsf.format(time);
System.out.println(g);
System.out.println(hh);
System.out.println("-------------------");
//通过simpledateformate解析字符串成为日期对象
String detestr="2020-12-12 12-12-11";
//1.创建简单日期格式化对象,格式与字符串格式相同
SimpleDateFormat ds=new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");//格式要相同
//解析字符串
Date s=ds.parse(detestr);
//Calender日历的使用方法
//Calender是一个抽象类,只能用子类来调用
//注意:calender是可变对象,一旦修改后对象本身的时间将改变
//1.得到系统的此时对应的日历信息
Calendar now=Calendar.getInstance();
System.out.println(now);
//2.获取日历的某个信息
int year=now.get(Calendar.YEAR);
System.out.println(year);
int days=now.get(Calendar.DAY_OF_MONTH);
System.out.println(days);
//3.拿到日历中记录的日期对象
Date d=now.getTime();
System.out.println(d);
//4.拿到时间的毫秒值
long time= now.getTimeInMillis();
System.out.println(time);
//5.修改日历的信息
now.set(Calendar.MONTH,3);//修改成4月份
System.out.println(now);
//为某个信息增加或减少
now.add(Calendar.DAY_OF_MONTH,2);
now.add(Calendar.DAY_OF_MONTH,-1);
System.out.println(now);
//传统的时间类(date,dateformat,calender)存在如下问题 //1.设计不合理,使用不方便 Date d=new Date(); // System.out.println(d.getYear()+1900); Calendar s=Calendar.getInstance(); int year=s.get(Calendar.YEAR); System.out.println(year); //2.都是可变对象,修改后会丢失原来的数据对象 //3。线程不安全 //4.不能精确到纳秒,只能精确到毫秒
LocalDate
//本地的日期信息(不可变的对象)
LocalDate d=LocalDate.now();//年 月 日
System.out.println(d);
//1.获取日期对象中的信息
int year=d.getYear();//年
int month=d.getMonthValue();//月
int day=d.getDayOfMonth();//日
int dayofyear=d.getDayOfYear();//一年中的第几天
int dayofmonth=d.getDayOfMonth();//一个月的第几天
int dayofweek=d.getDayOfWeek().getValue();//星期几
System.out.println(year);
System.out.println(month);
System.out.println(day);
System.out.println(dayofmonth);
System.out.println(dayofyear);
System.out.println(dayofweek);
//2.修改某个信息:withyear,withmoth,withdayofmoth
LocalDate d2=d.withYear(2099).withMonth(2);//修改后返回的是一个新的日期对象,原来的不变
System.out.println(d2);
System.out.println(d);
//3.把某个信息加多少:pluseyear,plusemoths,
LocalDate d1=d.plusYears(2);
System.out.println(d1);
//4.把某个信息减多少:minusyear,minusmoths
LocalDate d3=d1.minusYears(2);
System.out.println(d3);
//5.获得指定日期的 LocalDate对象
LocalDate d4=LocalDate.of(2099,12,12);
System.out.println(d4);
//6.判断两个日期对象,是否相等:equals isBefore isAfter
System.out.println(d4.equals(d));//判断两个日期是否相等
System.out.println(d4.isAfter(d));//判断之后
System.out.println(d4.isBefore(d));//判断之前
LocalTim
//获取本地的时间对象
LocalTime lt=LocalTime.now(); //时 分 秒 纳秒 都是不可变的
System.out.println(lt);
//1.获取时间的信息
int hour= lt.getHour();
int min=lt.getMinute();
int s=lt.getSecond();
int ns=lt.getNano();
System.out.println(hour);
System.out.println(min);
System.out.println(s);
System.out.println(ns);
//2.修改时间:withhour,withMinue,withsecond
LocalTime lt2=lt.withHour(12);
LocalTime lt3=lt.withMinute(13);
LocalTime lt4=lt.withSecond(11);
LocalTime lt5=lt.withNano(500);
//3.加多少:plusHour,plusMiuntes,plussecond
LocalTime lt6=lt.plusHours(12);
//4.减多少:minusHour,minusDays
LocalTime lt7=lt.minusHours(12);
//5.获得指定日期的 LocaTime对象
LocalTime lt8=LocalTime.of(12,12,12);
//6.判断两个日期对象,是否相等:equals isBefore isAfter
System.out.println(lt5.equals(lt));
System.out.println(lt5.isAfter(lt));
System.out.println(lt5.isBefore(lt));
LocalDateTime
//获取本地的时间和日期
LocalDateTime d=LocalDateTime.now();
System.out.println(d);
//1.获取时间和日期的全部信息
int year=d.getYear(); //年
int month=d.getMonthValue();//月
int day=d.getDayOfMonth();//日
int dayofyear=d.getDayOfYear();//一年中的第几天
int dayofmonth=d.getDayOfMonth();//一个月的第几天
int dayofweek=d.getDayOfWeek().getValue();//星期几
int hour= d.getHour();
int min=d.getMinute();
int s=d.getSecond();
int ns=d.getNano();
//2,修改时间信息:withyear,withmoth,withdayofmoth,withhour,withMinue,withsecond
LocalDateTime d3=d.withYear(2099).withMonth(2);
LocalDateTime lt2=d.withHour(12);
LocalDateTime lt3=d.withMinute(13);
LocalDateTime lt4=d.withSecond(11);
LocalDateTime lt5=d.withNano(500);
//3,加多少:
LocalDateTime lt6=d.plusHours(12);
LocalDateTime d1=d.plusYears(2);
//4。减多少:
LocalDateTime lt7=d.minusHours(12);
//5.获得指定日期和时间 LocaDatetime对象
LocalDateTime s1=LocalDateTime.of(12,12,12,12,12);
//6.判断两个日期对象,是否相等:equals isBefore isAfter
System.out.println(lt2.equals(lt4));
System.out.println(lt3.isAfter(lt5));
System.out.println(lt4.isBefore(lt5));
//7.可以把locadatetime转换成locatetime和locatedate
LocalDate ltt=lt2.toLocalDate();
LocalTime ldd=lt2.toLocalTime();
LocalDateTime see=LocalDateTime.of(ltt,ldd);
System.out.println(ltt);
System.out.println(ldd);
System.out.println(see);
//1.Zoneld的常见方法
//1.Zoneld的常见方法
//systemDegfault():获取系统的默认时区
ZoneId zoneId=ZoneId.systemDefault(); //系统的默认时区
System.out.println(zoneId.getId());
//getAvailableZoneIds():获取java支持全部时区ID
System.out.println(zoneId.getAvailableZoneIds());
//把某个时区的id封装成时区id
ZoneId zoneId1=zoneId.of("America/New_York");
System.out.println(zoneId1);
//2.ZonedDatetime:带时区的时间
//ZonedDateTime now() :获取某个时区的ZonedDatetime的对象
ZonedDateTime n=ZonedDateTime.now(zoneId1);
System.out.println(n);
//世界标准时间
ZonedDateTime now1=ZonedDateTime.now(Clock.systemUTC());
System.out.println(now1);
//ZonedDateTime now()获取系统的默认时区的Zonedatetime对象
ZonedDateTime now2=ZonedDateTime.now();
System.out.println(now2);
创建instant的对象,获取此刻的时间信息(不可变对象)
//1.创建instant的对象,获取此刻的时间信息(不可变对象)
Instant now=Instant.now();
System.out.println(now);
//2,获取总秒数
long second=now.getEpochSecond();
System.out.println(second);
//3.不够一秒的纳秒数
int nano=now.getNano();
System.out.println(nano);
//加多少,减多少,判断两个时刻对象,是否相等:equals isBefore isAfter
//instant对象的作用:做代码的性能分析,或者记录用户的操作时间点
Instant now1=Instant.now();
//代码
Instant now2=Instant.now();
DateTimeFormatter
//1.创建一个日期格式化对象 获取格式化对象
DateTimeFormatter formatter =DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH:mm:ss") ; //格式化日期
//2,format():对时间格式化
LocalDateTime now=LocalDateTime.now();
System.out.println(now);
String rs=formatter.format(now); //正向格式化:格式化器格式时间
System.out.println(rs);
//格式化时间:另一种方法
String rs1= now.format(formatter); //反向格式化:时间格式化
System.out.println(rs1);
//4.解析时间:一般使用LocalDatetime提供的解析方法
String se="2020年12月12日 12:12:12";
LocalDateTime lag=LocalDateTime.parse(se,formatter);
System.out.println(lag);
//Period
//Period的作用:计算两个日期的年,月,日
LocalDate start=LocalDate.of(2020,12,12);
LocalDate end=LocalDate.of(2032,12,23);
//1.创建Period的对象,并封装两个日期对象
Period period=Period.between(start,end);
//2,通过period的对象获取两个日期相差的信息
System.out.println(period.getYears());
System.out.println(period.getMonths());
System.out.println(period.getDays());
/Duration
//Duration作用:计算天数, 小时数,秒,纳秒只差
LocalDateTime start=LocalDateTime.of(2020,12,12,12,12,12,11);
LocalDateTime end=LocalDateTime.of(2312,12,12,12,12,12,12);
//1.创建duration的对象
Duration duration=Duration.between(start,end);
//2.获取两个时间对象的信息
System.out.println(duration.toDays());//天
System.out.println(duration.toHours());//小时
System.out.println(duration.toMinutes());//分
System.out.println(duration.toSeconds());//秒
System.out.println(duration.toMillis());//毫秒
System.out.println(duration.toNanos());//纳秒
//Arrays类的常用方法
//Arrays类的常用方法
//1。Arrays.tostring():返回数组的内容
int [] arr={1,12,2,134,21};
System.out.println(Arrays.toString(arr));
//2.copyOfRange(数组名,起始索引,结束索引),拷贝数组包前不包后
int []a={1,2,3,4,5};
int []b=Arrays.copyOfRange(a,1,4);
System.out.println(Arrays.toString(b));
//3.copof(拷贝数组,新数组的长度)
int []c={1,2,3,4,455,6};
int []c1=Arrays.copyOf(c,10);//长度比原来大,多余的会用0
int []c2=Arrays.copyOf(c,2);//长度比原来小,只会录入前几个
System.out.println(Arrays.toString(c1));
System.out.println(Arrays.toString(c2));
//4.setAll(price, new IntToDoubleFunction()把数组中的原数据改为新数据存入进去
double[]price={111,100,123,12};
//将数组大八折
Arrays.setAll(price, new IntToDoubleFunction() {
@Override
public double applyAsDouble(int value) {
//value是数组的索引
return price[value]*0.8;
}
});
System.out.println(Arrays.toString(price));
//5.sort(数组名) :对数组进行默认排序(默认是升序)
Arrays.sort(price);
System.out.println(Arrays.toString(price));
如何对数组中的对象进行排序
//掌握如何对数组中的对象进行排序
Student[] students=new Student[4];
students[0]=new Student("哈哈",180,12);
students[1]=new Student("发hi",180,11);
students[2]=new Student("为我",180,112);
students[3]=new Student("士大夫",180,123);
//方法1;
//sort(数组名) :对数组进行排序
// Arrays.sort(students);
// System.out.println(Arrays.toString(students));
//方法2:
//public static <T> void sort(T[]arr,Comparetor <? super T> c )
// Arrays.sort(students, new Comparator<Student>() {
// @Override
// public int compare(Student o1, Student o2) {
// //约定1:左边的对象大于右边的对象 返回正整数
// //约定2:左边的对象小于右边的对象 返回负整数
// //约定3:左边的对象等于右边的对象 返回0
// if (o1.getHeight()>o2.getHeight()){
// return 1;
// }else if (o1.getHeight()<o2.getHeight()){
// return -1;
// }
// return 0;
// //简便方法
// //return Double.compare(o1.getHeight(),o2.getHeight());
// }
// });
Arrays.sort(students, (Student o1, Student o2)->{
return Double.compare(o1.getHeight(),o2.getHeight());
});
System.out.println(Arrays.toString(students));
//lambda:用于简化匿名内部类
//lambda:用于简化匿名内部类
animal a=new animal() {
@Override
public void run() {
System.out.println("狗跑到飞快");
}
};
//注意:lambda的表达式不是能简化全部的匿名内部类,只能简化函数式接口的匿名内部类,且接口里面只能有一个抽象方法
// animal b=()->{
// System.out.println("狗哈哈哈");
// }
//
// Swimming s=new Swimming() {
// @Override
// public void swim() {
// System.out.println("ahahahhhah");
// }
// }
//
//
// }
Swimming s=()->{
System.out.println("hahahhahh");
};
s.swim();
}
}
interface Swimming{
void swim();
}
abstract class animal{
public abstract void run();
}
/lambda表达式进一步的写法
//lambda表达式进一步的写法
//参数类型可以不写
//如果只有一个参数,参数类型可以省略,同时()可以省略
//如果lambda表达式中只有一行代码,{}可以省略,';'可以省略,此时如果这行代码是return语句,return可以省略
//setAll(price, new IntToDoubleFunction()把数组中的原数据改为新数据存入进去
double[]price={111,100,123,12};
//将数组大八折
// Arrays.setAll(price, new IntToDoubleFunction() {
// @Override
// public double applyAsDouble(int value) {
// //value是数组的索引
// return price[value]*0.8;
// }
// });
//正常lambda的表达式
// Arrays.setAll(price, (int value)-> {
// //value是数组的索引
// return price[value]*0.8;
// });
// Arrays.setAll(price, (value)-> {
// return price[value]*0.8;
// });
// Arrays.setAll(price, value-> {
// return price[value]*0.8;
// });
Arrays.setAll(price, value-> price[value]*0.8);
System.out.println("-----------------------------------------------------------");
System.out.println(Arrays.toString(price));
Student[] students=new Student[4];
students[0]=new Student("哈哈",180,12);
students[1]=new Student("发hi",180,11);
students[2]=new Student("为我",180,112);
students[3]=new Student("士大夫",180,123);
//sort(数组名) :对数组进行排序
// Arrays.sort(students);
// System.out.println(Arrays.toString(students));
//原方法
//public static <T> void sort(T[]arr,Comparetor <? super T> c )
// Arrays.sort(students, new Comparator<Student>() {
// @Override
// public int compare(Student o1, Student o2) {
// if (o1.getHeight()>o2.getHeight()){
// return 1;
// }else if (o1.getHeight()<o2.getHeight()){
// return -1;
// }
// return 0;
// //简便方法
// //return Double.compare(o1.getHeight(),o2.getHeight());
// }
// });
// Arrays.sort(students, (Student o1, Student o2)->{
//
// return Double.compare(o1.getHeight(),o2.getHeight());
// });
// Arrays.sort(students, (o1,o2)->{
//
// return Double.compare(o1.getHeight(),o2.getHeight());
// });
Arrays.sort(students, (o1,o2)->Double.compare(o1.getHeight(),o2.getHeight()));
System.out.println(Arrays.toString(students));