001 package time;
002
003 import java.util.*;
004
005 /**
006 * This is only a simple example of API time usage.
007 */
008 class Sample1 {
009
010 public static void main(String arg[]) {
011
012 // Get the default GregorianChronology
013 GregorianChronology gc = new GregorianChronology();
014
015 // For each Time, we have a comment.
016 // Our book is just a Map, where the key is the Time, and the value the comment.
017 Map<Time, String> book = new TreeMap<Time, String>();
018
019
020 //------------------------------------------------------------
021
022 // Build some common TimeMask, taking advantage of varargs
023
024 // This mask define format like YYYY/MM/DD
025 TimeMask yearMonthDay = new TimeMask(gc.YEAR, gc.MONTH, gc.DAY_OF_MONTH);
026
027 // This mask defines format like YYYY WW, where WW is the week number
028 TimeMask yearWeek = new TimeMask(gc.YEAR, gc.WEEK_OF_YEAR);
029
030 // This mask defines format like YYYY WW MM/DD, where WW is the week number
031 TimeMask yearMonthWeekDay =
032 new TimeMask(gc.YEAR, gc.MONTH, gc.WEEK_OF_YEAR, gc.DAY_OF_MONTH);
033
034 // We could also write:
035 yearMonthWeekDay = new TimeMask(yearMonthDay, yearWeek);
036
037 // This mask defines only hour & minute
038 TimeMask hourMinute = new TimeMask(gc.HOUR_OF_DAY, gc.MINUTE);
039
040 //------------------------------------------------------------
041
042 // Let's work on the Week 49 of Year 2005
043 // (You may notice that we use varargs and autoboxing)
044 Time week = new Time(yearWeek, 2005, 49);
045
046
047 // Imagine we want to do something each day of this week
048 // So we iterate on the weekday of this week:
049 for (Time day : gc.split(week, gc.DAY_OF_WEEK)) {
050
051 DayOfWeek dayWeek = day.get(gc.DAY_OF_WEEK); //Monday, Tuesday...
052
053 // Let's retrieve all information (month, day_of_month...)
054 Time full = gc.getTime(gc.getInterval(day), yearMonthWeekDay);
055
056 // No casting!
057 Month m = full.get(gc.MONTH);
058
059 // Auto-unboxing!
060 int dayOfMonth = full.get(gc.DAY_OF_MONTH);
061
062 // The 9:15 hour
063 Time h = new Time(hourMinute, 9, 15);
064
065 // We complete the day information with the hour & minute
066 Time dayAndHour = new Time(full, h);
067
068 // We update our timebook
069 book.put(dayAndHour, "It is 9:15.");
070 }
071
072
073 // Maybe we can print out timebook, in chronological order
074 for (Map.Entry<Time, String> entry : book.entrySet()) {
075 Time t = entry.getKey();
076 System.out.println("At " + t +
077 " the current entry is "+entry.getValue());
078 }
079
080 //------------------------------------------------------------
081
082 // Let's work on the 12/12/2005
083 Time day1 = new Time(yearMonthDay, 2005, Month.DECEMBER, 12);
084
085 // Imagine we have something to do each hour of this day
086 for (Time hourly : gc.split(day1, gc.HOUR_OF_DAY)) {
087 System.out.println("It is "+hourly);
088
089 // We want to use legacy code: we convert hourly in a
090 // java.util.Date.
091 // The usual convention is to use the beginning of the hour
092 // (Example: 12/12/2005 12:00:00.000)
093 Date date = gc.getInterval(hourly).getStart().asImmutableDate();
094 }
095
096 // By the way, how many days in the month?
097 // We get the month
098 Time monthOfDay1 = day1.subMap(new TimeMask(gc.YEAR, gc.MONTH));
099
100 // Or we can use SortepMap methods:
101 monthOfDay1 = day1.headMap(gc.DAY_OF_MONTH);
102
103 // The computation is simple:
104 int numberOfDayInMonth = gc.split(monthOfDay1, gc.DAY_OF_MONTH).size();
105
106 //------------------------------------------------------------
107
108 // Last example: compute the number of day between the 02/02/1970 and
109 // the 03/03/2010.
110 Time dayStart = new Time(yearMonthDay, 1970, Month.FEBRUARY, 2);
111 Time dayEnd = new Time(yearMonthDay, 2010, Month.MARCH, 3);
112
113 SortedSet<Time> allDaysSet = gc.asSortedSet(yearMonthDay);
114
115 // And the result is:
116 int nbDays = allDaysSet.subSet(dayStart, dayEnd).size();
117
118 // And what's the 1000st day after the 02/02/1970?
119 List<Time> allDaysList = gc.asList(yearMonthDay);
120 int idx = allDaysList.indexOf(dayStart);
121
122 // Here is the searched day:
123 Time dayStart1000 = allDaysList.get(idx+1000);
124 }
125 }
|