source: nutchez-0.1/tomcat/webapps/examples/WEB-INF/classes/dates/JspCalendar.java @ 222

Last change on this file since 222 was 66, checked in by waue, 16 years ago

NutchEz - an easy way to nutch

File size: 3.6 KB
Line 
1/*
2* Licensed to the Apache Software Foundation (ASF) under one or more
3* contributor license agreements.  See the NOTICE file distributed with
4* this work for additional information regarding copyright ownership.
5* The ASF licenses this file to You under the Apache License, Version 2.0
6* (the "License"); you may not use this file except in compliance with
7* the License.  You may obtain a copy of the License at
8*
9*     http://www.apache.org/licenses/LICENSE-2.0
10*
11* Unless required by applicable law or agreed to in writing, software
12* distributed under the License is distributed on an "AS IS" BASIS,
13* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14* See the License for the specific language governing permissions and
15* limitations under the License.
16*/
17package dates;
18
19import java.util.*;
20
21public class JspCalendar {
22    Calendar  calendar = null;
23
24    public JspCalendar() {
25  calendar = Calendar.getInstance();
26  Date trialTime = new Date();
27  calendar.setTime(trialTime);
28    }
29
30    public int getYear() {
31  return calendar.get(Calendar.YEAR);
32    }
33   
34    public String getMonth() {
35  int m = getMonthInt();
36  String[] months = new String [] { "January", "February", "March",
37          "April", "May", "June",
38          "July", "August", "September",
39          "October", "November", "December" };
40  if (m > 12)
41      return "Unknown to Man";
42 
43  return months[m - 1];
44
45    }
46
47    public String getDay() {
48  int x = getDayOfWeek();
49  String[] days = new String[] {"Sunday", "Monday", "Tuesday", "Wednesday", 
50              "Thursday", "Friday", "Saturday"};
51
52  if (x > 7)
53      return "Unknown to Man";
54
55  return days[x - 1];
56
57    }
58   
59    public int getMonthInt() {
60  return 1 + calendar.get(Calendar.MONTH);
61    }
62
63    public String getDate() {
64  return getMonthInt() + "/" + getDayOfMonth() + "/" +  getYear();
65
66    }
67
68    public String getTime() {
69  return getHour() + ":" + getMinute() + ":" + getSecond();
70    }
71
72    public int getDayOfMonth() {
73  return calendar.get(Calendar.DAY_OF_MONTH);
74    }
75
76    public int getDayOfYear() {
77  return calendar.get(Calendar.DAY_OF_YEAR);
78    }
79
80    public int getWeekOfYear() {
81  return calendar.get(Calendar.WEEK_OF_YEAR);
82    }
83
84    public int getWeekOfMonth() {
85  return calendar.get(Calendar.WEEK_OF_MONTH);
86    }
87
88    public int getDayOfWeek() {
89  return calendar.get(Calendar.DAY_OF_WEEK);
90    }
91     
92    public int getHour() {
93  return calendar.get(Calendar.HOUR_OF_DAY);
94    }
95   
96    public int getMinute() {
97  return calendar.get(Calendar.MINUTE);
98    }
99
100
101    public int getSecond() {
102  return calendar.get(Calendar.SECOND);
103    }
104
105    public static void main(String args[]) {
106  JspCalendar db = new JspCalendar();
107  p("date: " + db.getDayOfMonth());
108  p("year: " + db.getYear());
109  p("month: " + db.getMonth());
110  p("time: " + db.getTime());
111  p("date: " + db.getDate());
112  p("Day: " + db.getDay());
113  p("DayOfYear: " + db.getDayOfYear());
114  p("WeekOfYear: " + db.getWeekOfYear());
115  p("era: " + db.getEra());
116  p("ampm: " + db.getAMPM());
117  p("DST: " + db.getDSTOffset());
118  p("ZONE Offset: " + db.getZoneOffset());
119  p("TIMEZONE: " + db.getUSTimeZone());
120    }
121
122    private static void p(String x) {
123  System.out.println(x);
124    }
125
126
127    public int getEra() {
128  return calendar.get(Calendar.ERA);
129    }
130
131    public String getUSTimeZone() {
132  String[] zones = new String[] {"Hawaii", "Alaskan", "Pacific",
133               "Mountain", "Central", "Eastern"};
134 
135  return zones[10 + getZoneOffset()];
136    }
137
138    public int getZoneOffset() {
139  return calendar.get(Calendar.ZONE_OFFSET)/(60*60*1000);
140    }
141
142
143    public int getDSTOffset() {
144  return calendar.get(Calendar.DST_OFFSET)/(60*60*1000);
145    }
146
147   
148    public int getAMPM() {
149  return calendar.get(Calendar.AM_PM);
150    }
151}
152
Note: See TracBrowser for help on using the repository browser.