1 / 11

Surely it must be easy?

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…. How do they work?.

weldon
Télécharger la présentation

Surely it must be easy?

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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. 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 Calendars, 13 September 2007, United States Naval Observatory.

  3. 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. 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

  5. Time since the Epoch • In the Unix world, time started on the 1st 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. Date object with locales • <%@ page language="java" contentType="text/html" %> • <%@ page import="java.util.*, java.text.*" %> • <html> • <head> • <title>Date Tester</title> • </head> • <body>

  7. Date object with locales • <% • Date today = new Date(); • Locale here = request.getLocale(); • DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT,here); • out.print( "<br>Local date: "+ df.format( today ) ); • Locale france = new Locale( "fr","FR" ); • df=DateFormat.getDateInstance( df.LONG, france ); • out.print( "<br>French date: "+ df.format( today ) );

  8. 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

  9. 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

  10. Links to developer documents • Gregorian Calendar docs: • http://download.oracle.com/javase/1.5.0/docs/api/java/util/GregorianCalendar.html • Date class docs: • http://download.oracle.com/javase/1.4.2/docs/api/java/util/Date.html

  11. More useful links • SimpleDateFormat class docs: • http://download.oracle.com/javase/1.4.2/docs/api/java/text/SimpleDateFormat.html • A proposal for “Earth Standard Time”: • http://xkcd.com/1061/

More Related