Two ways to display the current time in a JSP page simple instance _jsp programming

Source: Internet
Author: User

The JSP page implementation shows the current date time, we can use two ways to achieve:

1. By adding Java code implementations to the JSP page, the main code looks like this

Java.text.SimpleDateFormat SimpleDateFormat = new Java.text.SimpleDateFormat ( 
   "Yyyy-mm-dd HH:mm:ss"); 
  Java.util.Date currenttime = new Java.util.Date (); 
  String time = Simpledateformat.format (currenttime). toString ();  Put it in the head of the page} 

Current DateTime:<%=time%> This displays the formatted date time, and can be formatted differently in SimpleDateFormat according to our needs

2. Through JS implementation.

JS code is as follows: Clock.js

function Showtime () {var today; Var hour; var second; var minute; var year; var month; var date; var strdate; 
Today=new Date (); 
var n_day = Today.getday (); 
Switch (n_day) {case 0:{strdate = "Sunday"}break; 
Case 1:{strdate = "Monday"}break; 
Case 2:{strdate = "Tuesday"}break; 
Case 3:{strdate = "Wednesday"}break; 
Case 4:{strdate = "Thursday"}break; 
Case 5:{strdate = "Friday"}break; 
Case 6:{strdate = "Saturday"}break; 
Case 7:{strdate = "Sunday"}break; 
Year = Today.getyear (); 
month = Today.getmonth () +1; 
Date = Today.getdate (); 
hour = Today.gethours (); 
Minute =today.getminutes (); 
Second = Today.getseconds (); 
if (month<10) month= "0" +month; 
if (date<10) date= "0" +date; 
if (hour<10) hour= "0" +hour; 
if (minute<10) minute= "0" +minute; 
if (second<10) second= "0" +second;  document.getElementById (' clock '). InnerHTML = year + "years" + month + "Month" + Date + "Day" + strdate + "" + Hour + ":" + Minute + 
":" + second; 
SetTimeout ("Showtime ();", 1000); }

Then we introduce this JS in the JSP page,

The method Showtime () in the call can get the current time

The above is a small series for everyone to bring the JSP page two ways to show the current time of the simple example of all the content, I hope that we support cloud-Habitat Community ~

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.