Details | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 771 | blopes | 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 | */ |
||
| 17 | package dates; |
||
| 18 | |||
| 19 | import java.util.Calendar; |
||
| 20 | import java.util.Date; |
||
| 21 | |||
| 22 | public class JspCalendar { |
||
| 23 | final Calendar calendar; |
||
| 24 | |||
| 25 | public JspCalendar() { |
||
| 26 | calendar = Calendar.getInstance(); |
||
| 27 | Date trialTime = new Date(); |
||
| 28 | calendar.setTime(trialTime); |
||
| 29 | } |
||
| 30 | |||
| 31 | public int getYear() { |
||
| 32 | return calendar.get(Calendar.YEAR); |
||
| 33 | } |
||
| 34 | |||
| 35 | public String getMonth() { |
||
| 36 | int m = getMonthInt(); |
||
| 37 | String[] months = new String [] { "January", "February", "March", |
||
| 38 | "April", "May", "June", |
||
| 39 | "July", "August", "September", |
||
| 40 | "October", "November", "December" }; |
||
| 41 | if (m > 12) { |
||
| 42 | return "Unknown to Man"; |
||
| 43 | } |
||
| 44 | |||
| 45 | return months[m - 1]; |
||
| 46 | |||
| 47 | } |
||
| 48 | |||
| 49 | public String getDay() { |
||
| 50 | int x = getDayOfWeek(); |
||
| 51 | String[] days = new String[] {"Sunday", "Monday", "Tuesday", "Wednesday", |
||
| 52 | "Thursday", "Friday", "Saturday"}; |
||
| 53 | |||
| 54 | if (x > 7) { |
||
| 55 | return "Unknown to Man"; |
||
| 56 | } |
||
| 57 | |||
| 58 | return days[x - 1]; |
||
| 59 | |||
| 60 | } |
||
| 61 | |||
| 62 | public int getMonthInt() { |
||
| 63 | return 1 + calendar.get(Calendar.MONTH); |
||
| 64 | } |
||
| 65 | |||
| 66 | public String getDate() { |
||
| 67 | return getMonthInt() + "/" + getDayOfMonth() + "/" + getYear(); |
||
| 68 | |||
| 69 | } |
||
| 70 | |||
| 71 | public String getTime() { |
||
| 72 | return getHour() + ":" + getMinute() + ":" + getSecond(); |
||
| 73 | } |
||
| 74 | |||
| 75 | public int getDayOfMonth() { |
||
| 76 | return calendar.get(Calendar.DAY_OF_MONTH); |
||
| 77 | } |
||
| 78 | |||
| 79 | public int getDayOfYear() { |
||
| 80 | return calendar.get(Calendar.DAY_OF_YEAR); |
||
| 81 | } |
||
| 82 | |||
| 83 | public int getWeekOfYear() { |
||
| 84 | return calendar.get(Calendar.WEEK_OF_YEAR); |
||
| 85 | } |
||
| 86 | |||
| 87 | public int getWeekOfMonth() { |
||
| 88 | return calendar.get(Calendar.WEEK_OF_MONTH); |
||
| 89 | } |
||
| 90 | |||
| 91 | public int getDayOfWeek() { |
||
| 92 | return calendar.get(Calendar.DAY_OF_WEEK); |
||
| 93 | } |
||
| 94 | |||
| 95 | public int getHour() { |
||
| 96 | return calendar.get(Calendar.HOUR_OF_DAY); |
||
| 97 | } |
||
| 98 | |||
| 99 | public int getMinute() { |
||
| 100 | return calendar.get(Calendar.MINUTE); |
||
| 101 | } |
||
| 102 | |||
| 103 | |||
| 104 | public int getSecond() { |
||
| 105 | return calendar.get(Calendar.SECOND); |
||
| 106 | } |
||
| 107 | |||
| 108 | public static void main(String args[]) { |
||
| 109 | JspCalendar db = new JspCalendar(); |
||
| 110 | p("date: " + db.getDayOfMonth()); |
||
| 111 | p("year: " + db.getYear()); |
||
| 112 | p("month: " + db.getMonth()); |
||
| 113 | p("time: " + db.getTime()); |
||
| 114 | p("date: " + db.getDate()); |
||
| 115 | p("Day: " + db.getDay()); |
||
| 116 | p("DayOfYear: " + db.getDayOfYear()); |
||
| 117 | p("WeekOfYear: " + db.getWeekOfYear()); |
||
| 118 | p("era: " + db.getEra()); |
||
| 119 | p("ampm: " + db.getAMPM()); |
||
| 120 | p("DST: " + db.getDSTOffset()); |
||
| 121 | p("ZONE Offset: " + db.getZoneOffset()); |
||
| 122 | p("TIMEZONE: " + db.getUSTimeZone()); |
||
| 123 | } |
||
| 124 | |||
| 125 | private static void p(String x) { |
||
| 126 | System.out.println(x); |
||
| 127 | } |
||
| 128 | |||
| 129 | |||
| 130 | public int getEra() { |
||
| 131 | return calendar.get(Calendar.ERA); |
||
| 132 | } |
||
| 133 | |||
| 134 | public String getUSTimeZone() { |
||
| 135 | String[] zones = new String[] {"Hawaii", "Alaskan", "Pacific", |
||
| 136 | "Mountain", "Central", "Eastern"}; |
||
| 137 | |||
| 138 | return zones[10 + getZoneOffset()]; |
||
| 139 | } |
||
| 140 | |||
| 141 | public int getZoneOffset() { |
||
| 142 | return calendar.get(Calendar.ZONE_OFFSET)/(60*60*1000); |
||
| 143 | } |
||
| 144 | |||
| 145 | |||
| 146 | public int getDSTOffset() { |
||
| 147 | return calendar.get(Calendar.DST_OFFSET)/(60*60*1000); |
||
| 148 | } |
||
| 149 | |||
| 150 | |||
| 151 | public int getAMPM() { |
||
| 152 | return calendar.get(Calendar.AM_PM); |
||
| 153 | } |
||
| 154 | } |
||
| 155 |