Simple examples of two ways to display the current time in the jsp page

  • 2021-09-16 07:41:30
  • OfStack

In the jsp page, we can display the current date and time in one of the following two ways:

1. By adding Java code to the jsp page, the main code is as follows


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();  // On the page head Medium } 

Current date and time: < %=time% > This shows the formatted date and time, which can be set in different formats in simpledateformat according to our needs

2. It is implemented by js.

The code for js 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 = " Week 1" 
}break; 
case 2:{ 
strDate =" Week 2" 
}break; 
case 3:{ 
strDate = " Week 3" 
}break; 
case 4:{ 
strDate = " Week 4" 
}break; 
case 5:{ 
strDate = " Week 5" 
}break; 
case 6:{ 
strDate = " Week 6" 
}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 + " Year " + month + " Month " + date + " Day  " + strDate +" " + hour + ":" + minute + ":" + second; 
setTimeout("showtime();", 1000); 
}

Then we introduce this js on the jsp page.

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


Related articles: