📄 datetime.java
字号:
// ----------------------------------------------------------------------------// Copyright 2006-2008, Martin D. Flynn// All rights reserved// ----------------------------------------------------------------------------//// Licensed under the Apache License, Version 2.0 (the "License");// you may not use this file except in compliance with the License.// You may obtain a copy of the License at// // http://www.apache.org/licenses/LICENSE-2.0// // Unless required by applicable law or agreed to in writing, software// distributed under the License is distributed on an "AS IS" BASIS,// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.// See the License for the specific language governing permissions and// limitations under the License.//// ----------------------------------------------------------------------------// Description:// This class provides many Date/Time utilities// ----------------------------------------------------------------------------// Change History:// 2006/03/26 Martin D. Flynn// Initial release// 2006/06/30 Martin D. Flynn// -Repackaged// 2008/01/10 Martin D. Flynn// -Added 'getMonthStart'/'getMonthEnd' methods.// 2008/02/17 Martin D. Flynn// -Added date format constants// 2008/02/21 Martin D. Flynn// -Replaced date format constants with 'default' date/time formats// 2008/03/12 Martin Dl Flynn// -Added method 'parseArgumentDate'// 2008/04/11 Martin Dl Flynn// -Modified 'parseArgumentDate' to use the argument timezone when calculating// the default year/month/day.// ----------------------------------------------------------------------------package org.opengts.util;import java.util.*;import java.io.*;import java.text.*;/***** Performs every manner of function imaginable based on date/time values**/public class DateTime implements Comparable, Cloneable{ // ------------------------------------------------------------------------ public static final String GMT_TIMEZONE = "GMT"; // ------------------------------------------------------------------------ public static final String DEFAULT_TIME_FORMAT = "HH:mm:ss"; public static final String DEFAULT_DATE_FORMAT = "yyyy/MM/dd"; /** *** Analyzes the specified date format to determine the date separator characters *** @param dateFormat The date format to analyze *** @return The date separator characters **/ public static char[] GetDateSeparatorChars(String dateFormat) { char sep[] = new char[] { '/', '/' }; if (dateFormat != null) { for (int i = 0, c = 0; (i < dateFormat.length()) && (c < sep.length); i++) { char ch = dateFormat.charAt(i); if (!Character.isLetterOrDigit(ch)) { sep[c++] = ch; } } } return sep; } // ------------------------------------------------------------------------ public static final long HOURS_PER_DAY = 24L; public static final long SECONDS_PER_MINUTE = 60L; public static final long MINUTES_PER_HOUR = 60L; public static final long DAYS_PER_WEEK = 7L; public static final long SECONDS_PER_HOUR = SECONDS_PER_MINUTE * MINUTES_PER_HOUR; public static final long MINUTES_PER_DAY = HOURS_PER_DAY * MINUTES_PER_HOUR; public static final long SECONDS_PER_DAY = MINUTES_PER_DAY * SECONDS_PER_MINUTE; public static final long MINUTES_PER_WEEK = DAYS_PER_WEEK * MINUTES_PER_DAY; public static final long SECONDS_PER_WEEK = MINUTES_PER_WEEK * SECONDS_PER_MINUTE; /** *** Returns the number of seconds in the specified number of days *** @param days The number of days to convert to seconds *** @return The number of seconds **/ public static long DaySeconds(long days) { return days * SECONDS_PER_DAY; } /** *** Returns the number of seconds in the specified number of days *** @param days The number of days to convert to seconds *** @return The number of seconds **/ public static long DaySeconds(double days) { return (long)Math.round(days * (double)SECONDS_PER_DAY); } /** *** Returns the number of seconds in the specified number of hours *** @param hours The number of hours to convert to seconds *** @return The number of seconds **/ public static long HourSeconds(long hours) { return hours * SECONDS_PER_HOUR; } /** *** Returns the number of seconds in the specified number of minutes *** @param minutes The number of minutes to convert to seconds *** @return The number of seconds **/ public static long MinuteSeconds(long minutes) { return minutes * SECONDS_PER_MINUTE; } // ------------------------------------------------------------------------ public static final int JAN = 0; public static final int FEB = 1; public static final int MAR = 2; public static final int APR = 3; public static final int MAY = 4; public static final int JUN = 5; public static final int JUL = 6; public static final int AUG = 7; public static final int SEP = 8; public static final int OCT = 9; public static final int NOV = 10; public static final int DEC = 11; public static final int JANUARY = JAN; public static final int FEBRUARY = FEB; public static final int MARCH = MAR; public static final int APRIL = APR; //public static final int MAY = MAY; public static final int JUNE = JUN; public static final int JULY = JUL; public static final int AUGUST = AUG; public static final int SEPTEMBER = SEP; public static final int OCTOBER = OCT; public static final int NOVEMBER = NOV; public static final int DECEMBER = DEC; // I18N? private static final String MONTH_NAME[][] = { { "January" , "Jan" }, { "February" , "Feb" }, { "March" , "Mar" }, { "April" , "Apr" }, { "May" , "May" }, { "June" , "Jun" }, { "July" , "Jul" }, { "August" , "Aug" }, { "September", "Sep" }, { "October" , "Oct" }, { "November" , "Nov" }, { "December" , "Dec" }, }; /** *** Gets the 0-based index for the specified month abbreviation *** @param month The month abbreviation *** @return The 0-based index [0..11] of the specified month appreviation **/ public static int getMonthNumber0(String month) { String m = (month != null)? month.toLowerCase().trim() : null; if ((m != null) && !m.equals("")) { if (Character.isDigit(m.charAt(0))) { int v = StringTools.parseInt(m,-1); return (v < 0)? 0 : ((v > 30)? 30 : v); } else { for (int i = 0; i < MONTH_NAME.length; i++) { if (m.startsWith(MONTH_NAME[i][1].toLowerCase())) { return i; } } } } return -1; } /** *** Gets the 1-based index for the specified month abbreviation *** @param month The month abbreviation *** @return The 1-based index [0..11] of the specified month appreviation **/ public static int getMonthNumber1(String month) { int m = DateTime.getMonthNumber0(month); return (m < 0)? -1 : (m + 1); } /** *** Gets the month name/abbreviation for the specified 1-based month index *** @param mon1 A 1-based month index [1..12] *** @param abbrev True to return the month abbreviation, false to return the name *** @return The month name/abbreviation **/ public static String getMonthName(int mon1, boolean abbrev) { int mon0 = mon1 - 1; if ((mon0 >= JANUARY) && (mon0 <= DECEMBER)) { return abbrev? MONTH_NAME[mon0][1] : MONTH_NAME[mon0][0]; } else { return ""; } } /** *** Returns all month names/appreviations *** @param abbrev True to return month abbreviations, false to return month names *** @return An array of month names/abbreviations **/ public static String[] getMonthNames(boolean abbrev) { String mo[] = new String[MONTH_NAME.length]; for (int i = 0; i < MONTH_NAME.length; i++) { mo[i] = DateTime.getMonthName(i, abbrev); } return mo; } /** *** Returns a Map object containing a map of month names/abbreviations and it's *** 0-based month index [0..11] *** @param abbrev True to create the Map object with abbreviations, false for names *** @return The Map object **/ public static Map<String,Integer> getMonthNameMap(boolean abbrev) { Map<String,Integer> map = new OrderedMap<String,Integer>(); for (int i = 0; i < MONTH_NAME.length; i++) { map.put(DateTime.getMonthName(i, abbrev), new Integer(i)); } return map; } // ------------------------------------------------------------------------ private static final int MONTH_DAYS[] = { 31, // Jan 29, // Feb 31, // Mar 30, // Apr 31, // May 30, // Jun 31, // Jul 31, // Aug 30, // Sep 31, // Oct 30, // Nov 31, // Dec }; /** *** Gets the number of days in the specified month *** @param tz The TimeZone *** @param mon1 The 1-based month index [1..12] *** @param year The year *** @return The number of days in the specified month **/ public static int getDaysInMonth(TimeZone tz, int mon1, int year) { int yy = (year > mon1)? year : mon1; // larger of the two int mm = (year > mon1)? mon1 : year; // smaller of the two DateTime dt = new DateTime(tz, yy, mm, 1); return DateTime.getMaxMonthDayCount(mm, dt.isLeapYear(tz)); } /** *** Gets the maximum number of days in the specified month *** @param m1 The 1-based month index [1..12] *** @param isLeapYear True for leap-year, false otherwise *** @return The maximum number of days in the specified month **/ public static int getMaxMonthDayCount(int m1, boolean isLeapYear) { int m0 = m1 - 1; int d = ((m0 >= 0) && (m0 < DateTime.MONTH_DAYS.length))? DateTime.MONTH_DAYS[m0] : 31; return ((m0 != FEBRUARY) || isLeapYear)? d : 28; } // ------------------------------------------------------------------------ public static final int SUN = 0; public static final int MON = 1; public static final int TUE = 2; public static final int WED = 3; public static final int THU = 4; public static final int FRI = 5; public static final int SAT = 6; public static final int SUNDAY = SUN; public static final int MONDAY = MON; public static final int TUESDAY = TUE; public static final int WEDNESDAY = WED; public static final int THURSDAY = THU; public static final int FRIDAY = FRI; public static final int SATURDAY = SAT; // I18N? private static final String DAY_NAME[][] = { // 0 1 2 { "Sunday" , "Sun", "Su" }, { "Monday" , "Mon", "Mo" }, { "Tuesday" , "Tue", "Tu" }, { "Wednesday", "Wed", "We" }, { "Thursday" , "Thu", "Th" }, { "Friday" , "Fri", "Fr" }, { "Saturday" , "Sat", "Sa" }, }; /** *** Gets the day-of-week number for the specified day short abbreviation *** @param day The day short abbreviation *** @return The 0-based day number/index [0..6] **/ public static int getDayNumber(String day) { String d = (day != null)? day.toLowerCase().trim() : null; if ((d != null) && !d.equals("")) { if (Character.isDigit(d.charAt(0))) { int v = StringTools.parseInt(d,-1); return (v < 0)? 0 : (v % 7); } else { for (int i = 0; i < DAY_NAME.length; i++) { if (d.startsWith(DAY_NAME[i][2].toLowerCase())) { return i; } } } } return -1; } /** *** Gets the day-of-week name for the specified day number/index *** @param day A 0-based day number/index [0..6] *** @param abbrev 0 for full name, 1 for abbreviation, 2 for short abbreviation *** @return The day-of-week name/abbreviation **/ public static String getDayName(int day, int abbrev) { if ((day >= SUNDAY) && (day <= SATURDAY) && (abbrev >= 0) && (abbrev <= 2)) { return DAY_NAME[day][abbrev]; } else { return ""; } } /** *** Gets the day-of-week name for the specified day number/index *** @param day A 0-based day number/index [0..6] *** @param abbrev True for abbreviation, false for full name *** @return The day-of-week name/abbreviation **/ public static String getDayName(int day, boolean abbrev) { return DateTime.getDayName(day, abbrev? 1 : 0); } /** *** Returns an array of day-of-week names *** @param abbrev 0 for full name, 1 for abbreviation, 2 for short abbreviation *** @return An array of day-of-week names/abbreviations **/ public static String[] getDayNames(int abbrev) { String dy[] = new String[DAY_NAME.length]; for (int i = 0; i < DAY_NAME.length; i++) { dy[i] = DateTime.getDayName(i, abbrev); } return dy; } /** *** Returns an array of day-of-week names *** @param abbrev True abbreviations, false for full names *** @return An array of day-of-week names/abbreviations **/ public static String[] getDayNames(boolean abbrev) { return DateTime.getDayNames(abbrev? 1 : 0); } /** *** Returns a Map object of day-of-week names to their 0-based number/index *** (used as a VComboBox item list) *** @param abbrev 0 for full name, 1 for abbreviation, 2 for short abbreviation *** @return The Map object **/ public static Map<String,Integer> getDayNameMap(int abbrev) { Map<String,Integer> map = new OrderedMap<String,Integer>(); for (int i = 0; i < DAY_NAME.length; i++) { map.put(DateTime.getDayName(i, abbrev), new Integer(i)); } return map; } /** *** Returns a Map object of day-of-week names to their 0-based number/index *** (used as a VComboBox item list) *** @param abbrev True for abbreviations, false for full names *** @return The Map object **/ public static Map<String,Integer> getDayNameMap(boolean abbrev) { return DateTime.getDayNameMap(abbrev? 1 : 0); } // ------------------------------------------------------------------------ /** *** Gets a String array of hours in a day *** (used as a VComboBox item list) *** @param hr24 True for 24 hour clock, false for 12 hour clock *** @return A String array of hours in a day **/ public static String[] getHours(boolean hr24)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -