import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
/**
* Title: DataFromatTool.java
* Description: 时间格式化类
* Copyright: Copyright (c) 2011
* Company: company
*
* @author channing
* @date 2011-2-12 下午02:18:28
* @version 1.0
*/
public class DataFromatTool {
public final static String defaultFormat = "yyyy-MM-dd HH:mm:ss";
/**
*
* Title: getCurrentTime
* Description:当前时间格式化为字符串
*
* @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:当前时间格式化为Date类型
*
* @return
*/
public static Date getCurrentDate() {
return Calendar.getInstance().getTime();
}
/**
*
* Title: getMonthFirstDay
* Description: 当月第一天格式化为字符串类型
*
* @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: xxDate时间类型用 xxfmt 格式化为字符串类型
*
* @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: XX字符串时间用xxfmt 格式化为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.print("String to Date Error:" + e.getMessage());
return null;
}
}
public enum DateFiled{
YEAR,
MONTH,
DATE,
HOUR,
MINUTE
}
/**
*
* Title: modifyDate
* Description: 年、月、日、时、分不同时间段得到Date时间类型
* @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:
* @return
*/
public static String getBussinessNoByDate(){
SimpleDateFormat sdf = new SimpleDateFormat("yyMMddHHmmssSS");
return sdf.format(getCurrentDate());
}
}
DataFromatTool
最新推荐文章于 2022-02-25 23:47:59 发布