一、编程实现分时问候 上午好下午好等效果
package com.athhq.zuoye;
import java.util.Calendar;
/**
* @author HHQ996
* @create 2022-07-13 10:38
*/
public class Ex4 {
public static void main(String[] args) {
var c = Calendar.getInstance();
var h = c.get(Calendar.HOUR_OF_DAY);
//早上 5-8 上午 8-12 中午 12-14 下午 14-18 晚上 18-23 0-2 午夜 3-5 凌晨
h =14;
if(h>=5 && h<8){
System.out.println("早上好");
}else if(h>=8 && h<12){
System.out.println("上午好");
}else if(h>=12 && h<14){
System.out.println("中午好");
}else if(h>=14 && h<18){
System.out.println("下午好");
}else if(h>=18 && h<=23){
System.out.println("晚上好");
}else if(h>=0 && h<3){
System.out.println("午夜,多注意身体");
}else if(h>=3 && h<5){
System.out.println("凌晨好");
}
}
}
二、编程实现输出10个随机整数范围在10-20之间
int num = (int) Math.round(Math.random() * (9 - 0)) + 10;
三、编程实现随机日期范围在:2022-1-1 0:0:0 - 现在日期
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date d = sdf.parse("2022-1-1 0:0:0");
long start = d.getTime();
long now = System.currentTimeMillis();
long time = Math.round(Math.random() * (now - start) + start);
System.out.println(sdf.format(time));
四、编程实现计算两个日期之间的相差的天数
// 计算两个日期之间相关的天数
Date birth = sdf.parse("2021-1-1 0:0:0");
// 将字符串解析转换为Date
Date ddd = new Date();
long days = (ddd.getTime() - birth.getTime())/1000/60/60/24;
System.out.println(days);
五、编程实现判断年份是不是闰年?
int year = 2020;
if((year % 4 == 0 && year % 100 != 0) || year % 400 == 0){
System.out.println("是闰年");
}else {
System.out.println("不是闰年");
}