计算机中时间原点
1970年1月1日 00:00:00
时间换算单位
1秒 = 1000毫秒
Date类概述
Date 代表了一个特定的时间,精确到毫秒
Date类构造方法
| 方法名 | 说明 |
|---|---|
| public Date() | 分配一个 Date对象,并初始化,以便它代表它被分配的时间,精确到毫秒 |
| public Date(long date) | 分配一个 Date对象,并将其初始化为表示从标准基准时间起指定的毫秒数 |
示例代码
xpublic class DateDemo01 { public static void main(String[] args) { //public Date():分配一个 Date对象,并初始化,以便它代表它被分配的时间,精确到毫秒 Date d1 = new Date(); System.out.println(d1); //public Date(long date):分配一个 Date对象,并将其初始化为表示从标准基准时间起指定的毫秒数 long date = 1000*60*60; Date d2 = new Date(date); System.out.println(d2); }}常用方法
| 方法名 | 说明 |
|---|---|
| public long getTime() | 获取的是日期对象从1970年1月1日 00:00:00到现在的毫秒值 |
| public void setTime(long time) | 设置时间,给的是毫秒值 |
示例代码
xxxxxxxxxxpublic class DateDemo02 { public static void main(String[] args) { //创建日期对象 Date d = new Date(); //public long getTime():获取的是日期对象从1970年1月1日 00:00:00到现在的毫秒值// System.out.println(d.getTime());// System.out.println(d.getTime() * 1.0 / 1000 / 60 / 60 / 24 / 365 + "年"); //public void setTime(long time):设置时间,给的是毫秒值// long time = 1000*60*60; long time = System.currentTimeMillis(); d.setTime(time); System.out.println(d); }}SimpleDateFormat类概述
SimpleDateFormat是一个具体的类,用于以区域设置敏感的方式格式化和解析日期。
我们重点学习日期格式化和解析
SimpleDateFormat类构造方法
| 方法名 | 说明 |
|---|---|
| public SimpleDateFormat() | 构造一个SimpleDateFormat,使用默认模式和日期格式 |
| public SimpleDateFormat(String pattern) | 构造一个SimpleDateFormat使用给定的模式和默认的日期格式 |
SimpleDateFormat类的常用方法
格式化(从Date到String)
解析(从String到Date)
示例代码
xxxxxxxxxxpublic class SimpleDateFormatDemo { public static void main(String[] args) throws ParseException { //格式化:从 Date 到 String Date d = new Date();// SimpleDateFormat sdf = new SimpleDateFormat(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss"); String s = sdf.format(d); System.out.println(s); System.out.println("--------"); //从 String 到 Date String ss = "2048-08-09 11:11:11"; //ParseException SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date dd = sdf2.parse(ss); System.out.println(dd); }}需求
秒杀开始时间是2020年11月11日 00:00:00,结束时间是2020年11月11日 00:10:00,用户小贾下单时间是2020年11月11日 00:03:47,用户小皮下单时间是2020年11月11日 00:10:11,判断用户有没有成功参与秒杀活动
实现步骤
代码实现
xxxxxxxxxxpublic class DateDemo5 { public static void main(String[] args) throws ParseException { //开始时间:2020年11月11日 0:0:0 //结束时间:2020年11月11日 0:10:0 //小贾2020年11月11日 0:03:47 //小皮2020年11月11日 0:10:11 //1.判断两位同学的下单时间是否在范围之内就可以了。 //2.要把每一个时间都换算成毫秒值。 String start = "2020年11月11日 0:0:0"; String end = "2020年11月11日 0:10:0"; String jia = "2020年11月11日 0:03:47"; String pi = "2020年11月11日 0:10:11"; SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss"); long startTime = sdf.parse(start).getTime(); long endTime = sdf.parse(end).getTime();// System.out.println(startTime);// System.out.println(endTime); long jiaTime = sdf.parse(jia).getTime(); long piTime = sdf.parse(pi).getTime(); if(jiaTime >= startTime && jiaTime <= endTime){ System.out.println("小贾同学参加上了秒杀活动"); }else{ System.out.println("小贾同学没有参加上秒杀活动"); } System.out.println("------------------------"); if(piTime >= startTime && piTime <= endTime){ System.out.println("小皮同学参加上了秒杀活动"); }else{ System.out.println("小皮同学没有参加上秒杀活动"); } } }