使用new Date()获取时间,通过SimpleDateFormat格式化类对Date进行格式话时间。
具体代码如下:注意HH大写代表24小时制。
使用SimpleDateFormat格式化格式时:
1、yyyy表示年,如2013;
2、MM表示月,如12;
3、dd表示天,如31;
4、hh表示用12小时制,如7;
5、HH表示用24小时制,如18;
6、mm表示分,如59;
7、ss表示秒,如59;
8、SSS表示毫米,如333。
扩展资料:
Java时间string转换成时间:
输出结果:1510416000000,2017-11-12。方便的实现了string转时间的功能。
参考资料:
百度百科--java日期函数
你的意思的 如果当前时间是18点 - 23.59之间才返回 否则不返回是不
Date d = new Date();
int hours = d.getHours();
if(18
}else{
return null;
}
获取当前详细时间如下:
Date date =new Date();
SimpleDateFormat sdf =new SimpleDateFormat("HH:mm:ss");//只有时分秒
//SimpleDateFormat sdf =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//完整的时间
String time=sdf.format(date);
SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH");
String nowTime=format.format(new Date());
String now=nowTime.substring(nowTime.length()-2,nowTime.length());
if(17
}
Date time=new Date();
System.out.println(new Date);
SimpleDateFormat format=new SimpleDateFormat("hh");
String x=format.format(time);
if(Integer.parseInt(x)>17){
return ??
}