1

I am attempting to write an example JSP page for myself (very new to jsp), and have gone over an example to write one, but how do I get time to consistently update?

here is my snippet of code:

<body>
    <%
        java.text.DateFormat df = new java.text.SimpleDateFormat(
                "HH:mm:ss:SS z MM/dd/yyyy");
        Calendar cal = Calendar.getInstance();
    %>

    <h1>
        Current Date and Time:
        <%=df.format(cal.getTime())%>
    </h1>
</body>

By the way i'm using a tomcat server to deploy this

Vnge
  • 1,295
  • 25
  • 49

3 Answers3

2
    function updateYourTime() {
        var now = new Date(), 
            months = ['January', 'February', '...']; 
            time = now.getHours() + ':' + now.getMinutes(), 

            date = [now.getDate(), 
                    months[now.getMonth()],
                    now.getFullYear()].join(' ');

        document.getElementById('currentTime').innerHTML = [date, time].join(' / ');

        setTimeout(updateYourTime, 1000);//This method will call for every second
    }
    updateYourTime(); // initial call

see here for details

<div id="currentTime"></time>
Community
  • 1
  • 1
PSR
  • 39,804
  • 41
  • 111
  • 151
0

do you mean to show clock in your pages?

you can use java script. here is an example

Daniel Robertus
  • 1,100
  • 1
  • 11
  • 24
0

to show server clock in clients jsp use this javascripcode with java

Add a label where ever you want to show the server Time

<strong>Server Time&nbsp;:&nbsp;&nbsp;</strong><label id="timelable"></label>

And then add the following java script code at the end of the jsp inside the body tag

<script type="text/javascript">
        var myVar = setInterval(function(){ myTimer() }, 1000);
        var jsVar=  <%=java.util.Calendar.getInstance().getTimeInMillis()%>;
        var timeZoneOffset=<%=java.util.TimeZone.getDefault().getOffset(System.currentTimeMillis())%>;

        jsVar=jsVar+timeZoneOffset;
        function myTimer() {
        jsVar=jsVar+1000;
        var d = new Date(jsVar);
        var t=d.toUTCString();
    document.getElementById("timelable").innerHTML = t;
}

        </script>

Thats it now you will see the server time running in you jsp.

Baji Shaik
  • 1,022
  • 2
  • 10
  • 14