001 /* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 018 package cal; 019 020 import java.util.Calendar; 021 import java.util.Date; 022 023 public class JspCalendar { 024 Calendar calendar = null; 025 Date currentDate; 026 027 public JspCalendar() { 028 calendar = Calendar.getInstance(); 029 Date trialTime = new Date(); 030 calendar.setTime(trialTime); 031 } 032 033 034 public int getYear() { 035 return calendar.get(Calendar.YEAR); 036 } 037 038 public String getMonth() { 039 int m = getMonthInt(); 040 String[] months = new String[]{"January", "February", "March", 041 "April", "May", "June", 042 "July", "August", "September", 043 "October", "November", "December"}; 044 if (m > 12) 045 return "Unknown to Man"; 046 047 return months[m - 1]; 048 049 } 050 051 public String getDay() { 052 int x = getDayOfWeek(); 053 String[] days = new String[]{"Sunday", "Monday", "Tuesday", "Wednesday", 054 "Thursday", "Friday", "Saturday"}; 055 056 if (x > 7) 057 return "Unknown to Man"; 058 059 return days[x - 1]; 060 061 } 062 063 public int getMonthInt() { 064 return 1 + calendar.get(Calendar.MONTH); 065 } 066 067 public String getDate() { 068 return getMonthInt() + "/" + getDayOfMonth() + "/" + getYear(); 069 } 070 071 public String getCurrentDate() { 072 Date dt = new Date(); 073 calendar.setTime(dt); 074 return getMonthInt() + "/" + getDayOfMonth() + "/" + getYear(); 075 076 } 077 078 public String getNextDate() { 079 calendar.set(Calendar.DAY_OF_MONTH, getDayOfMonth() + 1); 080 return getDate(); 081 } 082 083 public String getPrevDate() { 084 calendar.set(Calendar.DAY_OF_MONTH, getDayOfMonth() - 1); 085 return getDate(); 086 } 087 088 public String getTime() { 089 return getHour() + ":" + getMinute() + ":" + getSecond(); 090 } 091 092 public int getDayOfMonth() { 093 return calendar.get(Calendar.DAY_OF_MONTH); 094 } 095 096 public int getDayOfYear() { 097 return calendar.get(Calendar.DAY_OF_YEAR); 098 } 099 100 public int getWeekOfYear() { 101 return calendar.get(Calendar.WEEK_OF_YEAR); 102 } 103 104 public int getWeekOfMonth() { 105 return calendar.get(Calendar.WEEK_OF_MONTH); 106 } 107 108 public int getDayOfWeek() { 109 return calendar.get(Calendar.DAY_OF_WEEK); 110 } 111 112 public int getHour() { 113 return calendar.get(Calendar.HOUR_OF_DAY); 114 } 115 116 public int getMinute() { 117 return calendar.get(Calendar.MINUTE); 118 } 119 120 121 public int getSecond() { 122 return calendar.get(Calendar.SECOND); 123 } 124 125 126 public int getEra() { 127 return calendar.get(Calendar.ERA); 128 } 129 130 public String getUSTimeZone() { 131 String[] zones = new String[]{"Hawaii", "Alaskan", "Pacific", 132 "Mountain", "Central", "Eastern"}; 133 134 return zones[10 + getZoneOffset()]; 135 } 136 137 public int getZoneOffset() { 138 return calendar.get(Calendar.ZONE_OFFSET) / (60 * 60 * 1000); 139 } 140 141 142 public int getDSTOffset() { 143 return calendar.get(Calendar.DST_OFFSET) / (60 * 60 * 1000); 144 } 145 146 147 public int getAMPM() { 148 return calendar.get(Calendar.AM_PM); 149 } 150 } 151 152 153 154 155