常用类
Math类
Math类中包含一些对数据进行数学运算的方法,而该类中的方法全都是静态的。像这样的类称之为工具类
public static int abs(int a)
对一个数据求绝对值
public static double ceil(double n)
对一个小数向上取整 4.3 --> 5.0
public static double floor(double n)
对一个小数向下取整 4.3 --> 4.0
public static int round(double n)
对一个小数进行四舍五入 4.3 --> 4 ; 4.5 --> 5
public static int max(int a,int b)
求两个数的最大值
public static int min(int a,int b)
求两个数的最小值
public static double random()
生成[0,1)范围内的随机数
System类
public static void exit(int n)
退出Java虚拟机 //System.exit(0);
public static long currentTimeMillis()
获取当前时间的毫秒值,距离1970年1月1日0时0分0秒之间的毫秒值
public static void arraycopy(Object src, int srcPos,Object dest,int destPos,int length)
把源数组中的元素复制到目标数组中。
参数:
src - 源数组。
srcPos - 源数组中的起始位置。
dest - 目标数组。
destPos - 目标数据中的起始位置。
length - 要复制的数组元素的数量。
int[] array={1,2,3,4,5};
int[] array1=new int[2];
System.arraycopy(array,2,array1,0,2);
Object类
Object是所有类的父类,任何一个子类都可以调用Object的方法。
public String toString()
把一个对象转换字符串。 字符串格式: 包名+类名@地址值。
每次打印对象,底层会自动去调用toString()方法。
注意:自己写的类,如果不想打印地址就可以复写toString()方法
快捷键:alt+inert-->toString()
public boolean equals(Object obj)
用来比较两个对象是否“相等“。
默认比较的是地址值,底层用的是"=="
注意:自己写的类,如果不想比较地址值可以重写equals()方法,比较自己的内容.
快捷键:alt+inert-->Equals and HashCode
Objects类
Objects类是一个工具类,经常用于对一个对象进行判空的处理。JDK7以后才有的
public static String toString(Object s,String defalutStr)
把一个对象转换为字符串, 如果对象为null,返回默认字符串
public static boolean isNull(Object s)
判断一个对象是否为null 等价于 s==null
public static boolean nonNull(Object s)
判断一个对象不为null 等价于 s!=null
BigDegimal类
BigDegimal可以做一些小数的精确运算,还可做一些较大数据的运算。
public BigDecimal add(BigDecimal augend)
对两个数进行加法运算
public BigDecimal subtract(BigDecimal subtrahend)
对两个数进行减法运算
public BigDecimal multiply(BigDecimal multiplicand)
对两个数进行乘法运算
public BigDecimal divide(BigDecimal divisor)
对两个数进行除法运算
public BigDecimal divide(BigDecimal divisor,int num,int type)
对两个数进行除法运算,可以保留小数个数.
参数:
BigDecimal divisor: 除数
int num:保留小数个数
int type: 保留小数的方式
BigDecimal.ROUND_HALF_UP 最后一位小数四舍五入
BigDecimal.ROUND_UP 最后一位小数不管满不满5都进1
BigDecimal.ROUND_FLOOR 最后一位小数不管慢不满5都舍弃
注意:需要运算的两个数,必须先封装成BigDecimal对象,然后通过上面的方法进行运算。
基本数据类型包装类
每一个基本数据类型都有一个对应的包装类,并且提供了一些方法给开发人员使用。
基本数据类型 包装类
byte Byte
short Short
int Integer
long Long
float Float
double Double
boolean Boolean
char Character
获取Integer对象
Integer num1=Integer.valueOf(100);
Integer num2=Integer.valueOf("100");
自动装箱和自动拆箱
为了方便基本数据类型和包装类能够直接运算,所有Java提供了自动装箱和自动拆箱的机制。
//自动装箱: 基本类型(int) 自动转换为 包装类(Integer)
Integer num=100;
//自动拆箱: 包装类(Integer) 自动转换为 基本类型(int)
int num2=num;
包装类的数据转换
如果一个字符串是纯数字的字符串(如: "12345"
),不能做数值的数学运算,必须把字符串转换为数值,才能做数学运算。 这样的转换方法在包装类中已经提供了.
- 字符串转其他的基本数据类型
//需求1:"12345"转换为12345
int num1=Integer.parseInt("12345");
//需求2: “3.14”转换为3.14
double num2=Double.parseDouble("3.14");
//需求3: "6.88"转换为6.88
float num3=Float.parseFloat("6.88");
口诀:要转换为什么类型,就找什么类型的包装类,parseXxx方法
把“1234”转换为int
int num =Integer.parseInt("12345");
- 其他类型转换为字符串
1.直接和""做拼接,因为任何一个数据和字符串做拼接结果都是字符串
String s=100+"";
2.可以使用String类的valueOf方法
String s1=String.valueOf(100);
String s2=String.valueOf(3.14);
Arrays类
Arrays是一个工具类。Arrays类中提供了一些对数组进行操作的方法(比如:查找和排序)
public static int binarySearch(int[] array,int key)
对数组的元素进行二分查找,返回的是索引或-1
public static void sort(int[] array)
对数组进行排序。
public static String toString(int[] array)
把数组转换为字符串 格式:"[元素1,元素2,元素3]"
String类
比较相等
public boolean equals(Object anObject)
比较字符串内容是否相等
public boolean equalsIgnoreCase(String anotherString)
比较字符串内容是否相等,不考虑大小写。
字符串遍历
public int length()
返回此字符串的长度(字符的个数)
public char charAt(int index)
返回指定索引处的 char 值。
public char[] toCharArray()
将此字符串转换为一个新的字符数组。
字符串截取
public String substring(int beginIndex)
从指定的索引位置开始(包含),截取到末尾,得到一个新的字符串
public String substring(int beginIndex, int endIndex)
从指定的开始索引(包含)到结束索引(不包含),截取中间一部分,得到一个新的字符
需求:屏蔽手机号码的中间几位
分析思路:
1)准备一个字符串当做手机号码
2)截取手机号码的前3位
3)截取手机号码的后4位
4)在前三位和后四位之间拼接"****"
public class Demo4{
public static void main(String[] args){
//1)准备一个字符串当做手机号码
String phone="13838384388";
//2)截取手机号码的前3位
String s1=phone.substring(0,3);
//3)截取手机号码的后4位
String s2=phone.substring(7);
//4)在前三位和后四位之间拼接"****"
String s3=s1+"****"+s2;
System.out.println("屏蔽后:"+s3);
}
}
字符串替换
public String replaceAll(String regex, String replacement)
使用新的字符串替换旧的字符串
public class Demo5 {
public static void main(String[] args) {
String s = "你TMD真是个人渣";
//把"TMD"替换为***,把"人渣"替换为"人才"
String s2 = s.replaceAll("TMD", "***").replaceAll("人渣", "人才");
System.out.println(s2);
//替换一个字符
String s3 = s.replace('你','我');
System.out.println(s3);
//扩展:replaceAll可以完成更高级的替换
String str = "我的电话号码是1303808438,我空约哦!";
//把手机号码替换为****
String s3 = str.replaceAll("[0-9]{11}", "****");
System.out.println(s3);
}
}
切割功能
public String[] split(String regex)
使用给定的字符串切割字符串,得到一个字符串数组
注意:"."表示任意字符,可以使用"\\."进行切割
补充方法
public boolean contains(CharSequence s)
判断字符串中是否包含某一个子字符串
public boolean startsWith(String prefix)
判断字符串以什么前缀开头
public boolean endsWith(String suffix)
判断字符串以什么后缀结尾
public int indexOf(String str)
获取字符串中子字符串第一次出现的索引
public int lastIndexOf(String str)
获取字符串中子字符串最后一个出现的位置
public char[] toCharArray()
把字符串转换为字符数组
public String toLowerCase()
把字符串转换为小写
public String toUpperCase()
把字符串转换为大写
public byte[] getBytes()
把字符串转换为字节数组
StringBuilder类
StringBuilder是一个可变的字符串,它可以理解成是一个容器,可以用来对字符串进行操作(拼接)
public StringBuilder append(String s)
往容器末尾添加字符串等数据,返回值是StringBuiler本身
注意:不管添加什么类型的数据,都会转换为字符串
public StringBuilder reverse()
把StringBuilder容器中的内容反转,返回值是StringBuiler本身
public String toString()
把StringBuilder转换为String
public int length()
获取StringBuilder容器中字符串的长度
- StringBuilder和String的相互转换
//1.把String转换为StringBuilder
StringBuilder sb = new StringBuilder("abc");
//2.把StringBuilder转换为String
String s = sb.toString();
- StringBuilder在实际中的运用
如果存在大量的拼接操作,建议使用StringBuilder的append方法替换"+"操作.
int[] array = {1,2,3,4,5};
//把数组转换为字符串,举例:[1,2,3,4,5]
StringBuilder sb=new StringBuilder("[");
for(int i=0; i<array.length; i++){
if(i<array.length-1){
sb.append(array[i]).append(",");
}else{
sb.append(array[i]).append("]");
}
}
//把StringBuilder转换为字符串
String s=sb.toString();
System.out.println(s);
JDK7的日期时间类。
Date类
Date类表示时间,时间可以精确到毫秒。创建一个Date对象,其实就表示时间的对象。
Date date1=new Date(); //当前系统时间
Date date2=new Date(0L); //1970年1月1日8点0时0分 (中国)
Date date3=new Date(1000L*60*60*24); //1970年1月2日8点0时0分 (中国)
获取和设置时间
Date date=new Date(); //当前系统时间
//设置时间为
date.setTime(1000L*60*60*24); //1970年1月2日8点0时0分 (中国)
//获取时间
long time=date.getTime(); //1000L*60*60*24
System.out.println(time); //毫秒值
SimpleDateFormat类
SimpleDateFormat类可以对日期进行格式化和解析的操作。
format格式化:把Date对象转换为字符串
prase解析:把字符串转换为Date对象
SimpleDateFormat在进行日期格式化或者解释时,需要识别一些特定符号
yyyy: 年 1999年
MM: 年中的月 8月
dd: 月中的天 28日
HH: 天中的小时 16时
mm: 时中的分 20分
ss: 分中的秒 18秒
SS: 毫秒
//在进行日期格式化和解析之前,必须使用以上的符号创建SimpleDateFormat对象,来指定日期或者时间的格式
SimpleDateFormat sdf=new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒");
- 下面是格式化和解析的代码
SimpleDateFormat sdf=new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒");
//1.日期格式化:Date对象-->String
String str = sdf.format(new Date()); //把当前时间格式化为字符串
//2.日期解析:String --> Date对象
Date date = sdf.prase("1992年08月09日 16时24分18秒");
- 练习:键盘录入一个生日(格式:yyyy-MM-dd),计算当前的年龄是多少?
//键盘录入字符串生日
Scanner sc=new Scanner(System.in);
System.out.println("请输入你的生日(格式:yyyy-MM-dd):");
String birthday=sc.nextLine():
//把生日的字符串,转换为Date
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
Date date1=sdf.prase(birthday); //生日那天的Date对象
long time1=date1.getTime(); //生日那天的毫秒值
//获取当前时间的毫秒值
long time2=System.currentTimeMills(); //当前时间的毫秒值
//计算时间差
long age=(time2-time1)/1000/60/60/24/365;
System.out.println("年龄为:"+age);
JDK8新增的日期类
在JDK中表示时间的对象有三个:
LocalDate: 表示年、月、日
LocalTime: 表示时、分、秒
LocalDateTime: 表示年、月、日、时、分、秒
LocalDateTime获取对象
LocalDateTime类 可以表示时间的(年、月、日、时、分、秒)
public static LocalDateTime now()
获取当前的系统时间LocalDateTime对象
public static LocalDateTime of(int y,int m,int d,int h,int m,int s)
获取指定时间的LocalDateTime对象
LocalDateTime的获取方法
public int getYear()
获取年
public int getMonthValue()
获取月
public int getDayOfMonth()
获取月中的天
public int getDayOfYear()
获取年中的天
public DayOfWeek getDayOfWeek()
获取星期中的第几天
public int getHour()
获取小时
public int getMinute()
获取分钟
public int getSecond()
获取秒
LocalDateTime的转换方法
三者的区别
LocalDateTime: 包含年、月、日、时、分、秒
LocalDate: 包含年、月、日
LocalTime: 包含时、分、秒
可以通过LocalDateTime转换为LocalDate和LocalTime
public LocalDate toLocalDate()
把LocalDateTime转换为LocalDate
public LocalTime toLocalTime()
把LocalDateTime转换为LocalTime
LocalDateTime的格式化和解析
public String format(DateTimeFormatter formatter)
格式化:把LocalDateTime转换为String
public static LocalDateTime parse(CharSequence text, DateTimeFormatter formatter)
解析:把字符串转换为LocalDateTime对象
- 日期格式化代码演示
//创建日期格式化器
DateTimeFormatter formatter =
DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
//获取当前时间的LocalDateTime对象
LocalDateTime localDateTime = LocalDateTime.now();
//格式化
String str=localDateTime.format(formatter);
System.out.println(str);
- 日期解析的代码演示
//创建日期格式化器
DateTimeFormatter formatter =
DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
//日期解析
LocalDateTime localDateTime=
LocalDateTime.parse("2020-08-23 15:19:39",formatter);
//...后面就可以使用LocalDateTime的方法,对时间进行操作
// (获取年月日、加减年月日、修改年月日等)
LocalDateTime加减运算方法
public LocalDateTime plusYears(int n)
加、减年
public LocalDateTime plusMonths(int n)
加、减月
public LocalDateTime plusDays(int n)
加、减日
public LocalDateTime plusHours(int n)
加、减时
public LocalDateTime plusMinutes(int n)
加、减分
public LocalDateTime plusSeconds(int n)
加、减秒
LocalDateTime设置时间的方法
public LocalDateTime withYear(int n)
设置年
public LocalDateTime withMonth(int n)
设置月
public LocalDateTime withDay(int n)
设置日
public LocalDateTime withHour(int n)
设置时
public LocalDateTime withMinute(int n)
设置分
public LocalDateTime withSecond(int n)
设置秒
时间间隔类
Period类
计算两个时间间隔的年、月、日。
//生日的时间
LocalDate localDate1 = LocalDate.of(1996, 6, 23);
//今天的时间
LocalDate localDate2 = LocalDate.now();
//获取时间间隔对象
Period period = Period.between(localDate1,localDate2);
//获取相隔的年
int years = period.getYears();
System.out.println(years);
...
Duration类
计算两个时间间隔的时、分、秒.
//生日的时间
LocalDateTime localDate1 = LocalDateTime.of(1996, 6, 23,13,34,22);
//今天的时间
LocalDateTime localDate2 = LocalDateTime.of(1996, 7, 13,06,34,11);
//获取时间间隔对象
Duration duration = Duration.between(localDate1,localDate2);
//获取相隔的小时
long totalHours = duration.toHours();
//获取相隔的分钟
long totalMinutes = duration.toMinutes();
//获取相隔的秒
long totalSeconds = duration.getSeconds();
//相隔的毫秒
long totalMillis = duration.toMillis();
//相隔时间小时的部分 2天3小时30分钟50秒
long hour= duration.toHoursPart();
//相隔时间分钟的部分
long min= duration.toMinutesPart();
//相隔时间的秒的部分
long secound=duration.toSecondsPart();
版权声明:本文为weixin_60851587原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。