1 16 package dates; 17 18 import java.text.DateFormat ; 19 import java.util.*; 20 21 public 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 153 | Popular Tags |