日期时间
1.Date
public class test {
public static void main(String[] args) {
//1.日期对象
Date d=new Date();
System.out.println(d);//Mon Jan 23 13:19:45 CST 2023
//2.时间毫秒值,从1970年1月1日00:00:00到现在的毫秒数
long time=d.getTime();
//long t1=System.currentTimeMillis();
System.out.println(time);//1674451185094
}
}
案例:从当前时间走1小时121秒后的时间
public class test {
public static void main(String[] args) {
//当前时间毫秒值
long t1=System.currentTimeMillis();
t1+=(60*60+121)*1000;
//将毫秒值转化为对应日期对象
//法一,Date d2=new Date(t1);
Date d2=new Date(t1);
System.out.println("计算时间:"+d2);
//法二, d3.setTime(t1);
Date d3=new Date();
d3.setTime(t1);
System.out.println("计算时间:"+d3);
}
}
/**
*当前时间:Mon Jan 23 13:47:46 CST 2023
*计算时间:Mon Jan 23 14:49:47 CST 2023
*/
2.SimpleDateFormat
SimpleDateFormat类作用
public class test {
public static void main(String[] args) {
//1.创建一个日期
Date d1=new Date();
System.out.println(d1);
//2.转换为所需要的日期形式 EEE星期 a上下午
SimpleDateFormat sdf=new SimpleDateFormat("yyy年MM月dd日 HH:mm:ss EEE a");
//3.格式化日期对象,转化为喜欢的字符串形式
String t=sdf.format(d1);
System.out.println(t);
/**
* Tue Jan 31 16:07:25 CST 2023
* 2023年01月31日 16:07:25 星期二 下午
*/
}
}
public class test2 {
public static void main(String[] args) throws ParseException {
/**
* 计算2021年08月06日 11点11分11秒,往后走2天14小时49分06秒后的时间是多少
*/
//1.把字符串时间拿到程序中来
String d="2021年08月06日 11:11:11";
//2.把字符串时间解析成日期对象
SimpleDateFormat sdf=new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
Date d1=sdf.parse(d);
//3.往后走2天14小时49分06秒
long time=d1.getTime()+(2L*24*60*60 + 14*60*60 + 49*60 + 6)*1000;//2L怕数值过大越界int
//4.格式化时间毫秒值,就是结果
System.out.println(sdf.format(time));
}
}
public class test3 {
public static void main(String[] args) throws ParseException {
//1.开始 结束时间
String startTime="2020-11-11 00:00:00";
String endTime="2020-11-11 00:10:00";
String jia="2020-11-11 00:03:47";
String pi="2020-11-11 00:10:11";
//2.解析时间
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date d1=sdf.parse(startTime);
Date d2=sdf.parse(endTime);
Date d3=sdf.parse(jia);
Date d4=sdf.parse(pi);
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("小皮秒杀失败");
}
}
}
3.Calender
public class test {
public static void main(String[] args) {
//1.拿到系统中的日历对象
Calendar cal=Calendar.getInstance();
System.out.println(cal);
//2.获取日历信息,获取日期中的某个字段信息
int year=cal.get(Calendar.YEAR);
System.out.println(year);
int month=cal.get(Calendar.MONTH)+1;//从0开始计数
System.out.println(month);
int days=cal.get(Calendar.DAY_OF_YEAR);
System.out.println(days);
//3.修改日历中某个字段信息
/* cal.set(Calendar.HOUR,12);
System.out.println(cal);*/
//4.为某个字段加减指定值
//例:64天后是什么时间?
cal.add(Calendar.DAY_OF_YEAR,64);
//5.获取此刻日期对象
Date d1=cal.getTime();
System.out.println(d1);
//6.拿到此刻时间毫秒值
long time=cal.getTimeInMillis();
System.out.println(time);
}
}
JDK8新增日期类
1.日期类
public class Demo01_LocalDate {
public static void main(String[] args) {
//1.获取本地日期对象(年、月、日)
LocalDate nowDate=LocalDate.now();
System.out.println("今日日期:"+nowDate); //今日日期:2023-01-31
int year=nowDate.getYear();
int month=nowDate.getMonthValue();
int day=nowDate.getDayOfMonth();
System.out.println("年:"+year);
System.out.println("月:"+month);
System.out.println("日:"+day);
//当年的第几天
int dayOfYear=nowDate.getDayOfYear();
System.out.println("dayOfYear:"+dayOfYear);
//星期
System.out.println(nowDate.getDayOfWeek()); //TUESDAY
System.out.println(nowDate.getDayOfWeek().getValue()); //2
//月份
System.out.println(nowDate.getMonth()); //JANUARY
System.out.println(nowDate.getMonth().getValue()); //1
//自定义日期
LocalDate birthday=LocalDate.of(1990,1,21);
System.out.println(birthday); //1990-01-21
System.out.println(LocalDate.of(1990, Month.JANUARY,21)); //1990-01-21
}
}
public class Demo2_LocalTime {
public static void main(String[] args) {
//1.获取本地时间对象。
LocalTime nowTime=LocalTime.now();
System.out.println("当前时间:"+nowTime); //当前时间:19:00:25.950
int hour= nowTime.getHour();
int minute=nowTime.getMinute();
int second=nowTime.getSecond();
int nano= nowTime.getNano();
System.out.println("时"+hour);
System.out.println("分"+minute);
System.out.println("秒"+second);
System.out.println("纳秒"+nano);
System.out.println(LocalTime.of(1,20));//01:20
System.out.println(LocalTime.of(1,20,30));//01:20:30
System.out.println(LocalTime.of(1,20,30,150));//01:20:30.000000150
LocalTime t2=LocalTime.of(1,20,30,150);
//LocalDataTime
System.out.println(LocalDateTime.of(1990,1,20,8,20,30,150));//1990-01-20T08:20:30.000000150
}
}
public class Demo03_UpdateTime {
public static void main(String[] args) {
LocalTime nowTime=LocalTime.now();
System.out.println(nowTime);
System.out.println(nowTime.minusHours(1));//一小时前
System.out.println(nowTime.minusMinutes(1));//一分钟前
System.out.println(nowTime.minusSeconds(1));//一秒前
System.out.println(nowTime.minusNanos(1));//一纳秒前
System.out.println(nowTime.plusHours(1));//一小时后 20:23:35.950
System.out.println(nowTime.plusMinutes(1));//一分钟后 19:24:35.950
System.out.println(nowTime.plusSeconds(1));//一秒后 19:23:36.950
System.out.println(nowTime.plusNanos(1));//一纳秒后 19:23:35.950000001
//不可变对象,每次修改产生的是新对象,nowTime没有变
System.out.println(nowTime);//19:23:35.950
System.out.println("------------");
LocalDate myDate=LocalDate.of(2023,1,20);
LocalDate nowDate=LocalDate.now();
System.out.println("今天是2023-01-20吗?"+nowDate.equals(myDate));
System.out.println(myDate+"是否在"+nowDate+"之前?"+myDate.isBefore(nowDate));
//2023-01-20是否在2023-01-31之前?true
System.out.println(myDate+"是否在"+nowDate+"之后?"+myDate.isAfter(nowDate));
//2023-01-20是否在2023-01-31之后?false
//判断今天是你的生日吗,只要月、日
System.out.println("---------------------");
LocalDate birthday=LocalDate.of(1990,1,21);
LocalDate nowDate2=LocalDate.now();
MonthDay birMd=MonthDay.of(birthday.getMonthValue(),birthday.getDayOfMonth());
System.out.println(birMd);
MonthDay nowMd=MonthDay.from(nowDate2);
System.out.println(nowMd);
System.out.println("今天是否是你的生日?"+birMd.equals(nowMd));
}
}
2.Instant时间戳
public class Demo04_Instant {
public static void main(String[] args) {
//得到一个时间戳对象
Instant instant=Instant.now();
System.out.println(instant);//2023-01-31T11:48:54.162Z (世界标准时钟)
//2.得到系统此刻时间戳
System.out.println(instant.atZone(ZoneId.systemDefault()));
//2023-01-31T19:52:04.403+08:00[Asia/Shanghai]
//3.返回Date对象
Date nowDate=Date.from(instant);
System.out.println(nowDate);//Tue Jan 31 19:54:33 CST 2023
Instant i2=nowDate.toInstant();
System.out.println(i2);//2023-01-31T11:55:49.598Z
}
}
3.DateTimeFormatter
public class Demo05_DataTimeFormat {
public static void main(String[] args) {
//本地日期 时间对象
LocalDateTime ldt=LocalDateTime.now();
System.out.println(ldt);//2023-01-31T20:06:02.821
//解析/格式化器
DateTimeFormatter dtf=DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss EEE a");
//正向格式化 日期对象转化为所需的字符串格式
String ldts1=dtf.format(ldt);
System.out.println(ldts1);//2023-01-31 20:06:02 星期二 下午
//逆向格式化
String ldts2=ldt.format(dtf);
System.out.println(ldts2);//2023-01-31 20:06:02 星期二 下午
//解析字符串时间
DateTimeFormatter dtf2=DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String dataStr="2020-01-20 08:20:30";
//解析当前字符串为本地时间对象
LocalDateTime ldt1=LocalDateTime.parse(dataStr,dtf2);
System.out.println(ldt1);//2020-01-20T08:20:30
}
}
4.Duration/Period
//Period年月是之间的间隔
public class Demo06_Period {
public static void main(String[] args) {
//当前本地 年月日
LocalDate nowDate=LocalDate.now();
System.out.println(nowDate);//2023-01-31
//生日 年月日
LocalDate birthday=LocalDate.of(1990,1,21);
System.out.println(birthday);//1990-01-21
Period period=Period.between(birthday,nowDate);//第二个参数 减 第一个参数
System.out.println(period.getYears());
System.out.println(period.getMonths());
System.out.println(period.getDays());
}
}
//Duration时分秒之间的间隔
public class Demo07_Duration {
public static void main(String[] args) {
//本地日期对象
LocalDateTime today=LocalDateTime.now();
System.out.println(today);
//今年的生日日期对象
LocalDateTime birthday=LocalDateTime.of(2023,10,21,15,20,15);
System.out.println(birthday);
Duration duration=Duration.between(today,birthday);//第二个参数 减 第一个参数
System.out.println(duration.toDays());//两天相差天数
System.out.println(duration.toHours());//两天相差小时数
System.out.println(duration.toMinutes());//两天相差分钟
System.out.println(duration.toMillis());//两天相差毫秒数
System.out.println(duration.toNanos());//两天相差纳秒
}
}
5.ChronoUnit
public class Demo08_ChronoUnit {
public static void main(String[] args) {
//本地此刻时间对象
LocalDateTime today=LocalDateTime.now();
System.out.println(today);
//生日时间
LocalDateTime birthday=LocalDateTime.of(1990,1,21,14,30,50);
System.out.println(birthday);
System.out.println("相差年数:"+ ChronoUnit.YEARS.between(birthday,today));
System.out.println("相差月数:"+ ChronoUnit.MONTHS.between(birthday,today));
System.out.println("相差天数:"+ ChronoUnit.DAYS.between(birthday,today));
System.out.println("相差小时数:"+ ChronoUnit.HOURS.between(birthday,today));
System.out.println("相差分钟数:"+ ChronoUnit.MINUTES.between(birthday,today));
System.out.println("相差秒数:"+ ChronoUnit.SECONDS.between(birthday,today));
System.out.println("相差毫秒数:"+ ChronoUnit.MICROS.between(birthday,today));
System.out.println("相差微秒数:"+ ChronoUnit.MILLIS.between(birthday,today));
System.out.println("相差纳秒数:"+ ChronoUnit.NANOS.between(birthday,today));
System.out.println("相差半天数:"+ ChronoUnit.HALF_DAYS.between(birthday,today));
System.out.println("相差十年数:"+ ChronoUnit.DECADES.between(birthday,today));
System.out.println("相差世纪数:"+ ChronoUnit.CENTURIES.between(birthday,today));
System.out.println("相差千年数:"+ ChronoUnit.MILLENNIA.between(birthday,today));
}
}
包装类
- 八种基本数据类型对应的引用类
public class test {
public static void main(String[] args) {
//1.
Integer a1=null;
//2.包装类可将基本数据类型的数字转化为字符串(不常用)
Integer a2=1234;
String s=a2.toString();
String s1=Integer.toString(a2);
//直接加空字符串转化即可
String s2=a2+"";
//3.把字符串类型数值转化为真实数据类型(有用)
//转整数
String age="24";
//int age2=Integer.parseInt(age);
int age2=Integer.valueOf(age);
//转小数
String score="90.5";
//double score1=Double.parseDouble(score);
double score1=Double.valueOf(score);
}
}
正则表达式
public class RegexDemo1 {
public static void main(String[] args) {
String qq1="1234568";
String qq2="a123456";
String qq3="";
System.out.println(checkQQ(qq1));
System.out.println(checkQQ(qq2));
System.out.println(checkQQ(qq3));
}
public static boolean checkQQ(String num){
return num!=null && num.matches("\\d{6,20}");
}
}
匹配规则
phoneNum.matches("1[3-9]\\d{9}"
emailNum.matches("\\w{1,30}@[a-zA-Z0-9]{2,20}(\\.[a-zA-Z0-9]{2,20}){1,2}")
telNum.matches("0\\d{2,6}-?\\d{5,20}")
字符串中的使用
public class RegexDemo3 {
public static void main(String[] args) {
//1.分割
String s1="张三123an李四";
String[] arrs=s1.split("\\w+");
for (int i = 0; i < arrs.length; i++) {
System.out.println(arrs[i]);
}
/**
* 张三
* 李四
*/
//2.替换
String s2=s1.replaceAll("\\w+"," ");
System.out.println(s2); //张三 李四
}
}
爬取信息匹配
Arrays类
常用API
比较器
public class ArraysDemo2 {
public static void main(String[] args) {
//1.降序排序(自定义比较器对象,只支持引用类型的排序)
Integer[] ages={10,24,15,30,23};
/**
* 参数一:被排序的数组,必须是引用类型的元素
* 参数二:匿名内部类对象,代表一个比较器对象
*/
Arrays.sort(ages, new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
//return o1-o2;//升序
return o2-o1;//降序
}
});
System.out.println(Arrays.toString(ages));//[30, 24, 23, 15, 10]
System.out.println("------------------");
Student[] students=new Student[3];
students[0]=new Student("张三",20,165.5);
students[1]=new Student("李四",19,185.3);
students[2]=new Student("王五",24,175.2);
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();//按年龄升序
return Double.compare(o1.getHeight(),o2.getHeight());//身高不是整数,调用Double.compare(),
//降序:互换参数位置
}
});
System.out.println(Arrays.toString(students));
//[Student{name='李四', age=19, height=185.0}, Student{name='张三', age=20, height=165.0}, Student{name='王五', age=24, height=175.0}]
}
}
常用的查找、排序算法
选择排序
public class test1 {
public static void main(String[] args) {
int[] arr={5,4,1,3};
//选择排序
for (int i = 0; i < arr.length-1; 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));//[1, 3, 4, 5]
}
}
二分查找
public class test2 {
public static void main(String[] args) {
int[] arr={10,20,30,40,50,60,70,80,90};
//二分查找
System.out.println(binarySearch(arr, 81));
}
public static int binarySearch(int[] arr,int data){
int left=0;
int right=arr.length-1;
while(left<=right){
int middleIndex=(left+right)/2;
if(arr[middleIndex]<data){
left=middleIndex+1;
}
else if(arr[middleIndex]>data){
right=middleIndex-1;
}else{
return middleIndex;
}
}
return -1;
}
}
Lambda表达式
作用:简化匿名内部类的代码
【注意】:只能简化函数接口的匿名内部类的写法形式
函数接口:1.是接口 2.仅有一个抽象方法
public class test {
public static void main(String[] args) {
//Lambda只能简化接口中只有一个抽象方法的匿名内部类形式
/* swim s1=new swim() {
@Override
public void swim() {
System.out.println("学生参加游泳比赛");
}
};*/
//简化后
swim s1=()->{
System.out.println("学生参加游泳比赛");
};
go(s1);
/* go(new swim() {
@Override
public void swim() {
System.out.println("老师参加比赛");
}
});*/
//简化后
go(()->{
System.out.println("老师参加比赛");
});
//再次简化
go(()-> System.out.println("老师参加比赛"));
}
public static void go(swim s){
System.out.println("开始--------");
s.swim();
System.out.println("结束--------");
}
}
@FunctionalInterface//一旦加上,必须是函数式接口
interface swim{
void swim();
}
public class test2 {
public static void main(String[] args) {
Integer[] arr={10,30,60,50};
Arrays.sort(arr, new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
return o1-o2;
}
});
//简化后
Arrays.sort(arr, (Integer o1, Integer o2)-> {
return o1-o2;
});
}
}
public class test2 {
public static void main(String[] args) {
Integer[] arr={10,30,60,50};
Arrays.sort(arr, new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
return o1-o2;
}
});
//简化后
Arrays.sort(arr, (Integer o1, Integer o2)-> {
return o1-o2;
});
//再次简化后
Arrays.sort(arr, (o1, o2)->o1-o2);
}
}
标签:String,Day8,int,System,mt,API,println,public,out
From: https://www.cnblogs.com/mt-nika/p/17089453.html