【JAVA入门】Day21 - 时间类
【JAVA入门】Day21 - 时间类
文章目录
- 【JAVA入门】Day21 - 时间类
- 一、JDK7前的时间相关类
- 1.1 Date
- 1.2 SimpleDateFormat
- 1.3 Calendar
- 二、JDK8新增的时间相关类
- 2.1 Date 相关类
- 2.1.1 ZoneId 时区
- 2.1.2 Instant 时间戳
- 2.1.3 ZoneDateTime 带时区的时间
- 2.2 DateTimeFormat 相关类
- 2.2.1 DateTimeFormatter 时间的格式化和解析类
- 2.3 Calendar 相关类
- 2.3.1 LocalDate、LocalTime、LocalDateTime
- 2.4 JDK8 新增的三个工具类
Java 中的时间有一些相关类,它们都与时间的编程息息相关。
一、JDK7前的时间相关类
类名 | 作用 |
---|---|
Date | 时间类 |
SimpleDateFormat | 格式化时间 |
Calender | 日历类 |
世界上的时间,是有一个统一的计算标准的。
曾经的世界时间标准被称作 格林尼治时间 / 格林威治时间 (Greenwich Mean Time),简称GMT。GMT 的计算核心是:地球自转一天是24小时,太阳直射时为正午12点。但是由于误差太大,现在 GMT 已经被舍弃。
现在的世界时间标准是利用原子钟规定的,利用铯原子的振动频率计算出来的时间,作为世界标准时间(UTC)。
中国地处东八区,要在世界标准时间的基础上 + 8小时。
1.1 Date
Date 类是 JDK 写好的一个 Javabean 类,用来描述时间,精确到毫秒。
利用空参构造创建的对象,默认表示为系统当前时间。
利用有参构造创建的对象,表示为指定的时间。
方法 | 作用 |
---|---|
public Date() | 创建Date对象,表示系统当前时间 |
public Date(long date) | 创建Date对象,表示指定时间 |
public void setTime(long time) | 设置/修改毫秒值 |
public long getTime() | 获取时间对象的毫秒值 |
【练习】学习使用Date类。
package DateClass;import java.util.Date;
import java.util.Random;public class DateDemo2 {public static void main(String[] args) {//需求一:打印一年后的时间extracted();//需求二:比较两个Date对象哪个在前哪个在后extracted1();}private static void extracted() {//1.打印时间原点一年后的时间Date d1 = new Date(0L);//2.获取d1的毫秒值long time = d1.getTime();//3.加上一年后的毫秒值time = time + 1000L * 60 * 60 * 24 * 365;//4.把计算后的时间毫秒值,设置回d1中d1.setTime(time);//5.打印System.out.println(d1);}private static void extracted1() {Random r = new Random();//创建两个时间对象Date d1 = new Date(r.nextInt());Date d2 = new Date(r.nextInt());System.out.println(d1);System.out.println(d2);long time1 = d1.getTime();long time2 = d2.getTime();if(time1 > time2) {System.out.println("d2在前,d1在后");} else if(time2 > time1) {System.out.println("d1在前,d2在后");} else {System.out.println("两时间一样");}}}
}
1.2 SimpleDateFormat
Date 类只能以默认的格式展示,不符合人们的阅读习惯。
SimpleDateFormat 可以把日期格式化;也可以解析日期,即把字符串表示的时间编程 Date 对象。
构造方法 | 说明 |
---|---|
public SimpleDateFormat() | 构造一个SimpleDateFormat对象,使用默认格式 |
public SimpleDateFormat(String pattern) | 构造一个SimpleDateFormat对象,使用指定的格式 |
常用方法 | 说明 |
---|---|
public final String format(Date date) | 格式化(日期对象 -> 字符串) |
public Date parse(String source) | 解析(字符串 -> 日期对象) |
格式化的时间形式的常用模式对应关系如下:
【练习1】练习使用SimpleDateFormat。
package DateClass;import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;public class DateDemo3 {public static void main(String[] args) throws ParseException {//需求一:格式化时间method1();//需求二:解析字符串method2();}private static void method2() throws ParseException {//1.定义一个字符串用来表示时间String str = "2023-11-11 11:11:11";//2.利用空参构造创建SimpleDateFormat对象//其创建的格式参数要和字符串的格式完全一致SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//解析字符串,变为时间对象Date date = sdf.parse(str);//打印毫秒值System.out.println(date.getTime());}private static void method1() {//1.利用空参构造创建SimpleDateFormat对象,默认格式SimpleDateFormat sdf = new SimpleDateFormat();Date d1 = new Date(0L);String str = sdf.format(d1);System.out.println(str); //1970/1/1 上午8:00//2.利用带参构造创建SimpleDateFormat对象,指定格式SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");String str2 = sdf2.format(d1);System.out.println(str2); //1970年01月01日 08:00:00//3.格式2SimpleDateFormat sdf3 = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒 EE");String str3 = sdf3.format(d1);System.out.println(str3); //1970年01月01日 08时00分00秒 周四}
}
【练习2】把一个日期转换为另一种格式。
package DateClass;import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;public class DateDemo4 {public static void main(String[] args) throws ParseException {/*2000-11-11*/String str = "2000-11-11";//1.解析字符串为日期SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");Date date = sdf.parse(str);//2.格式化日期为年月日SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy年MM月dd日");String result = sdf1.format(date);//3.打印System.out.println(result);}
}
【练习3】判断两个同学是否秒杀成功。
package DateClass;import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;public class DateDemo5 {public static void main(String[] args) throws ParseException {/*需求:秒杀活动:2023年11月11日 0:0:0开始时间:2023年11月11日 0:10:0小贾下单付款时间:2023年11月11日 0:01:00小皮下单付款时间:2023年11月11日 0:11:00计算两个人有没有参加成功*///1.比较两个时间String startStr = "2023年11月11日 0:0:0";String endStr = "2023年11月11日 0:10:0";String orderStr1 = "2023年11月11日 0:01:0";String orderStr2 = "2023年11月11日 0:11:00";//2.解析上面的时间,得到Date对象SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");Date startDate = sdf.parse(startStr);Date endDate = sdf.parse(endStr);Date orderDate1 = sdf.parse(orderStr1);Date orderDate2 = sdf.parse(orderStr2);//3.得到所有时间的毫秒值long startTime = startDate.getTime();long endTime = endDate.getTime();long orderTime1 = orderDate1.getTime();long orderTime2 = orderDate2.getTime();//4.判断if((startTime <= orderTime1)&&(orderTime1 <= endTime)) {System.out.println("小贾同学秒杀成功了!");}else{System.out.println("小贾同学秒杀失败了...");}if((startTime <= orderTime2)&&(orderTime2 <= endTime)) {System.out.println("小皮同学秒杀成功了!");}else{System.out.println("小皮同学秒杀失败了...");}}
}
1.3 Calendar
Calendar 就是日历类,它是时间类的补充,可以单独修改、获取事件中的年,月,日。
值得注意的是,Calendar 是一个抽象类,它不能直接创建对象。
我们需要通过一个静态方法来获取当前时间的日历对象。
public static Calendar getInstance()
Calendar 中常用的方法有这些:
方法名 | 说明 |
---|---|
public final Date getTime() | 获取日期对象 |
public final setTime(Date date) | 给日历设置日期对象 |
public long getTimeInMillis() | 拿到时间的毫秒值 |
public void setTimeInMillis(long millis) | 给日历设置时间毫秒值 |
public int get(int field) | 取得日历中某个字段的信息 |
public void set(int field,int value) | 修改日历中的某个字段信息 |
public void add(int field,int amount) | 为某个字段增加/减少指定的值 |
下面通过一个练习,熟悉一下这些方法。
package DateClass;import java.util.Calendar;
import java.util.Date;public class CalenderDemo1 {public static void main(String[] args) {//1.获取日历对象//Calender是一个抽象类,不能直接new,而是通过一个静态方法获取子类对象Calendar c = Calendar.getInstance();//底层原理://会根据系统的不同时区来获取不同的日历对象//把时间中的纪元,年,月,日,时,分,秒,星期,等等放到一个数组当中//2.修改一下日历代表的时间为时间原点//获取的时间信息有细节// 月份范围:0~11,0代表一月,11代表十二月// 星期:星期日是第一天,值为1;星期六的值为7Date d = new Date(0L);c.setTime(d);//3.获取日期中某个字段信息//参数为int类型//0:纪元 1:年 2:月 3:一年中的第几周 ...//Java当中,把索引数字都定义为了常量int year = c.get(Calendar.YEAR);int month = c.get(Calendar.MONTH) + 1;int date = c.get(Calendar.DAY_OF_MONTH);int week = c.get(Calendar.DAY_OF_WEEK);System.out.println(year + ", " + month + ", "+ date+ ", " + getWeek(week));//4.修改日历中某个字段c.set(Calendar.YEAR, 2000);c.set(Calendar.MONTH,12);//月份12是指13月,这是不存在的//系统会自动把年份调成次年//5.为某个字段增加/减少值c.add(Calendar.MONTH, -1);}//传入对应的数字:1~7//返回对应的星期//查表法:在方法中让数据跟索引产生对应关系public static String getWeek(int index) {String[] arr = {"","星期日","星期一","星期二","星期三","星期四","星期五","星期六"};return arr[index];}
}
二、JDK8新增的时间相关类
JDK8 新增的时间相关类,解决了 JDK7 代码麻烦的问题,而且在安全层面下有了突破。
JDK7 在多线程环境下会导致数据安全问题产生,而 JDK8 的时间日期对象都是不可变的,解决了这个问题。
JDK8 主要新增了以下四种类:
2.1 Date 相关类
2.1.1 ZoneId 时区
时区类常用方法如下。
package DateClass;import java.time.ZoneId;
import java.util.Set;public class CalenderDemo2 {public static void main(String[] args) {/*时区*///1.获取所有的时区名称Set<String> zoneIds = ZoneId.getAvailableZoneIds();System.out.println(zoneIds);System.out.println(zoneIds.size());//2.获取当前系统的默认时区ZoneId zoneId = ZoneId.systemDefault();System.out.println(zoneId);//3.获取指定的时区ZoneId zoneId1 = ZoneId.of("Asia/Aqtau");System.out.println(zoneId1);}
2.1.2 Instant 时间戳
时间戳是一种新的计算时间方法,它可以精确到时间的纳秒值(标准时间)。
package DateClass;import java.time.Instant;
import java.time.ZoneId;
import java.time.ZonedDateTime;public class DateDemo6 {public static void main(String[] args) {//1.获取当前时间的Instant对象(标准时间)Instant now = Instant.now();System.out.println(now); //2024-08-16T10:35:23.164490400Z//2.根据(秒/毫秒/纳秒)获取Instant对象//获取原初时间戳Instant instant1 = Instant.ofEpochMilli(0L);System.out.println(instant1); //1970-01-01T00:00:00Z//获取相较于原初时间一秒后的时间戳Instant instant2 = Instant.ofEpochSecond(1L);System.out.println(instant2); //1970-01-01T00:00:01Z//获取1秒+1000000000纳秒后的时间Instant instant3 = Instant.ofEpochSecond(1L, 1000000000L);System.out.println(instant3); //1970-01-01T00:00:02Z//3.指定时区//指定时区后再获得当前系统时间,中国在东八区,会自动加上8个小时ZonedDateTime zonedDateTime = Instant.now().atZone(ZoneId.of("Asia/Shanghai"));System.out.println(zonedDateTime); //2024-08-16T18:45:01.710355900+08:00[Asia/Shanghai]//4.isXxx 判断Instant instant4 = Instant.ofEpochMilli(0L);Instant instant5 = Instant.ofEpochMilli(1000L);boolean result1 = instant4.isBefore(instant5);System.out.println(result1); //trueboolean result2 = instant4.isAfter(instant5);System.out.println(result2); //false//5.增减时间Instant instant6 = Instant.ofEpochMilli(3000L);System.out.println(instant6); //1970-01-01T00:00:03ZInstant instant7 = instant6.minusSeconds(1L);System.out.println(instant7); //1970-01-01T00:00:02ZInstant instant8 = instant6.plusMillis(5000L);System.out.println(instant8); //1970-01-01T00:00:08Z}
}
2.1.3 ZoneDateTime 带时区的时间
带时区的时间就是带上时区的时间。
package DateClass;import java.time.Instant;
import java.time.ZoneId;
import java.time.ZonedDateTime;public class DateDemo7 {public static void main(String[] args) {//1.获取当前带时区的时间对象ZonedDateTime now = ZonedDateTime.now();System.out.println(now);//2.获取指定的带时区时间对象//年月日时分秒纳秒方式指定ZonedDateTime time1 = ZonedDateTime.of(2023,10,1,11,12,12,0, ZoneId.of("Asia/Shanghai"));System.out.println(time1);//通过Instant + 时区的方式获取指定时间对象Instant instant = Instant.ofEpochMilli(0L);ZoneId zoneId = ZoneId.of("Asia/Shanghai");ZonedDateTime time2 = ZonedDateTime.ofInstant(instant, zoneId);System.out.println(time2); //1970-01-01T08:00+08:00[Asia/Shanghai]//3.withXxx 修改时间系列的方法,可以单独修改年月日等ZonedDateTime time3 = time2.withYear(2000);System.out.println(time3); //2000-01-01T08:00+08:00[Asia/Shanghai]//4.减少时间ZonedDateTime time4 = time3.minusYears(1);System.out.println(time4);//5.增加时间ZonedDateTime time5 = time3.plusYears(1);System.out.println(time5);//细节://JDK8新增的时间对象都是不可变的//如果我们进行任何形式的修改,调用者本身都不会改变,而是会产生一个新的时间}
}
2.2 DateTimeFormat 相关类
2.2.1 DateTimeFormatter 时间的格式化和解析类
两个方法用来时间格式化和解析。被解析的时间对象可以是ZonedDateTime (带时区的时间类对象)。
package DateClass;import java.time.Instant;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;public class DateDemo8 {public static void main(String[] args) {//获取时间对象ZonedDateTime time = Instant.now().atZone(ZoneId.of("Asia/Shanghai"));//生成对象DateTimeFormatter dtf1 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss EE a");//格式化时间对象System.out.println(dtf1.format(time));}
}
2.3 Calendar 相关类
2.3.1 LocalDate、LocalTime、LocalDateTime
这三个类是这种关系,LocalDateTime 能表示的最全,年月日时分秒都可以,LocalDate 只能表示年月日,LocalTime 只能表示时分秒。LocalDate 和 LocalTime 之间可以互转。
LocalDate 类用法如下。
package DateClass;import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.Month;
import java.time.MonthDay;public class LocalDateDemo {public static void main(String[] args) {//1.获取当前时间的日历对象(包含年月日)LocalDate nowDate = LocalDate.now();//2.获取指定的时间的日历对象LocalDate ldDate = LocalDate.of(2023, 1, 1);System.out.println("指定日期:" + ldDate);System.out.println("===============================");//3.get系列方法获取日历中的每一个属性值//获取年int year = ldDate.getYear();System.out.println("year:" + year);//获取月//方式一:通过Month对象获取Month m = ldDate.getMonth();System.out.println(m);System.out.println(m.getValue());//方式二:用int变量接收int month = ldDate.getMonthValue();System.out.println(month);//获取日int day = ldDate.getDayOfMonth();System.out.println("day:" + day);//获取一年的第几天int dayOfYear = ldDate.getDayOfYear();System.out.println("dayOfYear:" + dayOfYear);//获取星期DayOfWeek dayOfWeek = ldDate.getDayOfWeek();System.out.println(dayOfWeek);System.out.println(dayOfWeek.getValue());//is方法表判断System.out.println(ldDate.isBefore(ldDate));System.out.println(ldDate.isAfter(ldDate));//with开头方法表示修改,只能修改年月日//修改传回的是一个新的对象LocalDate withLocalDate = ldDate.withYear(2000);System.out.println(withLocalDate);System.out.println(withLocalDate == ldDate); //false//minus减少年月日LocalDate minusLocalDate = ldDate.minusYears(1);System.out.println(minusLocalDate);//plus增加年月日LocalDate plusLocalDate = ldDate.plusDays(1);System.out.println(plusLocalDate);//------------------------------//判断今天是否是你的生日LocalDate birDate = LocalDate.of(2000, 1, 1);LocalDate nowDate1 = LocalDate.now();//月日对象MonthDay birMd = MonthDay.of(birDate.getMonthValue(), birDate.getDayOfMonth());MonthDay nowMd = MonthDay.of(nowDate1.getMonthValue(), nowDate1.getDayOfMonth());System.out.println("今天是你的生日吗?" + birMd.equals(nowMd));}
}
LocalTime 类用法如下。
package DateClass;import java.time.LocalTime;public class LocalTimeDemo {public static void main(String[] args) {//获取本地时间的日历对象(时分秒)LocalTime nowTime = LocalTime.now();System.out.println("今天的时间:" + nowTime);int hour = nowTime.getHour(); //时System.out.println("hour:" + hour);int minute = nowTime.getMinute(); //分System.out.println("minute:" + minute);int second = nowTime.getSecond(); //秒System.out.println("second:" + second);int nano = nowTime.getNano(); //纳秒System.out.println("nano:" + nano);System.out.println("------------------------");System.out.println(LocalTime.of(8, 20, 30)); //时分秒LocalTime mTime = LocalTime.of(8, 20, 30, 150);//is系列方法System.out.println(nowTime.isBefore(mTime));System.out.println(nowTime.isAfter(mTime));//with系列方法,只能修改时分秒System.out.println(nowTime.withHour(10));//minus系列System.out.println(nowTime.minusHours(10));//plus系列System.out.println(nowTime.plusHours(10));}
}
2.4 JDK8 新增的三个工具类
它们是:
- Duration:时间间隔(秒,纳秒)
- Period:时间间隔(年,月,日)
- ChronoUnit:时间间隔(所有单位)
Period - 计算年月日之间的时间间隔。
package DateClass;import java.time.LocalDate;
import java.time.Period;public class TimeUtils {public static void main(String[] args) {//当前本地年月日LocalDate today = LocalDate.now();System.out.println(today);//生日的年月日LocalDate birthDate = LocalDate.of(2000, 1, 1);System.out.println(birthDate);//时间间隔是多少:第二个参数减去第一个参数Period period = Period.between(birthDate, today);System.out.println("相差的时间间隔对象:" + period);System.out.println(period.getYears());System.out.println(period.getMonths());System.out.println(period.getDays());//转化为总共差几个月System.out.println(period.toTotalMonths());}
}
Duration - 计算时分秒之间的时间间隔
package DateClass;import java.time.Duration;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.Period;public class TimeUtils {public static void main(String[] args) {//当前本地年月日LocalDateTime today = LocalDateTime.now();System.out.println(today);//出生日期的时间对象LocalDateTime birthDate = LocalDateTime.of(2000,1,1,0,00,00);System.out.println(birthDate);Duration duration = Duration.between(birthDate,today);System.out.println("相差的时间间隔对象:" + duration);System.out.println("========================");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());}
}
ChronoUnit - 计算年月日时分秒的时间间隔。
package DateClass;import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;public class ChronoUnitDemo {public static void main(String[] args) {//当前时间LocalDateTime today = LocalDateTime.now();System.out.println(today);//生日时间LocalDateTime birthDate = LocalDateTime.of(2000,1,1,0,0,0);System.out.println(birthDate);System.out.println("相差的年数:" + ChronoUnit.YEARS.between(birthDate, today));System.out.println("相差的月数:" + ChronoUnit.MONTHS.between(birthDate, today));System.out.println("相差的周数:"+ ChronoUnit.WEEKS.between(birthDate, today));System.out.println("相差的天数:"+ ChronoUnit.DAYS.between(birthDate, today));System.out.println("相差的时数:"+ ChronoUnit.HOURS.between(birthDate, today));System.out.println("相差的分数:"+ ChronoUnit.MINUTES.between(birthDate, today));System.out.println("相差的秒数:"+ ChronoUnit.SECONDS.between(birthDate, today));System.out.println("相差的毫秒数:"+ ChronoUnit.MILLIS.between(birthDate, today));System.out.println("相差的微秒数:"+ ChronoUnit.MICROS.between(birthDate, today));System.out.println("相差的纳秒数:"+ ChronoUnit.NANOS.between(birthDate, today));System.out.println("相差的半天数:"+ ChronoUnit.HALF_DAYS.between(birthDate, today));System.out.println("相差的十年数:"+ ChronoUnit.DECADES.between(birthDate, today));System.out.println("相差的世纪(百年)数:"+ ChronoUnit.CENTURIES.between(birthDate, today));System.out.println("相差的千年数:"+ ChronoUnit.MILLENNIA.between(birthDate, today));System.out.println("相差的纪元数:"+ ChronoUnit.ERAS.between(birthDate, today));}
}
相关文章:

【JAVA入门】Day21 - 时间类
【JAVA入门】Day21 - 时间类 文章目录 【JAVA入门】Day21 - 时间类一、JDK7前的时间相关类1.1 Date1.2 SimpleDateFormat1.3 Calendar 二、JDK8新增的时间相关类2.1 Date 相关类2.1.1 ZoneId 时区2.1.2 Instant 时间戳2.1.3 ZoneDateTime 带时区的时间 2.2 DateTimeFormat 相关…...

SQL server数据库备份和还原
新手小白都懂的sql server数据库备份和还原 一、备份 1.打开sql server数据库找到 2.展开找到对应的数据库文件 鼠标右击—任务–备份 3.复制名称 4.复制完点击添加 5.点击添加完之后再次点击查找路径 6.分别两个路径 原路径和新路径 (新路径是找到原路径新建了一…...

B站搜索建库架构优化实践
前言 搜索是B站的重要基础功能,需要对包括视频、评论、图文等海量的站内优质资源建立索引,处理来自用户每日数亿的检索请求。离线索引数据的正确、高效产出是搜索业务的基础。我们在这里分享搜索离线架构整体的改造实践:从周期长,…...

XSS反射实战
目录 1.XSS向量编码 2.xss靶场训练(easy) 2.1第一关 2.2第二关 方法一 方法二 2.3第三关 2.4第四关 2.5第五关 2.6第六关 2.7第七关 第一种方法: 第二种方法: 第三个方法: 2.8第八关 1.XSS向量编码 &…...
远程消息传递的艺术:NSDistantObject在Objective-C中的妙用
标题:远程消息传递的艺术:NSDistantObject在Objective-C中的妙用 引言 在Objective-C的丰富生态中,NSDistantObject扮演着至关重要的角色,特别是在处理分布式系统中的远程消息传递。它允许对象之间跨越不同地址空间进行通信&…...

指向派生类的基类指针、强转为 void* 再转为基类指针、此时调用虚函数会发生什么?
指向派生类的基类指针、强转为 void* 再转为基类指针、此时调用虚函数会发生什么? 1、无论指针类型怎么转,类对象内存没有发生任何变化,还是vfptr指向虚函数表,下面是成员变量,这在编译阶段就已经确定好了;…...

操作系统(Linux实战)-进程创建、同步与锁、通信、调度算法-学习笔记
1. 进程的基础概念 1.1 进程是什么? 定义: 进程是操作系统管理的一个程序实例。它包含程序代码及其当前活动的状态。每个进程有自己的内存地址空间,拥有独立的栈、堆、全局变量等。操作系统通过进程来分配资源(如 CPU 时间、内…...

react的setState中为什么不能用++?
背景: 在使用react的过程中产生了一些困惑,handleClick函数的功能是记录点击次数,handleClick函数被绑定到按钮中,每点击一次将通过this.state.counter将累计的点击次数显示在页面上 困惑: 为什么不能直接写prevStat…...

2.2算法的时间复杂度与空间复杂度——经典OJ
本博客的OJ标题均已插入超链接,点击可直接跳转~ 一、消失的数字 1、题目描述 数组nums包含从0到n的所有整数,但其中缺了一个。请编写代码找出那个缺失的整数。你有办法在O(n)时间内完成吗? 2、题目分析 (1)numsS…...

【CentOS 】DHCP 更改为静态 IP 地址并且遇到无法联网
文章目录 引言解决方式标题1. **编辑网络配置文件**:标题2. **确保配置文件包含以下内容**:特别注意 标题3. **重启网络服务**:标题4. **检查配置是否生效**:标题5. **测试网络连接**:标题6. **检查路由表**࿱…...

Linux 操作系统 --- 信号
序言 在本篇内容中,将为大家介绍在操作系统中的一个重要的机制 — 信号。大家可能感到疑惑,好像我在使用 Linux 的过程中并没有接触过信号,这是啥呀?其实我们经常遇到过,当我们运行的进程当进程尝试访问非法内存地址时…...

黑马前端——days09_css
案例 1 页面框架文件 <!DOCTYPE html> <html lang"zh-CN"><head><meta charset"UTF-8"><meta name"viewport" content"widthdevice-width, initial-scale1.0"><meta http-equiv"X-UA-Compati…...
【Python爬虫】技术深度探索与实践
目录 引言 第一部分:Python爬虫基础 1.1 网络基础 1.2 Python爬虫基本流程 第二部分:进阶技术 2.1 动态网页抓取 2.2 异步编程与并发 2.3 反爬虫机制与应对 第三部分:实践案例 第四部分:法律与道德考量 第五部分&#x…...

智启万象|挖掘广告变现潜力,保障支付安全便捷
谷歌致力于为开发者提供 先进的广告变现与支付解决方案 一起回顾 2024 Google 开发者大会 了解如何利用谷歌最新工具和功能 提高变现收入,优化用户体验,保障交易安全 让变现更上一层楼 广告检查器是谷歌 AdMob 平台最新推出的高级测试工具,开…...

函数递归,匿名、内置行数,模块和包,开发规范
一、递归与二分法 一)递归 1、递归调用的定义 递归调用:在调用一个函数的过程中,直接或间接地调用了函数本身 2、递归分为两类:直接与间接 #直接 def func():print(from func)func()func() # 间接 def foo():print(from foo)bar…...

Springboot3 整合swagger
一、pom.xml <dependency><groupId>org.springdoc</groupId><artifactId>springdoc-openapi-starter-webmvc-api</artifactId><version>2.1.0</version></dependency> 二、application.yml # SpringDoc配置 # springdoc:swa…...
查看同一网段内所有设备的ip
使用命令提示符(CMD)进行扫描 查看本机IP地址 首先通过 ipconfig /all 命令查看本机的IP地址,确定你的网段,例如 192.168.1.。 Ping网段内每个IP地址 接着使用循环命令: for /L %i IN (1,1,254) DO ping -w 1 -n …...

Spark MLlib 特征工程(上)
文章目录 Spark MLlib 特征工程(上)特征工程预处理 Encoding:StringIndexer特征构建:VectorAssembler特征选择:ChiSqSelector归一化:MinMaxScaler模型训练总结Spark MLlib 特征工程(上) 前面我们一起构建了一个简单的线性回归模型,来预测美国爱荷华州的房价。从模型效果来…...

《SPSS零基础入门教程》学习笔记——03.变量的统计描述
文章目录 3.1 连续变量(1)集中趋势(2)离散趋势(3)分布特征 3.2 分类变量(1)单个分类变量(2)多个分类变量 3.1 连续变量 (1)集中趋势 …...
2024年杭州市网络与信息安全管理员(网络安全管理员)职业技能竞赛的通知
2024年杭州市网络与信息安全管理员(网络安全管理员)职业技能竞赛的通知 一、组织机构 本次竞赛由杭州市总工会牵头,杭州市人力资源和社会保障局联合主办,杭州市萧山区总工会承办,浙江省北大信息技术高等研究院协办。…...

【大模型RAG】拍照搜题技术架构速览:三层管道、两级检索、兜底大模型
摘要 拍照搜题系统采用“三层管道(多模态 OCR → 语义检索 → 答案渲染)、两级检索(倒排 BM25 向量 HNSW)并以大语言模型兜底”的整体框架: 多模态 OCR 层 将题目图片经过超分、去噪、倾斜校正后,分别用…...
云原生核心技术 (7/12): K8s 核心概念白话解读(上):Pod 和 Deployment 究竟是什么?
大家好,欢迎来到《云原生核心技术》系列的第七篇! 在上一篇,我们成功地使用 Minikube 或 kind 在自己的电脑上搭建起了一个迷你但功能完备的 Kubernetes 集群。现在,我们就像一个拥有了一块崭新数字土地的农场主,是时…...

超短脉冲激光自聚焦效应
前言与目录 强激光引起自聚焦效应机理 超短脉冲激光在脆性材料内部加工时引起的自聚焦效应,这是一种非线性光学现象,主要涉及光学克尔效应和材料的非线性光学特性。 自聚焦效应可以产生局部的强光场,对材料产生非线性响应,可能…...

突破不可导策略的训练难题:零阶优化与强化学习的深度嵌合
强化学习(Reinforcement Learning, RL)是工业领域智能控制的重要方法。它的基本原理是将最优控制问题建模为马尔可夫决策过程,然后使用强化学习的Actor-Critic机制(中文译作“知行互动”机制),逐步迭代求解…...

centos 7 部署awstats 网站访问检测
一、基础环境准备(两种安装方式都要做) bash # 安装必要依赖 yum install -y httpd perl mod_perl perl-Time-HiRes perl-DateTime systemctl enable httpd # 设置 Apache 开机自启 systemctl start httpd # 启动 Apache二、安装 AWStats࿰…...
JVM垃圾回收机制全解析
Java虚拟机(JVM)中的垃圾收集器(Garbage Collector,简称GC)是用于自动管理内存的机制。它负责识别和清除不再被程序使用的对象,从而释放内存空间,避免内存泄漏和内存溢出等问题。垃圾收集器在Ja…...

江苏艾立泰跨国资源接力:废料变黄金的绿色供应链革命
在华东塑料包装行业面临限塑令深度调整的背景下,江苏艾立泰以一场跨国资源接力的创新实践,重新定义了绿色供应链的边界。 跨国回收网络:废料变黄金的全球棋局 艾立泰在欧洲、东南亚建立再生塑料回收点,将海外废弃包装箱通过标准…...
如何为服务器生成TLS证书
TLS(Transport Layer Security)证书是确保网络通信安全的重要手段,它通过加密技术保护传输的数据不被窃听和篡改。在服务器上配置TLS证书,可以使用户通过HTTPS协议安全地访问您的网站。本文将详细介绍如何在服务器上生成一个TLS证…...

前端开发面试题总结-JavaScript篇(一)
文章目录 JavaScript高频问答一、作用域与闭包1.什么是闭包(Closure)?闭包有什么应用场景和潜在问题?2.解释 JavaScript 的作用域链(Scope Chain) 二、原型与继承3.原型链是什么?如何实现继承&a…...
Swagger和OpenApi的前世今生
Swagger与OpenAPI的关系演进是API标准化进程中的重要篇章,二者共同塑造了现代RESTful API的开发范式。 本期就扒一扒其技术演进的关键节点与核心逻辑: 🔄 一、起源与初创期:Swagger的诞生(2010-2014) 核心…...