正则表达式
package haha;
public class Normal1 {
public static void main ( String[ ] args) {
System. out. println ( checkQQ ( "012345" ) ) ;
System. out. println ( checkQQ ( "a1b345" ) ) ;
System. out. println ( checkQQ ( "123456" ) ) ;
System. out. println ( checkQQ ( "1234567890987654321" ) ) ;
String regex= "[1-9]\\d{4,14}" ;
System. out. println ( "2553868" . matches ( regex) ) ;
System. out. println ( "0553868" . matches ( regex) ) ;
}
public static boolean checkQQ ( String qq) {
boolean flag = true ;
if ( qq. length ( ) >= 5 && qq. length ( ) <= 15 ) {
if ( ! qq. startsWith ( "0" ) ) {
char [ ] arr = qq. toCharArray ( ) ;
for ( int i = 0 ; i < arr. length; i++ ) {
char ch = arr[ i] ;
if ( ! ( ch >= '0' && ch <= '9' ) ) {
flag = false ;
break ;
}
}
} else {
flag = false ;
}
} else {
flag = false ;
}
return flag;
}
}
常见对象字符类
package haha;
public class Normal1 {
public static void main ( String[ ] args) {
String regex= "[a-z&&[def]]" ;
System. out. println ( "a" . matches ( regex) ) ;
}
private static void demo4 ( ) {
String regex= "[a-d[m-p]]" ;
}
private static void demo3 ( ) {
String regex= "[a-zA-Z]" ;
}
private static void demo2 ( ) {
String regex= "[^abc]" ;
System. out. println ( "10" . matches ( regex) ) ;
System. out. println ( "1" . matches ( regex) ) ;
}
private static void demo1 ( ) {
String regex= "[abc]" ;
System. out. println ( "a" . matches ( regex) ) ;
}
}
常见对象预定义字符类
package haha;
public class Normal1 {
public static void main ( String[ ] args) {
}
private static void demo2 ( ) {
String regex= "\\d" ;
String regex2= "\\D" ;
String regex3= "\\s" ;
String regex4= "\\S" ;
String regex5= "\\w" ;
String regex6= "\\W" ;
}
private static void demo1 ( ) {
String regex= "." ;
System. out. println ( "a" . matches ( regex) ) ;
System. out. println ( "ab" . matches ( regex) ) ;
}
}
常见对象数量词
package haha;
public class Normal1 {
public static void main ( String[ ] args) {
}
private static void demo4 ( ) {
String regex= "[abc]{5}" ;
System. out. println ( "abcba" . matches ( regex) ) ;
System. out. println ( "abcbaabcba" . matches ( regex) ) ;
String regex2= "[abc]{5,}" ;
String regex3= "[abc]{5,15}" ;
}
private static void demo3 ( ) {
String regex= "[abc]+" ;
System. out. println ( "" . matches ( regex) ) ;
System. out. println ( "a" . matches ( regex) ) ;
System. out. println ( "aaaaaaaa" . matches ( regex) ) ;
}
private static void demo2 ( ) {
String regex= "[abc]*" ;
System. out. println ( "" . matches ( regex) ) ;
System. out. println ( "abc" . matches ( regex) ) ;
System. out. println ( "a" . matches ( regex) ) ;
}
private static void demo1 ( ) {
String regex= "[abc]?" ;
System. out. println ( "a" . matches ( regex) ) ;
System. out. println ( "d" . matches ( regex) ) ;
System. out. println ( "" . matches ( regex) ) ;
System. out. println ( " " . matches ( regex) ) ;
}
}
正则表达式的分割功能
package haha;
public class Normal1 {
public static void main ( String[ ] args) {
String s= "吴亦凡 蔡徐坤 李dayone" ;
String[ ] arr = s. split ( " " ) ;
for ( int i = 0 ; i < arr. length; i++ ) {
System. out. println ( arr[ i] ) ;
}
String s2= "吴亦凡.蔡徐坤.李dayone" ;
String[ ] arr2 = s2. split ( "\\." ) ;
for ( int i = 0 ; i < arr2. length; i++ ) {
System. out. println ( arr2[ i] ) ;
}
}
}
把给定字符串中的数排序
package haha;
import java. util. Arrays;
public class Normal1 {
public static void main ( String[ ] args) {
String s= "91 27 46 38 50" ;
String[ ] sArr= s. split ( " " ) ;
int [ ] arr= new int [ sArr. length] ;
for ( int i = 0 ; i < arr. length; i++ ) {
arr[ i] = Integer. parseInt ( sArr[ i] ) ;
}
Arrays. sort ( arr) ;
StringBuilder sb = new StringBuilder ( ) ;
for ( int i = 0 ; i < arr. length; i++ ) {
if ( i == arr. length - 1 ) {
sb. append ( arr[ i] ) ;
} else {
sb. append ( arr[ i] + " " ) ;
}
}
System. out. println ( sb) ;
}
}
正则表达式替换功能
package haha;
import java. util. Arrays;
public class Normal1 {
public static void main ( String[ ] args) {
String s= "wo1ai2heima" ;
String regex= "\\d" ;
String s2= s. replaceAll ( regex, "" ) ;
System. out. println ( s2) ;
}
}
正则表达式分组功能
package haha;
import java. util. Arrays;
public class Normal1 {
public static void main ( String[ ] args) {
String s= "我我....我...我.要...要要...要学....学学..学.编..编编.编.程.程.程..程" ;
String s2= s. replaceAll ( "\\.+" , "" ) ;
System. out. println ( s2) ;
String s3= s2. replaceAll ( "(.)\\1+" , "$1" ) ;
System. out. println ( s3) ;
}
private static void demo2 ( ) {
String s= "sdqqfgkkkhjppppkl" ;
String regex= "(.)\\1+" ;
String[ ] arr= s. split ( regex) ;
for ( int i = 0 ; i < arr. length; i++ ) {
System. out. println ( arr[ i] ) ;
}
}
private static void demo1 ( ) {
String regex= "(.)\\1(.)\\2" ;
System. out. println ( "快快乐乐" . matches ( regex) ) ;
System. out. println ( "快乐乐乐" . matches ( regex) ) ;
System. out. println ( "高高兴兴" . matches ( regex) ) ;
String regex2= "(..)\\1" ;
System. out. println ( "死啦死啦" . matches ( regex2) ) ;
}
}
Parttern Matcher
package haha;
import java. util. Arrays;
import java. util. regex. Matcher;
import java. util. regex. Pattern;
public class Normal1 {
public static void main ( String[ ] args) {
String s = "我的手机是18511866260,我曾用过18987654321,还用过18812345678" ;
String regex = "1[3578]\\d{9}" ;
Pattern p = Pattern. compile ( regex) ;
Matcher m = p. matcher ( s) ;
while ( m. find ( ) )
System. out. println ( m. group ( ) ) ;
}
private static void demo1 ( ) {
Pattern p = Pattern. compile ( "a*b" ) ;
Matcher m = p. matcher ( "aaaaab" ) ;
boolean b = m. matches ( ) ;
System. out. println ( "aaaaab" . matches ( "a*b" ) ) ;
}
}
Math类
B:成员方法
public static int abs(int a) public static double ceil(double a) public static double floor(double a) public static int max(int a,int b) min自学 public static double pow(double a,double b) public static double random() public static int round(float a) 参数为double的自学 public static double sqrt(double a)
package haha;
import java. util. Arrays;
import java. util. regex. Matcher;
import java. util. regex. Pattern;
public class Normal1 {
public static void main ( String[ ] args) {
System. out. println ( Math. PI) ;
System. out. println ( Math. abs ( - 10 ) ) ;
System. out. println ( Math. ceil ( 12.3 ) ) ;
System. out. println ( Math. ceil ( 12.99 ) ) ;
System. out. println ( Math. floor ( 12.3 ) ) ;
System. out. println ( Math. floor ( 12.99 ) ) ;
System. out. println ( Math. max ( 20 , 30 ) ) ;
System. out. println ( Math. pow ( 2 , 3 ) ) ;
System. out. println ( Math. random ( ) ) ;
System. out. println ( Math. round ( 12.3f ) ) ;
System. out. println ( Math. round ( 12.9f ) ) ;
System. out. println ( Math. sqrt ( 4 ) ) ;
System. out. println ( Math. sqrt ( 2 ) ) ;
}
}
Random类
B:构造方法
public Random() public Random(long seed) C:成员方法
public int nextInt() public int nextInt(int n)(重点掌握)
Random r3= new Random ( ) ;
for ( int i = 0 ; i < 10 ; i++ ) {
System. out. println ( r3. nextInt ( 100 ) ) ;
}
}
private static void demo2 ( ) {
Random r2= new Random ( 1000 ) ;
int a= r2. nextInt ( ) ;
int b= r2. nextInt ( ) ;
System. out. println ( a) ;
System. out. println ( b) ;
}
private static void demo1 ( ) {
Random r= new Random ( ) ;
for ( int i = 0 ; i < 10 ; i++ ) {
System. out. println ( r. nextInt ( ) ) ;
}
System类
B:成员方法
public static void gc() public static void exit(int status) public static long currentTimeMillis() pubiic static void arraycopy(Object src, int srcPos, Object dest, int destPos, int length)
package haha;
import java. util. Arrays;
import java. util. Iterator;
import java. util. Random;
import java. util. regex. Matcher;
import java. util. regex. Pattern;
public class Normal1 {
public static void main ( String[ ] args) {
int [ ] src= { 11 , 22 , 33 , 44 , 55 } ;
int [ ] dest= new int [ 8 ] ;
for ( int i = 0 ; i < dest. length; i++ ) {
System. out. print ( dest[ i] + " " ) ;
}
System. arraycopy ( src, 0 , dest, 0 , src. length) ;
for ( int i = 0 ; i < dest. length; i++ ) {
System. out. print ( dest[ i] + " " ) ;
}
}
private static void demo3 ( ) {
long start= System. currentTimeMillis ( ) ;
for ( int i= 0 ; i< 1000 ; i++ )
{
System. out. println ( "*" ) ;
}
long end = System. currentTimeMillis ( ) ;
System. out. println ( end- start) ;
}
private static void demo2 ( ) {
System. exit ( 0 ) ;
System. out. println ( "1111111" ) ;
}
private static void demo1 ( ) {
for ( int i= 0 ; i< 100 ; i++ )
{
new Demo ( ) ;
System. gc ( ) ;
}
}
}
class Demo
{
@Override
public void finalize ( ) {
System. out. println ( "垃圾被清扫了" ) ;
}
}
BigInteger类
超过integer的数进行运算 B:构造方法
public BigInteger(String val) C:成员方法
public BigInteger add(BigInteger val) public BigInteger subtract(BigInteger val) public BigInteger multiply(BigInteger val) public BigInteger divide(BigInteger val) public BigInteger[] divideAndRemainder(BigInteger val)
public class Normal1 {
public static void main ( String[ ] args) {
BigInteger bi1= new BigInteger ( "100" ) ;
BigInteger bi2= new BigInteger ( "2" ) ;
System. out. println ( bi1. add ( bi2) ) ;
System. out. println ( bi1. subtract ( bi2) ) ;
System. out. println ( bi1. multiply ( bi2) ) ;
System. out. println ( bi1. divide ( bi2) ) ;
BigInteger[ ] arr= bi1. divideAndRemainder ( bi2) ;
for ( int i = 0 ; i< arr. length; i++ ) {
System. out. println ( arr[ i] ) ;
}
}
}
BigDemical类
A:BigDecimal的概述
由于在运算的时候,float类型和double很容易丢失精度,演示案例。 所以,为了能精确的表示、计算浮点数,Java提供了BigDecimal 不可变的、任意精度的有符号十进制数。 B:构造方法
public BigDecimal(String val) C:成员方法
public BigDecimal add(BigDecimal augend) public BigDecimal subtract(BigDecimal subtrahend) public BigDecimal multiply(BigDecimal multiplicand) public BigDecimal divide(BigDecimal divisor)
public class Normal1 {
public static void main ( String[ ] args) {
BigDecimal bd1= BigDecimal. valueOf ( 2.0 ) ;
BigDecimal bd2= BigDecimal. valueOf ( 1.1 ) ;
System. out. println ( bd1. subtract ( bd2) ) ;
}
}
Date类
A:Date类的概述
B:构造方法
public Date() public Date(long date) C:成员方法
public long getTime() public void setTime(long time)
public class Normal1 {
public static void main ( String[ ] args) {
Date d1= new Date ( ) ;
System. out. println ( d1) ;
Date d2= new Date ( 0 ) ;
System. out. println ( d2) ;
Date d3= new Date ( ) ;
System. out. println ( d3. getTime ( ) ) ;
System. out. println ( System. currentTimeMillis ( ) ) ;
Date d4= new Date ( ) ;
d3. setTime ( 1000 ) ;
System. out. println ( d4) ;
}
}
SimpleDateFormat类
A:DateFormat类的概述
DateFormat 是日期/时间格式化子类的抽象类,它以与语言无关的方式格式化并解析日期或时间。是抽象类,所以使用其子类SimpleDateFormat B:SimpleDateFormat构造方法
public SimpleDateFormat() public SimpleDateFormat(String pattern) C:成员方法
public final String format(Date date) public Date parse(String source)
public class Normal1 {
public static void main ( String[ ] args) throws ParseException {
Date d= new Date ( ) ;
SimpleDateFormat sdf= new SimpleDateFormat ( ) ;
System. out. println ( sdf. format ( d) ) ;
SimpleDateFormat sdf2= new SimpleDateFormat ( "yyyy年MM月dd日HH:mm:ss" ) ; ;
System. out. println ( sdf2. format ( d) ) ;
String str= "2000年08月08日 08:08:08" ;
SimpleDateFormat sdf3 = new SimpleDateFormat ( "yyyy年MM月dd日HH:mm:ss" ) ;
Date d1= sdf3. parse ( str) ;
System. out. println ( d1) ;
}
private static void demo1 ( ) {
DateFormat df1= DateFormat. getDateInstance ( ) ;
}
}
你来到世界上多少天
public class Normal1 {
public static void main ( String[ ] args) throws ParseException {
String birthday= "1996年10月07日" ;
String today= "2019年03月17日" ;
SimpleDateFormat sdf= new SimpleDateFormat ( "yyyy年MM月dd日" ) ;
Date d1= sdf. parse ( birthday) ;
Date d2= sdf. parse ( today) ;
long time= d2. getTime ( ) - d1. getTime ( ) ;
System. out. println ( time / 1000 / 60 / 60 / 24 ) ;
}
}
Calendar类
A:Calendar类的概述
Calendar 类是一个抽象类,它为特定瞬间与一组诸如 YEAR、MONTH、DAY_OF_MONTH、HOUR 等日历字段之间的转换提供了一些方法,并为操作日历字段(例如获得下星期的日期)提供了一些方法。 B:成员方法
public static Calendar getInstance() public int get(int field)
public class Normal1 {
public static void main ( String[ ] args) throws ParseException {
Calendar c= Calendar. getInstance ( ) ;
System. out. println ( c. get ( Calendar. YEAR) ) ;
System. out. println ( c. get ( Calendar. MONTH) ) ;
System. out. println ( c. get ( Calendar. DAY_OF_MONTH) ) ;
System. out. println ( c. get ( Calendar. DAY_OF_WEEK) ) ;
c. set ( 2000 , 7 , 8 ) ;
System. out. println ( c. get ( Calendar. YEAR) + "年" + getNum ( ( c. get ( Calendar. MONTH) + 1 ) )
+ "月" + getNum ( c. get ( Calendar. DAY_OF_MONTH) ) + "日" + getWeek ( c. get ( Calendar. DAY_OF_WEEK) ) ) ;
}
public static String getWeek ( int week)
{
String[ ] arr= { "" , "星期日" , "星期一" , "星期二" , "星期三" , "星期四" , "星期五" , "星期六" } ;
return arr[ week] ;
}
public static String getNum ( int num) {
return num > 9 ? "" + num : "0" + num;
}
}
A:成员方法
public void add(int field,int amount) public final void set(int year,int month,int date)
判断平年瑞年
public class Normal1 {
public static void main ( String[ ] args) {
Scanner sc = new Scanner ( System. in) ;
System. out. println ( "请输入年份,判断该年份是闰年还是平年:" ) ;
String line = sc. nextLine ( ) ;
int year = Integer. parseInt ( line) ;
boolean b = getYear ( year) ;
System. out. println ( b) ;
}
private static boolean getYear ( int year) {
Calendar c = Calendar. getInstance ( ) ;
c. set ( year, 2 , 1 ) ;
c. add ( Calendar. DAY_OF_MONTH, - 1 ) ;
return c. get ( Calendar. DAY_OF_MONTH) == 29 ;
}
}