我写的一个Date函数库
/**
* һЩÈÕÆÚ´¦Àíº¯Êý¼¯
*
* @author Bruce
* @see com.macroview.jcomponent.datecombobox.DgDatePickerPanel
* @see com.macroview.jcomponent.datecombobox.DgDateComboBox
* @version 1.0
* date: 2002.3.30
* copyright: Macroview Telecom
*/
package com.macroview.jcomponent.datecombobox;
import java.util.Calendar;
/** ÈÕÆÚ´¦Àí¹¤¾ßº¯Êý¼¯
*/
public class DgDateUtils
{
private DgDateUtils()
{}
/** Translate String to Integer
* @return (if Exception function will return -1) */
public static int str2int(String s)
{
try
{
return Integer.parseInt(s);
}
catch (Exception e)
{
return -1;
}
}
/** ÅжÏÊÇ·ñΪÈòÄê */
public final static boolean isLeapYear(int year)
{
return ((year % 4 == 0) &&
((year % 100 != 0) || (year % 400 == 0)));
}
/** µÃµ½Ö¸¶¨ÈÕÆÚÊÇÐÇÆÚ¼¸
* @return 0: Sunday, 1: Monday, 2: Tuesday ... 6: Saturday
* */
public final static int getDateOfWeek(int year, int month, int day)
{
Calendar cal = Calendar.getInstance();
cal.set(year, month-1, day);
return cal.get(cal.DAY_OF_WEEK) - 1;
}
/** @return »ñÈ¡¸ÃÔ·ݵÄ×î´óÌìÊý£¬Çø·ÖÈòÄêºÍ´óСÔÂ
*/
public final static int getMonthMaxDays(int year, int month)
{
switch (month)
{
case 4 :
case 6 :
case 9 :
case 11 :
return 30;
case 2 :
if (isLeapYear(year))
return 29;
else
return 28;
default : // case 1, 3, 5, 7, 8, 10, 12:
return 31;
}
}
/** @return »ñÈ¡µ±Ç°ÏµÍ³µÄ
year Öµ
*/
public final static int getCurrentYear()
{
return Calendar.getInstance().get(Calendar.YEAR);
}
/** »ñÈ¡µ±Ç°ÏµÍ³µÄ
month Öµ
@return 0 ~ 11 */
public final static int getCurrentMonth()
{
return Calendar.getInstance().get(Calendar.MONTH);
}
/** @return »ñÈ¡µ±Ç°ÏµÍ³µÄ
day Öµ
*/
public final static int getCurrentDay()
{
return Calendar.getInstance().get(Calendar.DAY_OF_MONTH);
}
/** @return 0: Sunday, 1: Monday, 2: Tuesday ... 6: Saturday */
public final static int getCurrentDayOfWeek()
{
return Calendar.getInstance().get(Calendar.DAY_OF_WEEK) - 1;
}
/** ½«ÊäÈëµÄÊý×Ö×é֯Ϊָ¶¨³¤¶ÈµÄ×Ö·û´®
*/
public final static String formatNumber(int nValue, int nDigit)
{
String sValue = nValue + "";
if (sValue.length() > nDigit)
return sValue.substring(sValue.length() - nDigit, sValue.length());
else
if (sValue.length() < nDigit)
for (int i = 0;
i < nDigit - sValue.length();
i++)
sValue = "0" + sValue;
return sValue;
}
}