Date类:类 Date 表示特定的瞬间,精确到毫秒。Date是早期版本出现的,其API中有大量的过时方法,其许多方法由更为新的类Calendar类再次实现。
☆常见方法:
构造方法:public Date() //返回当前时间;public Date(long date) //返回指定毫秒值的日期对象
普通方法:public long getTime() //获取当前时间对象的毫秒值;public void setTime(long time) //设置时间毫秒值
DateFormat类:DateFormat是用于日期格式化类,即将一个Date对象转换为一个符合指定格式的字符串,也可以将一个符合指定格式的字符串转为一个Date对象。DateFormat是抽象类,我们需要使用其子类SimpleDateFormat。
☆常见方法:
构造方法:public SimpleDateFormat() //使用默认格式创建格式化对象
public SimpleDateFormat(String pattern) //使用指定格式创建格式化对象
主要方法:public final String format(Date date) //将Date转为字符串
public Date parse(String source) //将字符串转为Date(转换时,该String要符合指定格式,否则不能转换)
public class Demo {
public static void main(String[] args) throws ParseException {
Date now = new Date();//创建当前时间日期对象
DateFormat df = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");//使用指定格式创建格式化对象
String sDate = df.format(now);//调用format方法将日期转换为字符串
System.out.println(now);
System.out.println(sDate);
System.out.println("--------------------------------------------");
String birth = "1949-10-01";//1949-10-01 求给定字符串日期的毫秒值
DateFormat df2 = new SimpleDateFormat("yyyy-MM-dd");//创建日期格式化对象
Date d = df2.parse(birth);//调用parse方法将字符串转换为日期对象
System.out.println(d.getTime());//调用Date类的getTime方法获取毫秒值
}
}
Calendar类:
Calendar是日历类,在Date后出现,替换掉了许多Date的方法。该类将所有可能用到的时间信息封装为字段值,方便获取。Calendar为抽象类,由于语言敏感性,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() //获取该日历对象转成的日期对象
public class Demo {
public static void main(String[] args) {
Calendar rightNow = Calendar.getInstance();//获取日期对象
System.out.println(rightNow);
int i = rightNow.get(Calendar.YEAR);//获取时间字段值
System.out.println(i);
rightNow.add(Calendar.YEAR, -1);//给指定年份减1
System.out.println(rightNow.get(Calendar.YEAR));
rightNow.set(2017, 9, 10);
System.out.println(rightNow);
long time = rightNow.getTime().getTime();//日历对象转成的日期对象并获取毫秒值
long timeInMillis = rightNow.getTimeInMillis();//日历对象获取毫秒值
System.out.println(time);
System.out.println(timeInMillis);
}
}
//输出当前日期
public class Demo {
public static void main(String[] args) {
Date d = new Date();//1、获取当前日期对象
SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy年MM月dd日 今天是今年的第D天");
SimpleDateFormat sdf3 = new SimpleDateFormat("yyyy.MM.dd E");
String f1 = sdf1.format(d);
String f2 = sdf2.format(d);
String f3 = sdf3.format(d);
System.out.println(f1);
System.out.println(f2);
System.out.println(f3);
}
}
//获取指定年份的指定月份有多少天
public class Demo {
public static void main(String[] args) {
int day = showDay(2018,2);
System.out.println(day);
}
public static int showDay(int year,int month) {
Calendar c = Calendar.getInstance();
c.set(year, month+1, 1);//获取到指定年份的下月1日
c.add(Calendar.DATE, -1);//得到当月最后一天
int day = c.get(Calendar.DAY_OF_MONTH);//获取到当月天数
return day;
}
}
//出生了多少天
public class Demo {
public static void main(String[] args) throws ParseException{
String birth = "2010-01-01";
long day = showDay(birth);
System.out.println(day);
}
public static int showDay(String birth) throws ParseException {
DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
Date parse = df.parse(birth);//调用parse方法将字符串转换成日期
long time1 = parse.getTime();//获取给定时间的毫秒值
long time2 = new Date().getTime();//创建当前时间毫秒值
long day = (time2-time1)/1000/60/60/24;
return day;
}
}
System类:System中代表程序所在系统,提供了对应的一些系统属性信息,和系统操作。
☆常见方法:public static void gc() //用来运行JVM中的垃圾回收器,完成内存中垃圾的清除
public static void exit(int status) //用来结束正在运行的Java程序。参数传入一个数字即可。通常传入0记为正常状态,其他为异常状态
public static long currentTimeMillis() //获取当前系统时间与1970年01月01日00:00点之间的毫秒差值,与long time = new Date().getTime();long timeInMillis = Calendar.getInstance().getTimeInMillis();long timeInMillis = Calendar.getInstance().getTime.getTime();方法无异
public static Properties getProperties() //用来获取指定键(字符串名称)中所记录的系统属性信息
☆注意事项:System类不能手动创建对象,因为构造方法被private修饰,阻止外界创建对象。System类中的都是static方法,类名访问即可。在JDK中,有许多这样的类。
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) //a^b
public static double random() //产生随机数
public static int round(float a) //四舍五入
public class Demo {
public static void main(String[] args) {
System.out.println(Math.abs(-10));//绝对值
System.out.println(Math.round(10.1));//10四舍五入
System.out.println(Math.round(10.8));//11
System.out.println(Math.round(-10.1));//-10
System.out.println(Math.round(-10.8));//-11
System.out.println(Math.ceil(10.1));//11向上取整
System.out.println(Math.ceil(10.8));//11
System.out.println(Math.ceil(-10.1));//-10
System.out.println(Math.ceil(-10.8));//-10
System.out.println(Math.floor(10.1));//10向下取整
System.out.println(Math.floor(10.8));//10
System.out.println(Math.floor(-10.1));//-11
System.out.println(Math.floor(-10.8));//-11
System.out.println(Math.max(10.1, 20.2));//20.2取最大值
System.out.println(Math.pow(2, 8));//public static double pow(double a,double b) //a的b次方
System.out.println(Math.random());// 0.1之间随机小数 包含0 不包含1
}
}
基本类型包装类:Java中提供了相应的对象来解决字符串与基本数据之间的转换问题。基本数据类型对象包装类:java将基本数据类型值封装成了对象。
字节型 | 短整型 | 整型 | 长整型 | 字符型 | 布尔型 | 浮点型 | 浮点型 | |
---|---|---|---|---|---|---|---|---|
基本数据类型 | byte | short | int | long | char | boolean | float | double |
包装类 | Byte | Short | Integer | Long | Character | Boolean | Float | Double |
☆常见方法:
字符串→基本类型:public static byte parseByte(String s) //将字符串解析为有符号的十进制byte
public static short parseShort(String s) //将字符串解析为有符号的十进制short
public static int parseInt(String s)//将字符串解析为有符号的十进制int
public static long parseLong(String s)//将字符串解析为有符号的十进制long
public static float parseFloat(String s)//返回一个被字符串初始化的float值
public static double parseDouble(String s)//返回一个被字符串初始化的double值
public static boolean parseBoolean(String s)//将字符串解析为boolean
如果字符串无法转成基本类型,将会发生数字转换的问题 NumberFormatException
基本类型→字符串:基本类型转为字符串非常简单,直接与空字符串相加即可。
☆自动装箱拆箱:基本数据类型包装类这些引用数据类型与其他引用数据类型有些不同,他们有更强大的功能,即自动装箱拆箱。在需要的情况下,基本类型与包装类型可以通用。有些时候我们必须使用引用数据类型时,可以传入基本数据类型。
public class Demo {
public static void main(String[] args) {
String s = "123";
int i = Integer.parseInt(s);//将字符串解析为有符号的十进制int
String ss = i+"";
System.out.println(ss);
Integer in1 = new Integer(10);
Integer in2 = new Integer(20);
System.out.println(in1- in2);//计算时自动拆箱
ArrayList<Integer> list = new ArrayList<>();
list.add(10);//自动装箱
int i2 = list.get(0);//自动拆箱
Integer aa = 10; //10这个基本类型值自动装箱成了引用类型
int bb = new Integer("10"); //引用数据类型自动拆箱成了基本类型
Integer c = bb + aa; //计算时,a自动拆箱为基本类型,结果自动装箱成引用类型。
}
}
简单正则表达式:正则表达式(英语:Regular Expression,在代码中常简写为regex)。正则表达式是一个字符串,使用单个字符串来描述、用来定义匹配规则,匹配一系列符合某个句法规则的字符串。在开发中,正则表达式通常被用来检索、替换那些符合某个规则的文本。
☆正则表达式的匹配规则:
类别 | 具体 | 含义 | 举例 |
---|---|---|---|
字符 | x | 代表的是字符x | 匹配规则为 “a”,那么需要匹配的字符串内容就是 ”a” |
字符 | \ | 代表的是斜线字符’’ | 匹配规则为"\" ,那么需要匹配的字符串内容就是 ”\” |
字符 | \t | 代表的是制表符 | 匹配规则为"\t" ,那么对应的效果就是产生一个制表符的空间 |
字符 | \n | 代表的是换行符 | 匹配规则为"\n",那么对应的效果就是换行,光标在原有位置的下一行 |
字符 | \r | 代表的是回车符 | 匹配规则为"\r" ,那么对应的效果就是回车后的效果,光标来到下一行行首 |
字符类 | [abc] | 代表的是字符a、b 或 c | 匹配规则为"[abc]" ,那么需要匹配的内容就是字符a,或者字符b,或字符c的一个 |
字符类 | [^abc] | 代表的是除了 a、b 或 c以外的任何字符 | 匹配规则为"[^abc]",那么需要匹配的内容就是不是字符a,b,c的任意一个字符 |
字符类 | [a-zA-Z] | 代表的是a 到 z 或 A 到 Z,两头的字母包括在内 | 匹配规则为"[a-zA-Z]",那么需要匹配的是一个大写或者小写字母 |
字符类 | [0-9] | 代表的是 0到9数字,两头的数字包括在内 | 匹配规则为"[0-9]",那么需要匹配的是一个数字 |
字符类 | [a-zA-Z_0-9] | 代表的字母或者数字或者下划线(即单词字符) | 匹配规则为" [a-zA-Z_0-9] ",那么需要匹配的是一个字母或者是一个数字或一个下划线 |
预定义字符类 | . | 代表的是任何字符 | 匹配规则为" . “,那么需要匹配的是一个任意字符。如果,就想使用 . 的话,使用匹配规则”\."来实现 |
预定义字符类 | \d | 代表的是 0到9数字,两头的数字包括在内,相当于[0-9] | 匹配规则为"\d ",那么需要匹配的是一个数字 |
预定义字符类 | \w | 代表的字母或者数字或者下划线(即单词字符),相当于[a-zA-Z_0-9] | 匹配规则为"\w ",,那么需要匹配的是一个字母或者是一个数字或一个下滑线 |
边界匹配器 | ^ | 代表的是行的开头 | 匹配规则为^[abc][0-9]$ ,那么需要匹配的内容从[abc]这个位置开始, 相当于左双引号 |
边界匹配器 | $ | 代表的是行的结尾 | 匹配规则为^[abc][0-9]$ ,那么需要匹配的内容以[0-9]这个结束, 相当于右双引号 |
边界匹配器 | \b | 代表的是单词边界 | 匹配规则为"\b[abc]\b" ,那么代表的是字母a或b或c的左右两边需要的是非单词字符([a-zA-Z_0-9]) |
数量词 | X? | 代表的是X出现一次或一次也没有 | 匹配规则为"a?",那么需要匹配的内容是一个字符a,或者一个a都没有 |
数量词 | X* | 代表的是X出现零次或多次 | 匹配规则为"a*" ,那么需要匹配的内容是多个字符a,或者一个a都没有 |
数量词 | X+ | 代表的是X出现一次或多次 | 匹配规则为"a+",那么需要匹配的内容是多个字符a,或者一个a |
数量词 | X{n} | 代表的是X出现恰好 n 次 | 匹配规则为"a{5}",那么需要匹配的内容是5个字符a |
数量词 | X{n,} | 代表的是X出现至少 n 次 | 匹配规则为"a{5, }",那么需要匹配的内容是最少有5个字符a |
数量词 | X{n,m} | 代表的是X出现至少 n 次,但是不超过 m 次 | 匹配规则为"a{5,8}",那么需要匹配的内容是有5个字符a 到 8个字符a之间 |
逻辑运算符 | XY | 代表的是X后跟Y | 匹配规则为"ab",那么需要匹配的字符串内容就是 ”ab” |
逻辑运算符 | XY | 代表的是X后跟Y | 匹配规则为"ab",那么需要匹配的字符串内容就是 ”ab” |
逻辑运算符 | X|Y | 代表的是X 或 Y | 匹配规则为"a |
String类中,使用正则表达式匹配完整字符串的方法为:
public boolean matches(String regex) 调用方法的字符串为被规则匹配的字符串,regex字符串为匹配规则,返回值为布尔型,符合规则返回true,不符合返回false
public class Demo {
public static void main(String[] args) {
String data = "[email protected]";
boolean result = data.matches("\\w+@\\w{2,7}\\.\\w{2,3}");//邮箱规则:String regex = "\\w+@\\w{2,7}\\.\\w{2,3}";
System.out.println(result);//返回结果为true
String data2 = “itheima.cn”;
boolean result2 = data2.matches("\\w+@\\w{2,7}\\.\\w{2,3}"); //返回结果为false
String number="1356757888";//手机号规则:String regex = "[1][3,4,5,7,8][0-9]{9}";
boolean result3 = number.matches("[1][3,4,5,7,8][0-9]{9}");//返回结果为true
}
public String[] split(String regex) //将符合规则的字符串作为切割符切割
public String replaceAll(String regex,String replacement) //将符合规则的字符串替换为新字符串
public class Demo {
public static void main(String[] args) {
String girlName = "姗姗.慧慧.柳柳.莲莲.糖糖 ";
String[] girlNames = girlName.split("\\.");
for (int i = 0; i < girlNames.length; i++) {
String string = girlNames[i];
System.out.println(string);
}
String replaceAll = girlName.replaceAll("柳", "岩岩");
System.out.println(replaceAll);
}
}