计算器的代码已从hi上传给你了..
不知道你在要什么.给你个日期处理工具吧 要什么可以具体一些哦 java代码范围太广了.有不懂的再问我.
public class DateUtil extends BaseUtil {
private static final long serialVersionUID = 2990796742264152931L;
private static String datePattern = "yyyy-MM-dd";
private static String timePattern = "HH:mm";
/**
* 在页面中显示的日期格式yyyy-MM-dd
*
* @Title getDatePattern
* @Description
* @return
* @author l
* @date 2011-11-2 上午10:27:57
*/
public static String getDatePattern() {
return datePattern;
}
/**
* 根据日期格式,返回日期按datePattern格式转换后的字符串
*
* @Title getDate
* @Description
* @param aDate
* @return
* @author l
* @date 2011-11-2 上午10:27:57
*/
public static final String getDate(Date aDate) {
SimpleDateFormat df = null;
String returnValue = "";
if (aDate != null) {
df = new SimpleDateFormat(datePattern);
returnValue = df.format(aDate);
}
return (returnValue);
}
public static void main(String[] args) {
System.out.println(getDate("MMdd"));
}
/**
* 指定日期格式生成当前日期字符串
*
* @Title getDate
* @Description
* @param pattern
* @return
* @author l
* @date 2011-11-2 上午10:27:57
*/
public static final String getDate(String pattern) {
Date date = new Date();
return getDate(date, pattern);
}
/**
* 指定日期格式生成指定日期的字符串
*
* @Title getDate
* @Description
* @param date
* @param pattern
* @return
* @authorl
* @date 2011-11-2 上午10:27:57
*/
public static final String getDate(Date date, String pattern) {
SimpleDateFormat df = null;
String returnValue = "";
if (date != null) {
df = new SimpleDateFormat(pattern);
returnValue = df.format(date);
}
return (returnValue);
}
/**
* 根据日期字符串和指定日期格式返回日期
*
* @Title getDate
* @Description
* @param dateString
* @param pattern
* @return
* @author l
* @date Mar 11, 2011 3:37:14 PM
*/
public static Date getDate(String dateString, String pattern) {
SimpleDateFormat df = null;
Date date = new Date();
if (dateString != null) {
try {
df = new SimpleDateFormat(pattern);
if (dateString != null && !dateString.equals(""))
date = df.parse(dateString);
} catch (Exception e) {
e.printStackTrace();
}
}
return date;
}
/**
* 按照日期格式,将字符串解析为日期对象
*
* @Title convertStringToDate
* @Description
* @param aMask
* @param strDate
* @return
* @author l
* @date 2011-11-2 上午10:27:57
*/
public static final Date convertStringToDate(String aMask, String strDate) {
SimpleDateFormat df = null;
Date date = null;
df = new SimpleDateFormat(aMask);
try {
date = df.parse(strDate);
} catch (ParseException pe) {
log.error(pe);
}
return (date);
}
/**
* 获取当前日期
*
* @Title getTimeNow
* @Description
* @param theTime
* @return
* @author l
* @date 2011-11-2 上午10:27:57
*/
public static String getTimeNow(Date theTime) {
return getDateTime(timePattern, theTime);
}
/**
* 获取当前日期的Calendar
*
* @Title getToday
* @Description
* @return
* @throws ParseException
* @author l
* @date 2011-11-2 上午10:27:57
*/
public static Calendar getToday() throws ParseException {
Date today = new Date();
SimpleDateFormat df = new SimpleDateFormat(datePattern);
String todayAsString = df.format(today);
Calendar cal = new GregorianCalendar();
cal.setTime(convertStringToDate(todayAsString));
return cal;
}
/**
* 返回指定格式的日期字符串
*
* @Title getDateTime
* @Description
* @param aMask
* @param aDate
* @return
* @author l
* @date 2011-11-2 上午10:27:57
*/
public static final String getDateTime(String aMask, Date aDate) {
SimpleDateFormat df = null;
String returnValue = "";
if (aDate == null) {
log.error("aDate is null!");
} else {
df = new SimpleDateFormat(aMask);
returnValue = df.format(aDate);
}
return (returnValue);
}
/**
* 根据日期格式,返回日期按datePattern格式转换后的字符串
*
* @Title convertDateToString
* @Description
* @param aDate
* @return
* @authorl
* @date Mar 11, 2011 3:42:44 PM
*/
public static final String convertDateToString(Date aDate) {
return getDateTime(datePattern, aDate);
}
/**
* 按照日期格式,将字符串解析为日期对象
*
* @Title convertStringToDate
* @Description
* @param strDate
* @return
* @author l
* @date 2011-11-2 上午10:27:57
*/
public static Date convertStringToDate(String strDate) {
Date aDate = null;
aDate = convertStringToDate(datePattern, strDate);
return aDate;
}
public static String getYear() {
Date date = new Date();
return getDate(date, "yyyy");
}
public static String getMonth() {
Date date = new Date();
return getDate(date, "MM");
}
public static String getDay() {
Date date = new Date();
return getDate(date, "dd");
}
/**
* 返回小时
*
* @Title getHour
* @Description
* @param date
* @return
* @author l
* @date 2011-11-2 上午10:27:57
*/
public static int getHour(java.util.Date date) {
java.util.Calendar c = java.util.Calendar.getInstance();
c.setTime(date);
return c.get(java.util.Calendar.HOUR_OF_DAY);
}
/**
* 返回分钟
*
* @Title getMinute
* @Description
* @param date
* @return
* @author l
* @date 2011-11-2 上午10:27:57
*/
public static int getMinute(java.util.Date date) {
java.util.Calendar c = java.util.Calendar.getInstance();
c.setTime(date);
return c.get(java.util.Calendar.MINUTE);
}
/**
* 返回秒钟
*
* @Title getSecond
* @Description
* @param date
* @return
* @author l
* @date 2011-11-2 上午10:27:57
*/
public static int getSecond(java.util.Date date) {
java.util.Calendar c = java.util.Calendar.getInstance();
c.setTime(date);
return c.get(java.util.Calendar.SECOND);
}
/**
* 返回毫秒
*
* @Title getMillis
* @Description
* @param date
* @return
* @author l
* @date Mar 11, 2011 3:42:03 PM
*/
public static long getMillis(java.util.Date date) {
java.util.Calendar c = java.util.Calendar.getInstance();
c.setTime(date);
return c.getTimeInMillis();
}
/**
* 计算指定日期几天后的日期
*
* @Title addDate
* @Description
* @param date
* @param day
* @return
* @author l
* @date 2011-11-2 上午10:27:57
*/
public static java.util.Date addDate(java.util.Date date, int day) {
java.util.Calendar c = java.util.Calendar.getInstance();
c.setTimeInMillis(getMillis(date) + ((long) day) * 24 * 3600 * 1000);
return c.getTime();
}
/**
* 日期相减(生成日)
*
* @Title diffDate
* @Description
* @param date
* @param date1
* @return
* @author l
* @date 2011-11-2 上午10:27:57
*/
public static int diffDate(java.util.Date date, java.util.Date date1) {
return (int) ((getMillis(date) - getMillis(date1)) / (24 * 3600 * 1000));
}
/**
* 日期相减(生成小时)
*
* @Title diffDate
* @Description
* @param date
* @param date1
* @return
* @author l
* @date 2011-11-2 上午10:27:57
*/
public static int diffDateToHour(Date date, Date date1) {
return (int) ((getMillis(date) - getMillis(date1)) / (1000 * 60 * 60));
}
/**
* 转换时间字符串
*
* @Title parseDateStr
* @Description
* @param dateStr
* @return
* @author l
* @date 2011-11-2 上午10:27:57
*/
public static Date parseDateStr(String dateStr) {
try {
dateStr = dateStr.trim();
if (dateStr.matches("\\d{4}")) {// 2008
dateStr += "-01-01 00:00:00";
} else if (dateStr.matches("\\d{4}-\\d{1,2}")) {// 2008-09
dateStr += "-01 00:00:00";
} else if (dateStr.matches("\\d{4}-\\d{1,2}-\\d{1,2}")) {// 2008-09-12
dateStr += " 00:00:00";
} else if (dateStr.matches("\\d{4}-\\d{1,2}-\\d{1,2} d{1,2}:d{1,2}")) {// 2008-09-12
// 10:20
dateStr += ":00";
} else if (dateStr.matches("\\d{4}-\\d{1,2}-\\d{1,2} d{1,2}:d{1,2}:d{1,2}")) {// 2008-09-12
// 10:20:20
//
}
return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(dateStr);
} catch (Exception e) {
log.error(e);
}
return null;
}
}