霞浦县建设局网站,小程序商城制作流程,老网站删除做新站会影响收录吗,无代码开发软件关于日期的学习 目录 关于日期的学习JDK8以前的APIDate Date常用APIDate的API应用 SimpleDateFormatSimpleDateFormat常用API测试 反向格式化(逆操作)测试 训练案例需求(秒杀活动)实现 Calendar需求痛点常见API应用测试 JDK8及以后的API(修改与新增)为啥学习(推荐使用)新增的AP…关于日期的学习 目录 关于日期的学习JDK8以前的APIDate Date常用APIDate的API应用 SimpleDateFormatSimpleDateFormat常用API测试 反向格式化(逆操作)测试 训练案例需求(秒杀活动)实现 Calendar需求痛点常见API应用测试 JDK8及以后的API(修改与新增)为啥学习(推荐使用)新增的APILocal...(或Local开头)API创建对象的方案LocalDate常用APILocalTime常用APILocalDateTime常用APIZone开头的Instant(某个时刻/时间戳)APIDateTimeForMatter(时间格式化器)APIPeriod常用APIDuration常用API 拓展时间格式符号 参考视频 JDK8以前的API
Date Date常用API Date的API应用
测试
package com.xie.time;import java.util.Date;
/*** JDK8以前的日期API* 目标掌握Data日期类的使用* 重点* 时间毫秒值 跟 日期对象 之间 的转化关系 要厘清* */
public class DateTest {public static void main(String[] args) {// 创建一个Data的对象代表的是系统的当前时间信息Date date1 new Date();System.out.println(date1);// 获取时间毫秒值long times date1.getTime();System.out.println(times);/** 把时间毫秒值 转换成 日期对象计算2秒之后的时间* 利用有参构造器进行转化* */times 2 * 1000;Date date2 new Date(times);System.out.println(date2);/** 或通过setTime()方法直接进行修改 */Date date3 new Date();date3.setTime(times);System.out.println(date3);}
}SimpleDateFormat
SimpleDateFormat常用API 格式化工作示意 测试
package com.xie.time;import java.text.SimpleDateFormat;
import java.util.Date;
/*** 优化Date对日期格式进行格式化使用户更加容易识别与接受** 目标掌握对于SimpleDateFormat的使用* 注SimpleDateFormat代表简单日期格式化可以用来 把日期对象、时间毫秒值 格式化成 我们想要的形式* */
public class SimpleDateFormatTest {public static void main(String[] args) {// 获取时间对象及毫秒值Date date1 new Date();long times date1.getTime();/** 创建一个格式化对象分别进行日期对象和时间毫秒值的格式化操作 */// SimpleDateFormat sdf new SimpleDateFormat(yyyy-MM-dd HH:mm:ss EEE a);SimpleDateFormat sdf new SimpleDateFormat(yyyy年MM月dd日 HH:mm:ss EEE a);/** 进行时间对象格式化 */String format1 sdf.format(date1);System.out.println(format1);/** 进行时间毫秒值格式化 */String format2 sdf.format(times);System.out.println(format2);}
}反向格式化(逆操作) 测试
package com.xie.time;import java.text.SimpleDateFormat;
/*** 反向操作把字符串时间格式 解析成 日期对象* 目标掌握SimpleDateFormat解析字符串时间 成 日期对象* */
public class SimpleDateFormatTest2 {public static void main(String[] args) throws Exception {// 设计一个字符串时间String dateStr 2018-10-11 12:12:11;/** 创建简单日期格式化对象* 注指定的时间格式 必须与 被解析的时间格式一模一样否则程序会出bug。* */SimpleDateFormat sdf new SimpleDateFormat(yyyy-MM-dd HH:mm:ss);// 解析并打印输出System.out.println(sdf.parse(dateStr));}
}训练案例
需求(秒杀活动) 实现
package com.xie.time.case1;import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
/*** 秒杀活动案例* 结果预测小贾成功小皮失败* */
public class Shop {public static void main(String[] args) throws ParseException {/** 用变量记录秒杀活动案例开始时间、结束时间、小贾下单时间、小皮下单时间 */String startTime 2023年11月11日 0:0:0;String endTime 2023年11月11日 0:10:0;String xj 2023年11月11日 0:01:18;String xp 2023年11月11日 0:10:51;/** 创建简单格式化对象把上面的字符串时间格式 解析成 日期对象 */SimpleDateFormat sdf new SimpleDateFormat(yyyy年MM月dd日 HH:mm:ss);// 解析成日期对象Date startTimeFormat sdf.parse(startTime);Date endTimeFormat sdf.parse(endTime);Date xjFormat sdf.parse(xj);Date xpFormat sdf.parse(xp);/** 把日期对象 转化成 时间毫秒值 */long startTimes startTimeFormat.getTime();long endTimes endTimeFormat.getTime();long xjTimes xjFormat.getTime();long xpTimes xpFormat.getTime();/** 通过时间比对 判断 秒杀操作成功与否 *//*** 小贾* */if (xjTimes startTimes xjTimes endTimes) {System.out.println(小贾秒杀成功了~~~);}else {System.out.println(小贾秒杀失败了~~~);}/*** 小皮* */if (xpTimes startTimes xpTimes endTimes) {System.out.println(小皮秒杀成功了~~~);}else {System.out.println(小皮秒杀失败了~~~);}}
}Calendar
需求痛点 常见API 注Calendar代表的是系统此刻时间对应的日历通过它可以单独获取、修改时间中的年、月、日、时、分、秒等。 Calendar是可变对象一旦修改后其对象本身表示的时间将产生变化。 应用测试
package com.xie.time;import java.util.Calendar;
import java.util.Date;
/*** 日历对象的创建及使用* 目标掌握Calender的使用和特点* */
public class CalenderTest {public static void main(String[] args) {/** 得到系统此刻时间对应的日历对象 并打印输出*/Calendar calendar Calendar.getInstance();System.out.println(calendar);// 获取日历中的某个信息并打印输出System.out.println(calendar.get(Calendar.YEAR));System.out.println(calendar.get(Calendar.MONTH));System.out.println(calendar.get(Calendar.DAY_OF_YEAR));System.out.println(calendar.get(Calendar.DAY_OF_MONTH));// 获取日历中记录的日期对象Date date calendar.getTime();System.out.println(date);// 获取到时间毫秒值long times calendar.getTimeInMillis();System.out.println(times);System.out.println(---------------分隔符-----------------);/** 修改日历中的某位信息 */// 修改月份为11月切记是从0开始遍历的calendar.set(Calendar.MONTH, 11);// 修改一年中的第几天 为第几天calendar.set(Calendar.DAY_OF_YEAR, 125);/** 增加或减少计算 */calendar.add(calendar.DAY_OF_YEAR, 100);calendar.add(calendar.DAY_OF_YEAR, -10);calendar.add(Calendar.DAY_OF_MONTH, 6);calendar.add(Calendar.HOUR, 12);// 打印输出查看修改效果System.out.println(calendar);}
}JDK8及以后的API(修改与新增) 注很多方法都是返回不可变对象不同于JDK8之前的 为啥学习(推荐使用) 新增的API Local…(或Local开头)API创建对象的方案 LocalDate常用API 应用测试
package com.xie.time.local;import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.Month;
/*** LocalDate API应用测试* */
public class LocalDateTest {public static void main(String[] args) {/** 获取本地日期对象(不可变对象而JDK8之前是可变对象) */LocalDate dateObject LocalDate.now();System.out.println(dateObject);// 1、获取日期对象中的信息int year dateObject.getYear();Month month dateObject.getMonth();int dayOfMonth dateObject.getDayOfMonth();int dayOfYear dateObject.getDayOfYear();DayOfWeek dayOfWeek dateObject.getDayOfWeek();System.out.println(dayOfWeek);int value dateObject.getDayOfWeek().getValue();System.out.println(value);System.out.println(year);// 2、直接修改某个信息: withYear、withMonth、withDayOfMonth、withDayOfYear/*** 每次修改都会返回一个 新的 日期对象* */System.out.println(------------------------------------);LocalDate dateObject2 dateObject.withYear(2099);LocalDate dateObject3 dateObject.withMonth(12);System.out.println(新的年-- dateObject2);System.out.println(旧的年-- dateObject);System.out.println(新的月-- dateObject3);// 3、把某个信息加多少: plusYears、plusMonths、plusDays、plusWeeks/*** 同样每次修改都会返回一个 新的 日期对象* */System.out.println();LocalDate dateObject4 dateObject.plusYears(2);System.out.println(加了2年-- dateObject4);// 4、把某个信息减多少: minusYears、minusMonths、minusDays、minusWeeks/*** 同样每次修改都会返回一个 新的 日期对象* */System.out.println();LocalDate dateObject5 dateObject.minusYears(2);System.out.println(减了2年-- dateObject5);LocalDate dateObject6 dateObject.minusMonths(2);System.out.println(减了2月-- dateObject6);// 5、获取指定日期的LocalDate对象: public static LocalDate of(int year,int month, int dayOfMonth)/*** 同样每次修改都会返回一个 新的 日期对象* */System.out.println();LocalDate dateObject7 dateObject.of(2099, 12, 1);LocalDate dateObject8 dateObject.of(2099, 12, 1);System.out.println(指定日期-- dateObject7);// 6、判断2个日期对象是否相等在前还是在后: equals isBefore isAfter/*** 同样每次修改都会返回一个 新的 日期对象* */System.out.println();System.out.println(dateObject7.equals(dateObject8));// trueSystem.out.println(dateObject7.isAfter(dateObject));// trueSystem.out.println(dateObject8.isBefore(dateObject));// false}
}LocalTime常用API 应用测试
package com.xie.time.local;import java.time.LocalTime;
/*** LocalTime API应用测试* */
public class LocalTimeTest {public static void main(String[] args) {/** 获取本地时间对象 */LocalTime time LocalTime.now();System.out.println(time);// 获取时间对象中的信息int hour time.getHour();//时int minute time.getMinute();//分int second time.getSecond();//秒int nano time.getNano();//纳秒// 2、修改时间: withHour、withMinute、withSecond、withNanoLocalTime time3 time.withHour(10);LocalTime time4 time.withMinute(10);LocalTime time5 time.withSecond(10);LocalTime time6 time.withNano(10);// 3、加多少: pLusHours、plusMinutes、plusSeconds、plusNanosLocalTime time7 time.plusHours(10);LocalTime time8 time.plusMinutes(10);LocalTime time9 time.plusSeconds(10);LocalTime time10 time.plusNanos(10);// 4、减多少:minusHours、minusMinutes、minusSeconds、minusNanosLocalTime time11 time.minusHours(10);LocalTime time12 time.minusMinutes(10);LocalTime time13 time.minusSeconds(10);LocalTime time14 time.minusNanos(10);// 5、获取指定时间的LocalTime对象:// public static LocalTime of(int hour,int minute,int second)LocalTime time15 LocalTime.of( 12,12 ,12);LocalTime time16 LocalTime.of(12,12 ,12);// 6、判断2个时间对象是否相等在前还是在后equals isBefore isAfterSystem.out.println(time15.equals(time16));System.out.println(time15.isAfter(time));System.out.println(time5.isBefore(time));}
}LocalDateTime常用API 应用测试
package com.xie.time.local;import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
/*** LocalDateTime API应用测试* */
public class LocalDateTimeTest {public static void main(String[] args) {/** 获取本地日期时间对象 */LocalDateTime dateTime LocalDateTime.now();System.out.println(dateTime);// 1、可以获取日期和时间的全部信息int year dateTime.getYear(); // 年int month dateTime.getMonthValue(); // 月int day dateTime.getDayOfMonth(); // 日int dayOfYear dateTime.getDayOfYear(); // 一年中的第几天int dayOfWeek dateTime.getDayOfWeek().getValue(); // 获取是周几int hour dateTime.getHour(); //时int minute dateTime.getMinute(); //分int second dateTime.getSecond(); //秒int nano dateTime.getNano(); //纳秒// 3、加多少:// plusYears plusMonths plusDays plusWeeks plusHours plusMinutes plusSeconds plusNanosLocalDateTime dateTime4 dateTime.plusYears(2);LocalDateTime dateTime5 dateTime.plusMinutes(3);// 4、减多少:// minusDays minusYears minusMonths minusWeeks minusHours minusMinutes minusSeconds minusNanosLocalDateTime dateTime6 dateTime.minusYears(2);LocalDateTime dateTime7 dateTime.minusMinutes(3);// 5、获取指定日期和时间的LocalDateTime对象// public static LocalDateTime of(int year, Month month, int dayOfMonth, int hour,// int minute, int second, int nanoOfSecond)LocalDateTime dateTime8 dateTime.of(2029, 12, 12, 12, 12, 12, 1222);LocalDateTime dateTime9 dateTime.of(2029, 12, 12, 12, 12, 12, 1222);// 6、 判断2个日期、时间对象是否相等在前还是在后equals、isBefore、isAfterSystem.out.println(dateTime9.equals(dateTime));System.out.println(dateTime9.isAfter(dateTime));System.out.println(dateTime9.isBefore(dateTime));// 7、可以把LocalDateTime 转换成 LocalDate 和 LocalTime// public LocalDate toLocalDate()// public LocalTime toLocalTime()// public static LocalDateTime of(LocalDate date, LocalTime time)System.out.println();LocalDate date dateTime.toLocalDate();LocalTime time dateTime.toLocalTime();LocalDateTime dateTime2 LocalDateTime.of(date, time);System.out.println(dateTime2 -- date time);}
}Zone开头的 应用测试
package com.xie.time.zone;import java.time.Clock;
import java.time.ZoneId;
import java.time.ZonedDateTime;
/*** 目标了解时区 和 带时区的时间* */
public class ZoneIdAndZonedDateTimeTest {public static void main(String[] args) {/** 获取系统默认时区 */ZoneId zoneId ZoneId.systemDefault();System.out.println(zoneId.getId());System.out.println(zoneId);/** 获取Java支持的全部时区Id */System.out.println(ZoneId.getAvailableZoneIds().size());System.out.println(ZoneId.getAvailableZoneIds());/** 把某个时区id 封装成 ZoneId对象 */ZoneId zoneId1 ZoneId.of(America/New_York);/** 获取某个时区的时间 即先获取ZonedDateTime对象 */ZonedDateTime zonedDateTime ZonedDateTime.now(zoneId1);System.out.println(zonedDateTime);/** 获取世界标准时间 */System.out.println(ZonedDateTime.now(Clock.systemUTC()));/** 获取 系统 默认时区 的ZonedDateTime对象 */ZonedDateTime now ZonedDateTime.now();System.out.println(now);}
}Instant(某个时刻/时间戳)API 注可以用来记录代码的执行时间或用于记录用户操作某个事件的时间点。 应用测试
package com.xie.time;import java.time.Instant;
/*** 目标掌握对Instant的使用* Instant时间线上的某个时刻/时间戳* */
public class InstantTest {public static void main(String[] args) {/** 创建Instant对象获取此刻的时间信息 */Instant instant Instant.now();/** 获取总秒数 */long epochSecond instant.getEpochSecond();System.out.println(epochSecond);/** 获取不够一秒的纳秒数 */System.out.println(instant.getNano());System.out.println(instant);/** 修改方法使用示例许多方法同LocalDateTime对象的 */Instant instant1 instant.plusNanos(1111);System.out.println(instant1);System.out.println();// Instant对象的作用做代码的性能分析或者记录用户的操作时间点Instant instant2 Instant.now();// 代码执行。。。Instant instant3 Instant.now();}
}DateTimeForMatter(时间格式化器)API 应用测试
package com.xie.time;import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;/*** 格式化器* */
public class DateTimeForMatterTest {public static void main(String[] args) {/** 创建 一个 日期时间 格式化器 对象 */DateTimeFormatter dateTimeFormatter DateTimeFormatter.ofPattern(yyyy年MM月dd日 HH:mm:ss);/** 获取时间 */LocalDateTime localDateTime1 LocalDateTime.now();System.out.println(localDateTime1);/** 对时间 进行格式化 正向格式化 */String format1 dateTimeFormatter.format(localDateTime1);System.out.println(format1);/** 格式化另一种方案用LocalDateTime对象的方法进行 我们称之为反向格式化是相较于上面一种的 */String format2 localDateTime1.format(dateTimeFormatter);System.out.println(format2);/** 解析时间: 解析时间一般使用LocalDateTime提供的解析方法来解析 */// 设置时间String dateStr 2029年12月12日 12:12:11;LocalDateTime localDateTime2 LocalDateTime.parse(dateStr, dateTimeFormatter);System.out.println(localDateTime2);}
}Period常用API 应用测试
package com.xie.time;import java.time.LocalDate;
import java.time.Period;
/*** Period常用API的使用
* */
public class PeriodTest {public static void main(String[] args) {/**通过 指定时间 获取 两个日期对象*/LocalDate startTime LocalDate.of(2029, 8, 10);LocalDate endTime LocalDate.of(2035, 12, 20);/** 创建Period对象封装两个时间点的 日期对象 */Period period Period.between(startTime, endTime);/** 通过period对象 来获取 两个日期对象 相差的信息 */System.out.println(两者相差 period.getYears() 年);System.out.println(两者相差 period.getMonths() 月);System.out.println(两者相差 period.getDays() 日或天);}
}Duration常用API 应用测试
package com.xie.time;import java.time.Duration;
import java.time.LocalDateTime;
/*** Duration常用API的使用* */
public class DurationTest {public static void main(String[] args) {/**通过 指定时间 获取 两个日期时间对象*/LocalDateTime startDateTime LocalDateTime.of(2035, 8, 10, 0, 0, 0);LocalDateTime endDateTime LocalDateTime.of(2035, 8, 15, 6, 5, 5);// 1、获取Duration对象Duration duration Duration.between(startDateTime, endDateTime);// 2、获取两个日期时间对象的间隔信息System.out.println(两者相差 duration.toDays() 天);// 间隔多少天System.out.println(两者相差 duration.toHours() 小时);// 间隔多少小时System.out.println(两者相差 duration.toMinutes() 分);// 间隔多少分System.out.println(两者相差 duration.getSeconds() 秒);// 间隔名少秒System.out.println(两者相差 duration.toMillis() 毫秒);// 间隔多少毫秒System.out.println(两者相差 duration.toNanos() 纳秒);// 间隔多少纳秒}
}拓展
时间格式符号 参考视频
黑马磊哥