Nic Shulver, N.A.Shulver@staffs.ac.uk
Date and Time in JSP
Date is time, Calendar is date...
The Gregorian calendar provides the standard
calendar system used by most of the world
The java.util.GregorianCalendar() class provides a
lot of date
-
related functionality
The java.util.Date class Date represents a specific
instant in time, with millisecond precision
It used to handle date and time, but the Calendar
classes do dates much better
Nic Shulver, N.A.Shulver@staffs.ac.uk
Date and Time in JSP
Code Example
<%
java.util.Calendar currDate = new java.util.GregorianCalendar();
// add 1 to month as Calendar's months start at 0, not 1
int month = currDate.get(currDate.MONTH)+1;
int day = currDate.get(currDate.DAY_OF_MONTH);
int year = currDate.get(currDate.YEAR);
%>
The current date is: <%= year %>/<%= month %>/<%= day %>
See:
http://fcet11.staffs.ac.uk:8080/nas1/examples/DateAndTime/date01.jsp
Nic Shulver, N.A.Shulver@staffs.ac.uk
Date and Time in JSP
Time since the Epoch
In the Unix world, time started on the 1
st
January, 1970...
This is called “the epoch”
It's just a convenient way to measure elapsed
time
GregorianCalendar class: computeTime()
-
“Converts calendar field values to the time
value (millisecond offset from the Epoch)”
Nic Shulver, N.A.Shulver@staffs.ac.uk
Date and Time in JSP
Date object with locales
<%@ page language="java" contentType="text/html" %>
<%@ page import="java.util.*, java.text.*" %>
<html>
<head>
<title>Date Tester</title>
</head>
<body>
Nic Shulver, N.A.Shulver@staffs.ac.uk
Date and Time in JSP
Date object with locales
<%
Date today = new Date();
Locale here = request.getLocale();
DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT,here);
out.print( "<br>UK date: "+ df.format( today ) );
Locale france = new Locale( "fr","FR" );
df=DateFormat.getDateInstance( df.LONG, france );
out.print( "<br>French date: "+ df.format( today ) );
Nic Shulver, N.A.Shulver@staffs.ac.uk
Date and Time in JSP
Date object with locales
Locale germany = new Locale( "de","DE" );
df=DateFormat.getDateInstance( df.LONG, germany );
out.print( "<br>German date: "+ df.format( today ) );
Locale usa = new Locale( "en","US" );
df=DateFormat.getDateInstance( df.LONG, usa );
out.print( "<br>USA date: "+ df.format( today ) );
%>
</body></html>
http://fcet11.staffs.ac.uk:8080/nas1/examples/DateAndTime/date02.jsp
Nic Shulver, N.A.Shulver@staffs.ac.uk
Date and Time in JSP
The DateFormat class
<h1>Today is
<%
DateFormat df = DateFormat.getDateInstance(DateFormat.FULL);
Date today = new Date();
String msg = df.format(today);
out.println(msg);
%>
</h1>
http://fcet11.staffs.ac.uk:8080/nas1/examples/DateAndTime/date04.jsp
Nic Shulver, N.A.Shulver@staffs.ac.uk
Date and Time in JSP
Links to developer documents
Gregorian Calendar docs:
http://download.oracle.com/javase/1.5.0/docs/ap
i/java/util/GregorianCalendar.html
Date class docs:
http://download.oracle.com/javase/1.4.2/docs/ap
i/java/util/Date.html
SimpleDateFormat class docs:
http://download.oracle.com/javase/1.4.2/docs/ap
i/java/text/SimpleDateFormat.html
Enter the password to open this PDF file:
File name:
-
File size:
-
Title:
-
Author:
-
Subject:
-
Keywords:
-
Creation Date:
-
Modification Date:
-
Creator:
-
PDF Producer:
-
PDF Version:
-
Page Count:
-
Preparing document for printing…
0%
Comments 0
Log in to post a comment