- 博客(0)
- 资源 (3)
空空如也
JAVA时间处理类
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import org.apache.commons.lang.StringUtils;
/**
* 日付に関するユーティリティクラス
*
*/
public class DateUtil {
/**
* 年月日時のフォーマット(年月日)
*/
public static final String YYYYMMDD = "yyyyMMdd";
/**
* 年月日時のスラッシュ入りフォーマット(年/月/日)
*/
public static final String YYYYMMDD_SLASH = "yyyy/MM/dd";
/**
* 年月のスラッシュ入りフォーマット(年/月)
*/
public static final String YYYYMM_SLASH = "yyyy/MM";
/**
* 年月日時の中間線入りフォーマット(年-月-日)
*/
public static final String YYYYMMDD_MID_LINE = "yyyy-MM-dd";
/**
* 年月のスラッシュ入りフォーマット(年-月)
*/
public static final String YYYYMM_MID_LINE = "yyyy-MM";
/**
* 年月日時分秒ミリ秒のフォーマット(年月日時分秒ミリ秒)
*/
public static final String YYYYMMDDHHMMSSSSS = "yyyyMMddHHmmssSSS";
/**
* 年月日時分秒ミリ秒のフォーマット(年-月-日 時:分:秒)
*/
public static final String YYYYMMDDHHMMSS_MID_LINE = "yyyy-MM-dd HH:mm:ss";
/**
* 年月日時分秒ミリ秒のフォーマット(年/月/日 時:分:秒)
*/
public static final String YYYYMMDDHHMMSS_SLASH = "yyyy/MM/dd HH:mm:ss";
/**
* コンストラクタ
*/
private DateUtil() {
// 何もしない
}
/**
* システム日時を取得する
*
* @return システム日時
*/
public static Date getSystemDate() {
return new Date();
}
/**
* システム日時をTimestampオブジェクトで返却する
*
* @return Timestampオブジェクト
*/
public static Timestamp getTimestamp() {
return toTimestamp(getSystemDate());
}
/**
* 指定するフォーマットのシステム日時を取得する
* (2008年3月22日15時30分 → 2008-03-22 (format = "yyyy-MM-dd"))
* @param format
* 指定するフォーマット
* @return システム日時
*/
public static String getCurrentTime(String format) {
return formatDateToStr(getSystemDate(), format);
}
/**
* 現在の月を取得する
*
* @return 現在の月
*/
public static String getCurentMonth() {
SimpleDateFormat format = new SimpleDateFormat("MM");
return format.format(getSystemDate());
}
/**
* Nヶ月後の当月の最初の日を取得する
*
* @param n
* Nヶ月後
* @return Nヶ月後の当月の最初の日
*/
public static Calendar getMonthFirstDay(int n) {
Calendar cal = Calendar.getInstance();
cal.set(Calendar.DAY_OF_MONTH, 1);
cal.add(Calendar.MONTH, n);
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
return cal;
}
/**
* Nヶ月後の当月の最後の日を取得する
*
* @param n
* Nヶ月後
* @return Nヶ月後の当月の最後の日
*/
public static Calendar getMonthlastDay(int n) {
Calendar cal = Calendar.getInstance();
cal.set(Calendar.DAY_OF_MONTH, 1);
cal.add(Calendar.MONTH, n + 1);
cal.add(Calendar.DATE, -1);
cal.set(Calendar.HOUR_OF_DAY, 23);
cal.set(Calendar.MINUTE, 59);
cal.set(Calendar.SECOND, 59);
return cal;
}
/**
* 获得与某日期相隔几天的日期
*
* @param date
* 指定する日付
* @param addCount
* 離れた日数
* @return 処理後の日付
*/
public static Date addDay(Date date, int addCount) {
if (date == null) {
return null;
}
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.DATE, addCount);
return calendar.getTime();
}
/**
* 获得与某日期相隔几天的日期
*
* @param date
* 指定する日付
* @param addCount
* 離れた月数
* @return 処理後の日付
*/
public static Date addMonth(Date date, int addCount) {
if (date == null) {
return null;
}
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.MONTH, addCount);
return calendar.getTime();
}
/**
* DateオブジェクトをTimestampオブジェクトに変換する。
*
* @param date
* Dateオブジェクト
* @return Timestamp オブジェクト
*/
public static Timestamp toTimestamp(Date date) {
if (date == null) {
return null;
}
return new Timestamp(date.getTime());
}
/**
* Dateオブジェクトを指定の形式でフォーマットする。
* 例:'yyyy年M月d日 h時m分'
* @param date
* Dateオブジェクト
* @param dateFormat
* Dateフォーマット
* @return フォーマットされた文字列
*/
public static String formatDateToStr(Date date, String dateFormat) {
if (date == null) {
return "";
}
return getFormatter(dateFormat).format(date);
}
/**
* 将某个日期格式字符串转换成另一指定格式日期字符串 例:传入"2009/1/15 16:58:00","yyyy/MM/dd
* hh:mm:ss","yyyy年M月d日 a h时m分s秒" 则返回 “2009年1月15日 下午 4时58分0秒”
*
* @param dateStr
* 指定する日付
* @param inDateFormat
* 传入日期字符串的日期格式
* @param outDateFormat
* 传出日期字符串的日期格式
* @return 返回指定格式的日期字符串
*/
public static String formatDateToStr(String dateStr, String inDateFormat, String outDateFormat) {
SimpleDateFormat simpleDateFormat = null;
String str = "";
Date date = null;
try {
simpleDateFormat = getFormatter(inDateFormat);
date = simpleDateFormat.parse(dateStr);
simpleDateFormat.applyPattern(outDateFormat);
return simpleDateFormat.format(date);
} catch (Exception e) {
return str;
}
}
/**
* 将字符串转化为格式为formmat 的日期格式返回
*
* @param date
* 待转换的日期
* @param formmat
* 转换的format
* @return 转换后的日期
*/
public static Date stringToFormatDate(String date, String formmat) {
try{
return getFormatter(formmat).parse(date);
}catch(Exception e){
return null;
}
}
/**
* 日付の正確性チェック
*
* @param
* dateStr 日期パラメータ
* @param
* format 日期フォーマット
* @return 正しい:true 違い:false
*/
public static boolean isValidDate (String dateStr, String format) {
try {
getFormatter(format).parse(dateStr);
return true;
} catch (Exception e) {
return false;
}
}
/**
* タイムの有効性チェック
*
* @param dateStr
* タイム
* @return 有効:true 無効:false
*/
public static boolean isValidTime (String dateStr) {
boolean reval = false;
if (!StringUtils.isBlank(dateStr)) {
if (dateStr.length() == 4) {
try {
Integer.parseInt(dateStr);
int hour = Integer.parseInt(dateStr.substring(0, 2));
int min = Integer.parseInt(dateStr.substring(2, 4));
if (hour = 0 && min = 0) {
return true;
}
} catch (Exception e) {
}
}
}
return reval;
}
/**
* 根据输入的日期计算年龄
*
* @param birthday 出生日期 格式:yyyy-MM-dd
* @return String 年龄(周岁年龄)
* */
public static String getAge(String birthday){
if (StringUtils.isBlank(birthday)) {
return "0";
}
Calendar birthdays = Calendar.getInstance();
try {
birthdays.setTime(getFormatter(YYYYMMDD_MID_LINE).parse(birthday));
} catch (ParseException e) {
return "0";
}
Calendar today = new GregorianCalendar();
int age = today.get(Calendar.YEAR) - birthdays.get(Calendar.YEAR);
birthdays.add(Calendar.YEAR, age);
if (today.before(birthdays)) {
age--;
}
return String.valueOf(age);
}
/**
* 二つの日付の差を計算する
*@param date1 計算日付1
*@param date2 計算日付2
* @return int 日付の差
*/
public static int getDateDiffDays(Date date1 ,Date date2) {
int retValue = 0;
if (date1 != null && date2 != null) {
long dateDiff = date1.getTime() - date2.getTime();
retValue = (int)(dateDiff/(24 * 60 * 60 * 1000));
}
return retValue;
}
/**
* 得到具体某个月的天数
* @throws ParseException
*/
public static int getDaysOfMonth(String year, String month) throws ParseException{
String datestr = year + "/" + month;
Calendar cal = Calendar.getInstance();
SimpleDateFormat dateFormat = getFormatter(YYYYMM_SLASH);
cal.setTime(dateFormat.parse(datestr));
int daysOfMonth = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
return daysOfMonth;
}
/**
* 获取一个简单的日期格式化对象
* @param format
* @return
*/
private static SimpleDateFormat getFormatter(String format) {
return new SimpleDateFormat(format);
}
}
2013-08-01
Struts中文手册.CHM
Struts中文手册
Struts实现的重点在C(Controller),包括ActionServlet/RequestProcessor和我们定制的Action,也为V(View)提供了一系列定制标签(Custom Tag)。但Struts几乎没有涉及M(Model),所以Struts可以采用JAVA实现的任何形式的商业逻辑。
2009-11-22
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人