110 likes | 186 Views
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?.
E N D
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? • 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.
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
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
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)”
Date object with locales • <%@ page language="java" contentType="text/html" %> • <%@ page import="java.util.*, java.text.*" %> • <html> • <head> • <title>Date Tester</title> • </head> • <body>
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 ) );
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
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
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
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/