正则表达式
==============================================
* 字符串匹配规则语法
* 百度:正则表达式大全
* java.util.regex.Pattern
java.util.regex.Matcher
--------------------------------
在字符串当中查找所有匹配的子串:
String s = sc.nextLine();
String regex = "\\d{3,}";
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(s);
while(m.find()) { // 寻找下一个匹配子串
String n = m.group(); // 获得刚刚找到的子串
int start = m.start();// 子串起始位置
int end = m.end();// 子串结束位置(不含)
System.out.println(
"["+start+","+end+") - "+n);
}
正则表达式 字符串
--------------- -----------------------
a a
abc abc
[abc] a、b、c
[a-z] a、k、z、f
[0-0a-z_A-Z] 5、a、K、9、A、B、z、_
[abc]+ a、ac、b、abc、aabbabcabc
+:1..*
0[abc]*9 0a9、09、0ababccabc9
*:0..*
0[abc]?9 09、0a9、0c9
?:0,1
[^abc] z、9、(
. 任意字符
\d 数字
\D 非数字
\s 空白字符
\S 非空白字符
\w [0-9a-zA-Z_] 单词字符
\W 非单词字符
^[abc]\d[abc]$ a6c、b9c
^:匹配开头
$:匹配结尾
?
*
+
\d{2,5} 35、391、9174、18346
\d{3} 432、456、643、234
\d{3,} 342、234536256342
\d|[a-z] 3、a、6、z
基本类型的封装类
=================================================
* 基本类型不面向对象
* 用面向对象的方式处理基本数据,要将基本类型数据包装为对象
byte - Byte
short - Short
int - Integer
long - Long
float - Float
double - Double
char - Character
boolean - Boolean
数字父类(了解)
-------------------------------------
java.lang.Number 抽象类
|- Byte
|- Short
|- Integer
|- Long
|- Float
|- Double
java.lang.Integer
-------------------------------------
Integer.valueOf(10)
Integer.parseInt("2345")
Integer.parseInt("ff", 16)
Integer.parseInt("77", 8)
Integer.parseInt("11111111", 2)
Integer.toBinaryString(255) - "11111111"
Integer.toHexString(255) - "ff"
Integer.toOctalString(255) - "377"
byteValue()
shortValue()
intValue()
longValue()
floatValue()
doubleValue()
java.lang.Double
-------------------------------------
Double.valueOf(3.14)
Double.parseDouble("3.141592653")
Double.isInfinite(double d) - 检查double 的特殊值 Infinity
Double.isNaN(double d) - 检查double 的特殊值 NaN
自动装箱
自动拆箱
-------------------------------------
* JDK 1.5 版本中的新语法
装箱:
---------------------------
Integer i1 = 10; // Integer i1 = Integer.valueOf(10);
拆箱:
---------------------------
int n = i1; // int n = i1.intValue();
* 当心: null 值无法完成自动拆箱
public void a(Integer n) {
System.out.println(n + 1);
}
java.math.BigDecimal
java.math.BigInteger
=================================================
* 做精确浮点数运算
* 做超出长整形范围的整数运算
* 浮点数不精确: 2.0-1.9, 4.35*100
精确运算:
-------------------------------
创建 BigDecimal 实例:
BigDecimal bd1 = BigDecimal.valueOf(2.0)
BigDecimal bd2 = BigDecimal.valueOf(1.9)
四则运算方法:
BigDecimal bd3 = bd1.add(bd2);
BigDecimal bd4 = bd1.subtract(bd2);
BigDecimal bd5 = bd1.multiply(bd2);
BigDecimal bd6 = bd1.divide(bd2);
取出运算结果
double d = bd3.doubleValue()
float f = bd3.floatValue()
int i = bd3.intValue()
String s = bd3.toString()
java.util.Date
================================================
* 表示一个时间点
* Date 内部包装着一个长整型毫秒值
毫秒值:1970-1-1 日 0 点开始的毫秒值
构造方法:
----------------------------------------------
Date d1 = new Date(); // 表示系统当前时间
Date d2 = new Date(毫秒); // 表示指定的毫秒值时间
方法:
----------------------------------------------
toString() - 获得长格式的时间字符串
getTime() - 获得内部封装的长整型毫秒值
java.text.SimpleDateFormat
================================================
* 日期格式控制工具
* 可以将 Date 实例转为指定格式字符串
* 将日期字符串按指定格式转为 Date 实例
构造方法:
------------------------------------------
SimpleDateFormat f =
new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss");
* 格式字符参考 SimpleDateFormat 文档
方法:
------------------------------------------
format(Date实例) - Date --> String
parse(String) - String --> Date
java.util.Calendar 抽象父类
java.util.GregorianCalendar
===============================================
* Calendar 与 Date 区别:
*) Date 仅表示时间点
*) Calendar 可以对时间进行运算
构造方法:
---------------------------------------------
Calendar c1 = new GregorianCalendar();
Calendar c2 = Calendar.getInstance();
* 创建的日历都表示系统当前时间
方法:
---------------------------------------------
get(字段) - 获得指定的字段的值
set(字段, 值) - 设置指定字段的值
getTime() - 获得 Date 实例
add(int field, int amount) - 指定字段上加一个值
getActualMaximum(int field) - 获得指定字段的实际最大值,用于获取一个月的最大天数
getFirstDayOfWeek() - 获得一周的第一天是周几
setFirstDayOfWeek(int value)- 设置一周的第一天是周几
SUNDAY - 1
MONDAY - 2
TUESDAY - 3
WEDNESDAY - 4
THURSDAY - 5
FRIDAY - 6
SATURDAY - 7
getTimeInMillis() - 获得70年1月1日0点开始的毫秒值
setTimeInMillis(long millis)- 设置毫秒值
setTime(Date d) - 设置为 Date 实例表示的时间点