Download presentation
Presentation is loading. Please wait.
Published byJuliet McDonald Modified over 9 years ago
1
Nic Shulver, N.A.Shulver@staffs.ac.uk Date and Time in JSP Surely it must be easy? Many calendars exist. The year 2013 (“Gregorian” or “Western”) is also: 6763, Assyrian 1420, Bengali 5773–5774, Hebrew 12013, Holocene (Geologist’s calendar) 1434–1435, Islamic 1356998400–1388534399, Unix…
2
Nic Shulver, N.A.Shulver@staffs.ac.uk Date and Time in JSP How do they work? Most calendars are solar (and lunar) – so there are about 365 days in a year. Gregorian: 365 days 5 hours 49 minutes 12 seconds on average! “Every year that is exactly divisible by four is a leap year, except for years that are exactly divisible by 100; …centurial years that are exactly divisible by 400 are still leap years.” Introduction to CalendarsIntroduction to Calendars, 13 September 2007, United States Naval Observatory.
3
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
4
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: / / See: http://fcet11.staffs.ac.uk:8080/nas1/examples/DateAndTime/date01.jsphttp://fcet11.staffs.ac.uk:8080/nas1/examples/DateAndTime/date01.jsp
5
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)”
6
Nic Shulver, N.A.Shulver@staffs.ac.uk Date and Time in JSP Date object with locales Date Tester
7
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( " Local date: "+ df.format( today ) ); Locale france = new Locale( "fr","FR" ); df=DateFormat.getDateInstance( df.LONG, france ); out.print( " French date: "+ df.format( today ) );
8
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( " German date: "+ df.format( today ) ); Locale usa = new Locale( "en","US" ); df=DateFormat.getDateInstance( df.LONG, usa ); out.print( " USA date: "+ df.format( today ) ); %> http://fcet11.staffs.ac.uk:8080/nas1/examples/DateAndTime/date02.jsp
9
Nic Shulver, N.A.Shulver@staffs.ac.uk Date and Time in JSP The DateFormat class Today is <% DateFormat df = DateFormat.getDateInstance(DateFormat.FULL); Date today = new Date(); String msg = df.format(today); out.println(msg); %> http://fcet11.staffs.ac.uk:8080/nas1/examples/DateAndTime/date04.jsp
10
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
11
Nic Shulver, N.A.Shulver@staffs.ac.uk Date and Time in JSP More useful links SimpleDateFormat class docs: http://download.oracle.com/javase/1.4.2/docs/ap i/java/text/SimpleDateFormat.html A proposal for “Earth Standard Time”: http://xkcd.com/1061/
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.