各位帮忙看下怎么才能获取系统时间让在不同的时间显示不同的信息呢?public static void main(String[] args){
int work=2;
switch(work){
case 1:
System.out.println("现在是凌晨了,注意休息,不要熬夜"); //凌晨1-6点
break;
case 2:
System.out.println("早上好,记得洗脸刷牙哦"); //早上7-9点
break;
case 3:
System.out.println("忙碌的一个上午进行着"); //上午10-12点
break;
case 4:
System.out.println("中午了,不要忘记吃饭哦"); //中午13-15点
break;
case 5:
System.out.println("下午很容易困,要集中精力");//下午16-18点
break;
case 6:
System.out.println("忙碌了一天了,泡杯茶听听音乐放松一下吧");//晚上19-21点
}
}
int work=2;
switch(work){
case 1:
System.out.println("现在是凌晨了,注意休息,不要熬夜"); //凌晨1-6点
break;
case 2:
System.out.println("早上好,记得洗脸刷牙哦"); //早上7-9点
break;
case 3:
System.out.println("忙碌的一个上午进行着"); //上午10-12点
break;
case 4:
System.out.println("中午了,不要忘记吃饭哦"); //中午13-15点
break;
case 5:
System.out.println("下午很容易困,要集中精力");//下午16-18点
break;
case 6:
System.out.println("忙碌了一天了,泡杯茶听听音乐放松一下吧");//晚上19-21点
}
}
http://blog.csdn.net/zjx2388/archive/2008/07/13/2646149.aspx
-
http://blog.csdn.net/rainlight/archive/2006/04/14/663757.aspx
int hour = c.get(Calendar.HOUR_OR_DAY);
...
import java.util.Date;
public class CreateDateObject { public static void main(String[] args) {
// TODO Auto-generated method stub
Date d1=new Date();
System.out.println("d1:"+d1);//打印当前日期
//Date d2=new Date( 2010,3,24,20,58);
//System.out.println("d2:"+d2);
Date timeNow=new Date();
System.out.println("General way(toString):"+timeNow.toString());
System.out.println("Local time(tolocaleStringDate):"+timeNow.toLocaleString());
System.out.println("GMT time(toGMTstring()):"+timeNow.toGMTString());
System.out.println("Time offset"+timeNow.getTimezoneOffset());
//System.in.read();
System.out.println();
}}
结果是
d1:Fri Apr 02 17:21:16 CST 2010
General way(toString):Fri Apr 02 17:21:16 CST 2010
Local time(tolocaleStringDate):2010-4-2 17:21:16
GMT time(toGMTstring()):2 Apr 2010 09:21:16 GMT
Time offset-480
不过把 HOUR_OF_DAY 写错了。
public static void main(String[] args){
//实例化日期类,不建议使用Date
Calendar c = Calendar.getInstance();
//获取当前小时数
int hour = c.get(Calendar.HOUR_OF_DAY);
int work = 2; //根据 hour 判断对 work 赋值 ……
}
Date today=new Date();
int work=today.getHours();
获取当前的小时..
int time = date.getHours(); switch (time) {
case 1:
case 2:
case 3:
case 4:
case 5:
case 6:
System.out.println("现在是凌晨了,注意休息,不要熬夜"); // 凌晨1-6点
break;
case 7:
case 8:
case 9:
System.out.println("早上好,记得洗脸刷牙哦"); // 早上7-9点
break;
case 10:
case 11:
case 12:
System.out.println("忙碌的一个上午进行着"); // 上午10-12点
break;
case 13:
case 14:
case 15:
System.out.println("中午了,不要忘记吃饭哦"); // 中午13-15点
break;
case 16:
case 17:
case 18:
System.out.println("下午很容易困,要集中精力");// 下午16-18点
break;
case 19:
case 20:
case 21:
System.out.println("忙碌了一天了,泡杯茶听听音乐放松一下吧");// 晚上19-21点
case 22:
case 23:
case 24:
System.out.println("夜深了,注意不要熬夜");
}---------------------------------------
用If来写:Date date = new Date();
int time = date.getHours();
if (time <= 6) {
System.out.println("现在是凌晨,您还没有休息吗?");
} else if (time <= 12) {
System.out.println("新的一天到来了,您准备好一天的工作了吗");
} else if (time <= 15) {
System.out.println("中午到了,您吃饭了吗?");
} else if (time <= 17) {
System.out.println("下午的时光总是让人很悠闲");
} else if (time <= 24) {
System.out.println("夜深了,早点休息吧,不要熬夜");
}else{
System.out.println("中网欢迎您");
}