一:Date
Date类: 这个类是java.util.Date
getTime() : 获取内部维护的long值
Date date = new Date(); long time = date.getTime();
setTime():按照指定的long值(表示的时间)设置Date表示的时间
time += 60*60*24*1000; date.setTime(time);
before(): 此时间是否在指定时间之前
date1.before(date2) 1在2之前吗?
after():此时间是否在指定之间之后
二:Calendar
Calendar类:这个类是java.util包下的类,是JDK1.2推出的用于替换Date类的,但是在实际开发中我们还是使用Date类
和Date对象相互转换
getTime() : 获取当前系统时间的Date对象
Calendar calendar = Calendar.getInstance(); Date date = calendar.getTime(); long time = date.getTime();
setTime(): 将当前的calendar按照指定Date时间进行设置
Date beforeDay = new Date(); Calendar calendar = Calendar.getInstance(); calendar.setTime(beforeDay);创建当前时区实例对象 向上造型
Calendar c = Calendar.getInstance();
设置对象的具体值 get set 方法的调用
年月日时分秒:如图
Date time = calendar.getTime();
calendar.setTime(time);//将date日期数据转化为calendar类型、
c.get(Calendar.HOUR_OF_DAY); c.get(Calendar.MINUTE); c.get(Calendar.SECOND); c.get(Calendar.DAY_OF_WEEK) c.get(Calendar.DATE) c.get(Calendar.MONTH)+1add方法 c.set(Calendar.YEAR,Calendar.YEAR+3); 等同c.add(Calendar.YEAR,3)
三点五 作业练习
/**
*随机大小写六位数 大小写英文字符数字
*
*/
public class Test {public static void main(String[] args) {Random random = new Random();StringBuilder m = new StringBuilder() ;for (int i = 0; i < 6; i++) {char a = (char) ('a'+random.nextInt(26));char a1 = (char) ('A'+random.nextInt(26));int a2 = (int)(Math.random()*9);int a3 = random.nextInt(11);if (a3 <= 3){m.append(a);}elseif (a3 <= 6){m.append(a1);}elseif (a3 <= 9){m.append(a2);}}System.out.println(m);}
}
/*** 输入商品生产日期 保质天数* 促销日期是往前一周的周三* 2001-02-18* 11*/
public class Test03 {public static void main(String[] args) {try {Scanner scanner = new Scanner(System.in);System.out.println("输入生产日期");System.out.println("输入保质日期");String births = scanner.nextLine();
// String sells = scanner.next();SimpleDateFormat s1 = new SimpleDateFormat("yyyy-MM-dd");Date birth = s1.parse(births);
// Date sell = s1.parse(sells);int sells = scanner.nextInt();//先算出过期日期long expiredDays = birth.getTime()+sells*1000*24*60*60;//毫秒值Date expiredDay = new Date();expiredDay.setTime(expiredDays);//过期周x日到前一周周x 的日期Date beforeDay = new Date();long beforeDays = expiredDays - 7L*1000*60*60*24;beforeDay.setTime(beforeDays);//周x到周三的日期Calendar calendar = Calendar.getInstance();calendar.setTime(beforeDay);calendar.set(Calendar.DAY_OF_WEEK,Calendar.WEDNESDAY);System.out.println(calendar);Date time = calendar.getTime();String str = s1.format(time);System.out.println(str);} catch (ParseException e) {e.printStackTrace();}}
}
/** 两种方法* 输入商品生产日期 保质天数* 促销日期是往前一周的周三* 2001-02-18* 11 */
public class Test03 {public static void main(String[] args) {try {Scanner scanner = new Scanner(System.in);System.out.println("输入生产日期");System.out.println("输入保质日期");String births = scanner.nextLine();
// String sells = scanner.next();SimpleDateFormat s1 = new SimpleDateFormat("yyyy-MM-dd");Date birth = s1.parse(births);
// Date sell = s1.parse(sells);int sells = scanner.nextInt();//先算出过期日期long expiredDays = birth.getTime()+sells*1000*24*60*60;//毫秒值Date expiredDay = new Date();expiredDay.setTime(expiredDays);//过期周x日到前一周周x 的日期Date beforeDay = new Date();long beforeDays = expiredDays - 7L*1000*60*60*24;beforeDay.setTime(beforeDays);//周x到周三的日期Calendar calendar = Calendar.getInstance();calendar.setTime(beforeDay);calendar.set(Calendar.DAY_OF_WEEK,Calendar.WEDNESDAY);System.out.println(calendar);Date time = calendar.getTime();String str = s1.format(time);System.out.println(str);} catch (ParseException e) {e.printStackTrace();}}
}/*** 计算促销日期* 促销日期为该商品过期日前一周的周三* <p>* 程序启动动,要求用户输入一个商品的生产日期,以及保质期天数* 然后经过处理后输出该商品的促销日期* <p>* 日期格式:yyyy-MM-dd* <p>* 如:* 2023-7-8* 15* 该商品的促销日期为 2023-7-12*/
public class Test02 {public static void main(String[] args) {try {Scanner scan = new Scanner(System.in);System.out.println("请输入商品生产日期:(格式:yyyy-MM-dd)");String createDate = scan.nextLine();System.out.println("请输入商品保质期:");String strDay = scan.nextLine();//将保质期strDay转换int类型int days = Integer.parseInt(strDay); //保质期//将生产日期(String)转换成Date类型SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");Date date = sdf.parse(createDate); //生产日期//把生产日期date转换成CalendarCalendar calendar = Calendar.getInstance();calendar.setTime(date);//计算过期日calendar.add(Calendar.DAY_OF_YEAR, days);//计算过期日的前一周calendar.add(Calendar.DAY_OF_YEAR, -7);//设置为当周的周三calendar.set(Calendar.DAY_OF_WEEK, Calendar.WEDNESDAY);//将calendar转换成Date,Date time = calendar.getTime();//将time转换成字符串String dateStr = sdf.format(time);System.out.println("该商品的打折日是:" + dateStr);} catch (ParseException e) {e.printStackTrace();}}
}
/*** 秒杀活动* 开始时间 2023-11-11 00:00:00* 结束时间 2023-11-11 00:15:00* 小r秒杀时间 00:12:13* 小k秒杀时间 00:15:12*/
public class Test04 {public static void main(String[] args) {try {String s1 = "2023-11-11 00:00:00";String s2 = "2023-11-11 00:15:00";String r = "2023-11-11 00:12:13";String k = "2023-11-11 00:15:12";SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");Date date1 = simpleDateFormat.parse(s1);Date date2 = simpleDateFormat.parse(s2);Date dater = simpleDateFormat.parse(r);Date datek = simpleDateFormat.parse(k);if (dater.before(date2) && dater.after(date1) ){System.out.println("成功");}else {System.out.println("shibai");}if (datek.before(date2) && datek.after(date1)){ System.out.println("成功");}else {System.out.println("shibai");}} catch (ParseException e) {e.printStackTrace();}}
}
三:SimpleDatetime
创建SimpleDatetime对象 格式任意设置
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");格式
yyyy:年
MM:月
dd: 日
HH:24小时的时间
hh:12小时的时间
mm:分钟
ss:秒
实现String类和Date类相互转换 需要try{}crach(){}抛出格式不匹配异常
parse() 将字符串转换成Date类型
String date = "2008-08-08 20-08-08"; SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss"); Date d = simpleDateFormat.parse(date);
format() 将Date类型转换成String类型
Date date = new Date(); SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss"); String dateStr = simpleDateFormat.format(date);抛出格式不匹配异常
try {String s1 = "2001-02-18";SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");Date date1 = simpleDateFormat.parse(s1);Date date2 = new Date();System.out.println(date1.before(date2));System.out.println(date1.after(date2)); } catch (ParseException e) {e.printStackTrace(); }
四:Math 静态方法
double e = Math.E; double pi = Math.PI; Math.abs(323); //绝对值 Math.cbrt(2); //立方根 Math.floor(2.55);//向上取整 Math.ceil(3.66);//向下取整 Math.round(2.3); Math.round(1.88);//四舍五入 Math.sqrt(3);//平方根 Math.pow(3,2);//几的几次方 Math.random();//生成0-1随机数 包含0不包含1 double v = Math.random() * 10 + 1;