发布网友 发布时间:2022-04-26 14:33
共4个回答
热心网友 时间:2022-06-29 06:12
已经改好!加几个if判断即可,希望对你有帮助
import java.util.Calendar;
import java.util.Date;
public class gaintime {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Date today = new Date();
System.out.println("当前时间是" + today.toString());
Calendar now = Calendar.getInstance();
int day = now.get(Calendar.DATE);
int time = now.get(Calendar.HOUR_OF_DAY);
System.out.println(time);
if (time >= 0 && time <= 6) {
time = 0;
} else if (time >= 7 && time <= 12) {
time = 1;
} else if (time >= 13 && time <= 14) {
time = 2;
} else if (time >= 15 && time < 18) {
time = 3;
} else if (time >= 19 && time <= 22) {
time = 4;
} else if (time >= 23 && time <= 24) {
time = 5;
}
switch (time) {
case 0:
System.out.println("早上好!");
break;
case 1:
System.out.println("工作时间!");
break;
case 2:
System.out.println("午安时间!");
break;
case 3:
System.out.println("工作时间!");
break;
case 4:
System.out.println("努力工作!");
break;
case 5:
System.out.println("晚安啦!");
break;
}
}
}
热心网友 时间:2022-06-29 06:13
import java.util.*;
public class gaintime {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Date today = new Date();
System.out.println("当前时间是" + today.toString());
Calendar now = Calendar.getInstance();
int day = now.get(Calendar.DATE);
int time = now.get(Calendar.HOUR_OF_DAY);
System.out.println(time);
if (time < 7)// 0:00-6:00就输出“早上好!”
System.out.println("早上好!");
else if (time < 12 && time >= 7)// :00-12:00输出“工作时间!”
System.out.println("工作时间!");
else if (time < 14 && time >= 12)// 13:00-14:00输出“午安时间!”
System.out.println("午安时间!");
else if (time < 18 && time >= 14)// 15:00-18:00输出“工作时间”
System.out.println("工作时间!");
else if (time < 22 && time >= 18)// 19:00-22:00输出“努力工作!”
System.out.println("努力工作!");
else
// 22:00-24:00输出“晚安!“
System.out.println("晚安啦!");
}
}
热心网友 时间:2022-06-29 06:13
import java.util.*;
public class test{
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Date today=new Date();
System.out.println("当前时间是"+today.toString());
Calendar now=Calendar.getInstance();
int day=now.get(Calendar.DATE);
int time=now.get(Calendar.HOUR_OF_DAY);
System.out.println(time);
if(0 <= time && time < 6){
System.out.println("早上好!");
}else if(7 <= time && time < 12){
System.out.println("工作时间!");
}else if(13 <= time && time < 14){
System.out.println("午安时间!");
}else if(12 <= time && time < 18){
System.out.println("工作时间!");
}else if(19 <= time && time < 22){
System.out.println("努力工作!");
}else if(22 <= time && time < 24){
System.out.println("晚安啦!");
}
// switch(time)
// {
// case 0: System.out.println("早上好!");break;
// case 1: System.out.println("工作时间!");break;
// case 2: System.out.println("午安时间!");break;
// case 3: System.out.println("工作时间!");break;
// case 4: System.out.println("努力工作!");break;
// case 5: System.out.println("晚安啦!");break;
}
}
}
热心网友 时间:2022-06-29 06:14
通过java.util.Date类获取当前时间。
示例:
public static void main(String[] args){