java--TimeTools
import java.text.DateFormat;import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.Calendar;import java.util.Date;/** * Title: TimeTools.java Description: Copyright: Copyright (c) 2011 Company: * company * * @author channing * @date 2011-5-25 下午03:13:32 * @version 1.0 */public class TimeTools {public final static String defaultFormat = "yyyy-MM-dd HH:mm:ss";public enum DateFiled {YEAR,MONTH,DATE,HOUR,MINUTE}/** * * Title: getCurrentTime * Description: 格式化时间 为String 类型 * * @param format * @return */public static String getCurrentTime(String format) {SimpleDateFormat fmt = new SimpleDateFormat((format != null && !"".equals(format)) ? format : defaultFormat);return fmt.format(Calendar.getInstance().getTime());}/** * * Title: getCurrentDate * Description: 得到当前时间 * * @return */public static Date getCurrentDate() {return Calendar.getInstance().getTime();}/** * * Title: getMonthFirstDay * Description:得到本月的第一天 传入"yyyy-MM-dd" * * @param format * @return */public static String getMonthFirstDay(String format) {SimpleDateFormat fmt = new SimpleDateFormat(format);Calendar calendar = Calendar.getInstance();calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));return fmt.format(calendar.getTime());}/** * * Title: getCurrentMonthLisatDay * Description: 当月最后一天 * * @param format * @return */public static String getCurrentMonthLisatDay(String format) {SimpleDateFormat fmt = new SimpleDateFormat((format != null && !"".equals(format)) ? format : defaultFormat);Calendar cal = Calendar.getInstance();// 获取当前日期cal.set(Calendar.DAY_OF_MONTH, 1);// 设置为1号,当前日期既为本月第一天cal.add(Calendar.MONTH, 1);// 月增加1天cal.add(Calendar.DAY_OF_MONTH, -1);// 日期倒数一日,既得到本月最后一天return fmt.format(cal.getTime());}/** * * Title: fmtDate * Description: 格式化时间 date 格式为 fmt * * @param date * @param fmt * @return */public static String fmtDate(Date date, String fmt) {if (date == null)return null;SimpleDateFormat sdf = new SimpleDateFormat((fmt != null && !"".equals(fmt)) ? fmt : defaultFormat);return sdf.format(date);}/** * * Title: getTime * Description: 格式化时间 string time 格式为 fmt 返回 date 类型 * * @param time * @param fmt * @return */public static Date getTime(String time, String fmt) {SimpleDateFormat sdf = new SimpleDateFormat((fmt != null && !"".equals(fmt)) ? fmt : defaultFormat);try {return sdf.parse(time);} catch (ParseException e) {System.out.println("String to Date Error:" + e.getMessage());return null;}}/** * * Title: modifyDate * Description: 增加年、月、日 * * @param date * @param amount * @param filed * @return */public static Date modifyDate(Date date, int amount, DateFiled filed) {Calendar c = Calendar.getInstance();c.setTime(date);if (filed == DateFiled.YEAR) {c.add(Calendar.YEAR, amount);} else if (filed == DateFiled.MONTH) {c.add(Calendar.MONTH, amount);} else if (filed == DateFiled.DATE) {c.add(Calendar.DAY_OF_MONTH, amount);} else if (filed == DateFiled.HOUR) {c.add(Calendar.HOUR, amount);} else if (filed == DateFiled.MINUTE) {c.add(Calendar.MINUTE, amount);}return c.getTime();}/** * * Title: getBussinessNoByDate * Description: bussiness no Date * * @return */public static String getBussinessNoByDate() {SimpleDateFormat sdf = new SimpleDateFormat("yyMMddHHmmssSS");return sdf.format(getCurrentDate());}/** * * Title: getfmtDateStr * Description: * * @param dateStr * @param dateStrFmt * @param fmt * @return */public static String getfmtDateStr(String dateStr, String dateStrFmt,String fmt) {DateFormat df2 = (null == dateStrFmt) ? new SimpleDateFormat(defaultFormat) : new SimpleDateFormat(dateStrFmt);try {Date date2 = df2.parse(dateStr);return fmtDate(date2, fmt);} catch (ParseException e) {System.out.println("String time Error:" + e.getMessage());}return null;}public static void main(String[] args) {String time = TimeTools.getBussinessNoByDate();System.out.println(time);}}