目录
- 日期与时间
- Date
- SimpleDateFormat
- Calendar概述
- JDK8新增日期类
- 概述、LocalTime /LocalDate / LocalDateTime
- Instant
- DateTimeFormatter
- Duration/Period
- chronoUnit
- 包装类
- 正则表达式
- Arrays类
- Arrays类概述,常用功能演示
- Arrays类对于Comparator比较器的支持
- 常见算法
- 选择排序
- 二分查找
- Lambda表达式
- Lambda概述
- Lambda实战-简化函数式接口
- Lambda表达式的省略规则
日期与时间
Date
package com.itzixin.d1_date;
import java.util.Date;
public class DateDemo1 {
public static void main(String[] args) {
Date d =new Date();
System.out.println(d);
long time = d.getTime();
System.out.println(time);
System.out.println("------------");
Date d1 = new Date();
System.out.println(d1);
long time2 = System.currentTimeMillis();
time2+=(60*60+121)*1000;
Date d2 = new Date(time2);
System.out.println(d2);
Date d3 = new Date();
d3.setTime(time2);
System.out.println(d3);
}
}
SimpleDateFormat
package com.itzixin.d2_simpledateformat;
import java.text.SimpleDateFormat;
import java.util.Date;
public class SimpleDateFormatDemo01 {
public static void main(String[] args) {
Date d =new Date();
System.out.println(d);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss EEE a");
String rs = sdf.format(d);
System.out.println(rs);
System.out.println("--------------");
long time1 = System.currentTimeMillis()+121*1000;
String rs2 = sdf.format(time1);
System.out.println(rs2);
System.out.println("-----------");
}
}
package com.itzixin.d2_simpledateformat;
import java.sql.SQLOutput;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class SimpleDateFormatDemo2 {
public static void main(String[] args) throws ParseException {
String dateStr = "2021年08月06日 11:11:11";
SimpleDateFormat sdf =new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
Date d =sdf.parse(dateStr);
long time = d.getTime()+(2L*24*60*60+ 14*60*60 + 49*60+ 6)*6000;
System.out.println(sdf.format(time));
}
}
package com.itzixin.d2_simpledateformat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class SimpleDateFormatTest3 {
public static void main(String[] args) throws ParseException {
String startTime = "2021-11-11 00:00:00";
String endTime = "2021-11-11 00:10:00";
String xiaoJia = "2021-11-11 00:03:47";
String xiaoPi = "2021-11-11 00:10:11";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date d1 = sdf.parse(startTime);
Date d2 = sdf.parse(endTime);
Date d3 = sdf.parse(xiaoJia);
Date d4 = sdf.parse(xiaoPi);
if(d3.after(d1)&&d3.before(d2)){
System.out.println("小佳秒杀成功,可以发货了");
}else{
System.out.println("小佳秒杀失败了");
}
if(d4.after(d1)&&d4.before(d2)){
System.out.println("小皮秒杀成功,可以发货了");
}else{
System.out.println("小皮秒杀失败!");
}
}
}
Calendar概述
package com.itzixin.d2_simpledateformat;
import java.util.Calendar;
import java.util.Date;
public class d3_calendar {
public static void main(String[] args) {
Calendar cal = Calendar.getInstance();
System.out.println(cal);
int year = cal.get(Calendar.YEAR);
System.out.println(year);
int mm = cal.get(Calendar.MONTH)+1;
System.out.println(mm);
int days = cal.get(Calendar.DAY_OF_YEAR);
System.out.println(days);
cal.add(Calendar.DAY_OF_YEAR,66);
cal.add(Calendar.MINUTE,59);
Date d = cal.getTime();
System.out.println(d);
long time = cal.getTimeInMillis();
System.out.println(time);
}
}
JDK8新增日期类
概述、LocalTime /LocalDate / LocalDateTime
Instant
DateTimeFormatter
Duration/Period
chronoUnit
包装类
正则表达式
package com.itzixin.d6_regex;
public class RegexDemo1 {
public static void main(String[] args) {
System.out.println(checkQQ("ofisjo33"));
System.out.println(checkQQ("345534"));
System.out.println(checkQQ(null));
System.out.println("------------");
System.out.println(checkQQ2("ofisjo33"));
System.out.println(checkQQ2("345534"));
System.out.println(checkQQ2(null));
}
public static boolean checkQQ2(String qq){
return qq!=null && qq.matches("\\d{6,20}");
}
public static boolean checkQQ(String qq){
if(qq==null || qq.length()<6||qq.length()>20){
return false;
}
for(int i =0;i<qq.length();i++){
char ch = qq.charAt(i);
if(ch<'0'|| ch>'9'){
return false;
}
}
return true;
}
}
package com.itzixin.d6_regex;
import java.util.Scanner;
public class RegexTest3 {
public static void main(String[] args) {
checkPhone();
}
private static void checkPhone() {
Scanner sc = new Scanner(System.in);
while(true){
System.out.println("请您输入您的注册手机号码:");
String phone = sc.next();
if(phone.matches("1[3-9]\\d{9}")){
System.out.println("手机号码格式正确,注册完成!");
break;
}else{
System.out.println("格式有误!");
}
}
}
}
package com.itzixin.d6_regex;
public class RegexDemo4 {
public static void main(String[] args) {
String names = "小路gjojg蓉儿jjioaa过儿";
String [] arrs = names.split("\\w+");
for(int i = 0;i<arrs.length;i++){
System.out.println(arrs[i]);
}
String names2 = names.replaceAll("\\w+"," ");
System.out.println(names2);
}
}
Arrays类
Arrays类概述,常用功能演示
package com.itzixin.d7_arrays;
import java.util.Arrays;
public class ArraysDemo1 {
public static void main(String[] args) {
int [] arr = {10,2,55,22,24,200};
System.out.println(arr);
System.out.println(Arrays.toString(arr));
Arrays.sort(arr);
System.out.println(Arrays.toString(arr));
int index=Arrays.binarySearch(arr,55);
System.out.println(index);
int index2 = Arrays.binarySearch(arr,555);
System.out.println(index2);
int[] arr2={12,36,34,25,234}
}
}
Arrays类对于Comparator比较器的支持
package com.itzixin.d7_arrays;
import java.util.Arrays;
import java.util.Comparator;
public class ArraysDemo2 {
public static void main(String[] args) {
int [] ages = {34,12,42,23};
Arrays.sort(ages);
System.out.println(Arrays.toString(ages));
Integer[] ages1 = {34,12,42,23};
Arrays.sort(ages1, new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
// if(o1>o2){
// return 1;
// }else if(o1<o2){
// return -1;
// }
// return 0;
// return o1-o2;
return o2-o1;
}
});
System.out.println(Arrays.toString(ages1));
System.out.println("-------------");
Student[] students = new Student[3];
students[0] = new Student("吴磊",23,175);
students[1]= new Student("谢欣",22,188);
students[2]= new Student("王亮",18,222);
System.out.println(Arrays.toString(students));
Arrays.sort(students, new Comparator<Student>() {
@Override
public int compare(Student o1, Student o2) {
return o1.getAge()-o2.getAge();
}
});
System.out.println(Arrays.toString(students));
}
}
常见算法
选择排序
package com.itzixin.d8_sort_binarysearch;
import java.util.Arrays;
public class Test1 {
public static void main(String[] args) {
int [] arr = {5,1,3,2};
for(int i =0;i<arr.length;i++){
for(int j =i+1;j<arr.length;j++){
if(arr[i]>arr[j]){
int temp = arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
}
System.out.println(Arrays.toString(arr));
}
}
二分查找
Lambda表达式
Lambda概述
package com.itzixin.d9_lambda;
import java.sql.SQLOutput;
public class LambdaDemo1 {
public static void main(String[] args) {
// Swimming s1 = new Swimming() {
// @Override
// public void swim() {
// System.out.println("老师游泳六六六");
// }
// }
Swimming s1 = () ->{
System.out.println("老师游泳六六六");
};
go(s1);
}
private static void go(Swimming s) {
System.out.println("开始了。。。");
s.swim();
System.out.println("结束。。。");
}
}
@FunctionalInterface
interface Swimming{
void swim();
}
Lambda实战-简化函数式接口
package com.itzixin.d9_lambda;
import java.util.Arrays;
public class LambdaDemo3 {
public static void main(String[] args) {
Integer [] ages1 = {34,12,42,23};
Arrays.sort(ages1,(Integer o1,Integer o2)->{
return o2-o1;
});
System.out.println(Arrays.toString(ages1));
}
}