📄 dateutil.java
字号:
int ageInYears = asofYYYY - birthYYYY;
int ageInMonths = asofMM - birthMM;
int ageInDays = asofDD - birthDD + 1;
if(ageInDays < 0)
{
/*
* Guaranteed after this single treatment, ageInDays will be >= 0.
* that is ageInDays = asofDD - birthDD + daysInBirthMM.
*/
ageInDays += from.getActualMaximum(Calendar.DAY_OF_MONTH);
ageInMonths--;
}
if(ageInDays == to.getActualMaximum(Calendar.DAY_OF_MONTH))
{
ageInDays = 0;
ageInMonths++;
}
if(ageInMonths < 0)
{
ageInMonths += 12;
ageInYears--;
}
if((birthYYYY < 0) && (asofYYYY > 0))
{
ageInYears--;
}
if(ageInYears < 0)
{
ageInYears = 0;
ageInMonths = 0;
ageInDays = 0;
}
int[] result = new int[3];
result[0] = ageInYears;
result[1] = ageInMonths;
result[2] = ageInDays;
return result;
}
/**
* Returns the current SQL date.
*
* @return Date
*/
public static java.sql.Date getSystemDate()
{
Calendar cal = Calendar.getInstance();
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
return new java.sql.Date(cal.getTime().getTime());
}
/**
* Returns the current timestamp.
*
* @return Timestamp
*/
public static Timestamp getSystemTimestamp()
{
return new Timestamp(System.currentTimeMillis());
}
/**
* Returns true if the length of the year is of 4 digits
*
* @param s String value Year
* @return True if the year length is of 4 digits, False otherwise
* @since 20/04/2001
*/
public static boolean isValidYearFormat(String s)
{
if(s == null)
{
return false;
} else if(s.trim().length() == 4)
{
return true;
}
return false;
}
/**
* This method convert the date to string
*
* @param date date
* @param strFormat the string format
* @return date as string format
*/
public static String getDate(Date date, String strFormat)
{
return DateUtil.parseDate(date, strFormat);
}
/**
* Returns true if the String is a valid date.
*
* @param strDate The date in format ddmmyyyy.
* @return True, if it is a valid date. False, otherwise.
*/
public static boolean isValidDate(String strDate)
{
return DateUtil.toDate(strDate, "ddMMyyyy") != null;
}
/**
* Returns true if the String is a valid date by specifying the date format to be verified.
*
* @param strDate The date.
* @param dateStrFormat The date format of the specified strDate
* @return True, if it is a valid date. False, otherwise.
*/
public static boolean isValidDate(String strDate, String dateStrFormat)
{
return DateUtil.toDate(strDate, dateStrFormat) != null;
}
/**
* Add year, month or day to a date
* To subtract the specified number of Days to the specified Date object, juz use a negative number
* Example: DateUtil.addDaysToDate(date, -5) == subtracting 5 days from the specified date.
* The same applies to month and year.
*
* @param type (int). Indicates the input num value is in year, month, or
* days. Valid values are Calendar.YEAR, Calendar.MONTH,
* Calendar.DATE
* @param date (java.sql.Date).
* @param num (int). The value to be added to the input date.
* @return java.sql.Date.
*/
public static Date addDate(int type, Date date, int num)
{
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(type, num);
return new Date(cal.getTime().getTime());
}
/**
* Adds the specified number of Days to the specified Date object
* To subtract the specified number of Days to the specified Date object, juz use a negative number
* Example: DateUtil.addDaysToDate(date, -5) == subtracting 5 days from the specified date.
*
* @param date Date to be add
* @param numDays Number of days to add
* @return date Added Date
*/
public static Date addDaysToDate(Date date, int numDays)
{
if(date == null)
{
return null;
}
Calendar c = Calendar.getInstance();
c.setTime(date);
c.add(Calendar.DATE, numDays);
return c.getTime();
}
/**
* Adds the specified number of Hours to the specified Date object
* To subtract the specified number of hours to the specified Date object, juz use a negative number
* Example: DateUtil.addDaysToDate(date, -5) == subtracting 5 hours from the specified date.
*
* @param date Date to be add
* @param numHours A valued byte that could possibly be of negative value.
* @return date Added Date
* @since 27/10/2001
*/
public static Date addHoursToDate(Date date, int numHours)
{
if(date == null)
{
return null;
}
Calendar c = Calendar.getInstance();
c.setTime(date);
c.add(Calendar.HOUR_OF_DAY, numHours);
return c.getTime();
}
/**
* Adds the specified number of Minutes to the specified Date object
* To subtract the specified number of Minutes to the specified Date object, juz use a negative number
* Example: DateUtil.addDaysToDate(date, -5) == subtracting 5 minutes from the specified date.
*
* @param date Date to be add
* @param numMins Number of minutes to add
* @return date Added Date
* @since 27/10/2001
*/
public static Date addMinutesToDate(Date date, int numMins)
{
if(date == null)
{
return null;
}
Calendar c = Calendar.getInstance();
c.setTime(date);
c.add(Calendar.MINUTE, numMins);
return c.getTime();
}
/**
* Adds the specified number of Months to the specified Date object
*
* @param date Date to be add
* @param numMonths Number of months to add
* @return date Added Date
*/
public static Date addMonthsToDate(Date date, int numMonths)
{
if(date == null)
{
return null;
}
Calendar c = Calendar.getInstance();
c.setTime(date);
c.add(Calendar.MONTH, numMonths);
return c.getTime();
}
/**
* Adds the specified number of Years to the specified Date object
*
* @param date Date to be add
* @param numYears Number of years to add
* @return date Added Date
*/
public static Date addYearsToDate(Date date, int numYears)
{
if(date == null)
{
return null;
}
Calendar c = Calendar.getInstance();
c.setTime(date);
c.add(Calendar.YEAR, numYears);
return c.getTime();
}
/**
* The method will compares 2 dates (excluding the HH MM SS)
*
* @param date1 1st date parameter
* @param date2 2nd date parameter
* @return returns -1 if 1st date parameter is earlier than 2nd date
* parameter retuns 0 if both dates parameter is the same day
* retuns 1 if 1st date parameter is later than 2nd date parameter
*/
public static int compareDates(Date date1, Date date2)
{
if((date1 == null) && (date2 == null))
{
return 0;
}
if(date1 == null)
{
return -1;
}
if(date2 == null)
{
return 1;
}
String strFormat = "yyyyMMdd";
SimpleDateFormat dateFormat = new SimpleDateFormat(strFormat);
int intDate1 = Integer.parseInt(dateFormat.format(date1));
int intDate2 = Integer.parseInt(dateFormat.format(date2));
if(intDate1 == intDate2)
{
return 0;
}
if(intDate1 > intDate2)
{
return 1;
}
return -1;
}
/**
* Parses Date object to formatted string
*
* @param date date to be converted
* @param formatStr Date/Time pattern. Example: ddMMyyyy or HHmmss or any
* other patterns
* @return String in required format Format : dd = Day MM = Month yyyy =
* Year HH = Hour mm = Minute ss = Second All format same as
* SimpleDateFormat. Null is returned if the date object is null.
* @since 22/03/2001
*/
public static String parseDate(Date date, String formatStr)
{
SimpleDateFormat dateFormat = new SimpleDateFormat(formatStr);
if(date == null)
{
return null;
} else
{
return dateFormat.format(date);
}
}
/**
* Parses Date object to date-time formatted string
*
* @param date THe date to be converted
* @return String in required format. Null is returned if the date object
* is null. (All format same as SimpleDateFormat)
* @since 25/10/2001
*/
public static String parseDate(Date date)
{
return parseDate(date, DATETIME_FORMAT);
}
/**
* Resets time fields of date to 00:00
*
* @param date Date to be reset the time to zero
* @return date Converted Date
*/
public static Date resetTime(Date date)
{
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
return cal.getTime();
}
/**
* Converts the specified date-time string to Date object based on the
* specified date-time format. <CODE>null</CODE> is returned if the
* specified date is invalid.
*
* @param strDateTime The date string in this format 'ddMMyyyy HHmm'.
* @param dateTimeFormat The date pattern in this format 'ddMMyyyy HHmm'
* @return the Date representation. Returns null if the date object or the
* strDateTime or the dateTimeFormat is null.
*/
public static Date toDate(String strDateTime, String dateTimeFormat)
{
if((strDateTime == null) || (strDateTime.length() == 0) ||
(dateTimeFormat == null) || (dateTimeFormat.length() == 0))
{
return null;
}
SimpleDateFormat dateFormat = new SimpleDateFormat(dateTimeFormat);
Date date = dateFormat.parse(strDateTime, new ParsePosition(0));
if(date == null)
{
return null;
}
String dateStr = parseDate(date, dateTimeFormat);
if(!strDateTime.equals(dateStr))
{
return null;
}
return date;
}
/**
* Converts the specified date-time string to Date object based on the
* specified date-time format. <CODE>null</CODE> is returned if the
* specified date is invalid.
*
* @param strDateTime The date string in this format 'ddMMyyyy HHmm'.
* @return the Date representation. Returns null if the date object or the
* strDateTime or the dateTimeFormat is null.
*/
public static Date toDate(String strDateTime)
{
return toDate(strDateTime, DATETIME_FORMAT);
}
/**
* Gets an integer string representation of the specified month.
*
* @param mthMMM Month of three letter string. For example, "JAN",
* "FEB",..
* @return a string number equivalent of the specified month string. If
* the specified month is unknown, zero string is returned that is
* "00".
* @since 27/03/2000
*/
public static String toMMFormat(String mthMMM)
{
if(mthMMM.equalsIgnoreCase(MTH_JAN))
{
return "01";
} else if(mthMMM.equalsIgnoreCase(MTH_FEB))
{
return "02";
} else if(mthMMM.equalsIgnoreCase(MTH_MAR))
{
return "03";
} else if(mthMMM.equalsIgnoreCase(MTH_APR))
{
return "04";
} else if(mthMMM.equalsIgnoreCase(MTH_MAY))
{
return "05";
} else if(mthMMM.equalsIgnoreCase(MTH_JUN))
{
return "06";
} else if(mthMMM.equalsIgnoreCase(MTH_JUL))
{
return "07";
} else if(mthMMM.equalsIgnoreCase(MTH_AUG))
{
return "08";
} else if(mthMMM.equalsIgnoreCase(MTH_SEP))
{
return "09";
} else if(mthMMM.equalsIgnoreCase(MTH_OCT))
{
return "10";
} else if(mthMMM.equalsIgnoreCase(MTH_NOV))
{
return "11";
} else if(mthMMM.equalsIgnoreCase(MTH_DEC))
{
return "12";
}
return null;
}
/**
* Gets a specified month string as JAN, FEB..
*
* @param mthMM The month as 2 digits For example, "01", "02",..
* @return a specified month string. If the specified month is unknown,
* empty string ("") is returned.
* @since 27/03/2000
*/
public static String toMMMFormat(String mthMM)
{
if("01".equals(mthMM))
{
return MTH_JAN;
} else if("02".equals(mthMM))
{
return MTH_FEB;
} else if("03".equals(mthMM))
{
return MTH_MAR;
} else if("04".equals(mthMM))
{
return MTH_APR;
} else if("05".equals(mthMM))
{
return MTH_MAY;
} else if("06".equals(mthMM))
{
return MTH_JUN;
} else if("07".equals(mthMM))
{
return MTH_JUL;
} else if("08".equals(mthMM))
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -