?? dateutils.java
字號:
package anni.core.utils;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
/**
* 日期工具類.
*
* @author Lingo
*/
public class DateUtils {
/** * 年-月-日,默認日期格式. */
private static SimpleDateFormat dateFormat = new SimpleDateFormat(
"yyyy-MM-dd");
/** * 緊縮日期格式. */
private static SimpleDateFormat customerFormat = new SimpleDateFormat(
"yyyyMMdd");
/** * protected構造方法. */
protected DateUtils() {
}
/**
* 使用默認格式將日期轉換成字符串.
*
* @param date 需要轉換的日期
* @return 轉換后的字符串,如果發生異常,返回空字符串
*/
public static String date2Str(Date date) {
if (date == null) {
return "";
} else {
return dateFormat.format(date);
}
}
/**
* 使用自定義格式,將日期轉換成字符串.
*
* @param date 需要轉換的日期
* @param pattern 自定義格式
* @return 轉換后的字符串,如果發生異常,返回空字符串
*/
public static String date2Str(Date date, String pattern) {
if (date == null) {
return "";
}
try {
customerFormat.applyPattern(pattern);
} catch (IllegalArgumentException ex) {
return "";
}
return customerFormat.format(date);
}
/**
* 使用默認格式,將字符串轉換成日期.
*
* @param str 需要轉換的字符串
* @return 轉換后的日期,如果發生異常,返回null
*/
public static Date str2Date(String str) {
try {
return dateFormat.parse(str);
} catch (Exception ex) {
return null;
}
}
/**
* 返回上個月的最后一天.
*
* @return 日期
*/
public static Date getLastMonthLastDate() {
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.MONTH, -1);
int max = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
calendar.set(Calendar.DAY_OF_MONTH, max);
return calendar.getTime();
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -