一个比较全的日期操作例子Calendar
2024-07-21 02:14:13
供稿:网友
import java.text.dateformat; import java.util.*;
public class jspcalendar { calendar calendar = null;
public jspcalendar() { calendar = calendar.getinstance(); date trialtime = new date(); calendar.settime(trialtime); }
public int getyear() { return calendar.get(calendar.year); } public string getmonth() { int m = getmonthint(); string[] months = new string [] { "january", "february", "march", "april", "may", "june", "july", "august", "september", "october", "november", "december" }; if (m > 12) return "unknown to man"; return months[m - 1];
}
public string getday() { int x = getdayofweek(); string[] days = new string[] {"sunday", "monday", "tuesday", "wednesday", "thursday", "friday", "saturday"};
if (x > 7) return "unknown to man";
return days[x - 1];
} public int getmonthint() { return 1 + calendar.get(calendar.month); }
public string getdate() { return getmonthint() + "/" + getdayofmonth() + "/" + getyear();
}
public string gettime() { return gethour() + ":" + getminute() + ":" + getsecond(); }
public int getdayofmonth() { return calendar.get(calendar.day_of_month); }
public int getdayofyear() { return calendar.get(calendar.day_of_year); }
public int getweekofyear() { return calendar.get(calendar.week_of_year); }
public int getweekofmonth() { return calendar.get(calendar.week_of_month); }
public int getdayofweek() { return calendar.get(calendar.day_of_week); } public int gethour() { return calendar.get(calendar.hour_of_day); } public int getminute() { return calendar.get(calendar.minute); }
public int getsecond() { return calendar.get(calendar.second); }
public static void main(string args[]) { jspcalendar db = new jspcalendar(); p("date: " + db.getdayofmonth()); p("year: " + db.getyear()); p("month: " + db.getmonth()); p("time: " + db.gettime()); p("date: " + db.getdate()); p("day: " + db.getday()); p("dayofyear: " + db.getdayofyear()); p("weekofyear: " + db.getweekofyear()); p("era: " + db.getera()); p("ampm: " + db.getampm()); p("dst: " + db.getdstoffset()); p("zone offset: " + db.getzoneoffset()); p("timezone: " + db.getustimezone()); }
private static void p(string x) { system.out.println(x); }
public int getera() { return calendar.get(calendar.era); }
public string getustimezone() { string[] zones = new string[] {"hawaii", "alaskan", "pacific", "mountain", "central", "eastern"}; return zones[10 + getzoneoffset()]; }
public int getzoneoffset() { return calendar.get(calendar.zone_offset)/(60*60*1000); }
public int getdstoffset() { return calendar.get(calendar.dst_offset)/(60*60*1000); }
public int getampm() { return calendar.get(calendar.am_pm); } }