<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="java.util.Date" %>
<html>
<head>
<title>TIME</title>
<script type="text/javascript">
window.onload = function () {
let oldHours = new Date().getHours();
setInterval(function () {
let date = new Date();
let nowTime = document.getElementById("now-time");
let hours = date.getHours();
let minutes = date.getMinutes();
let seconds = date.getSeconds();
if (hours !== oldHours) {
window.location.reload();
}
nowTime.innerHTML = '现在时间是:' + (hours < 10 ? '0' + hours : hours) + '时' +
(minutes < 10 ? '0' + minutes : minutes) + '分' + (seconds < 10 ? '0' + seconds : seconds) + '秒';
}, 1000);
}
</script>
</head>
<body>
<%!
private String sayHello() {
long nowTime = System.currentTimeMillis();
Date date = new Date(nowTime);
int hours = date.getHours();
String info = "";
switch (hours) {
case 0:
case 1:
case 2:
case 3:
case 4: {
info = "凌晨好!";
break;
}
case 5:
case 6:
case 7: {
info = "早上好!";
break;
}
case 8:
case 9:
case 10: {
info = "上午好!";
break;
}
case 11:
case 12: {
info = "中午好!";
break;
}
case 13:
case 14:
case 15:
case 16: {
info = "下午好!";
break;
}
default: {
info = "晚上好!";
break;
}
}
return info;
}
%>
<%
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
String info = sayHello();
%>
<h1><%=info%></h1>
<p id="now-time"></p>
</body>
</html>
利用jsp写一个分时问候页面
最新推荐文章于 2022-03-05 18:59:32 发布