package com.saicfc.saicifx3.util;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.Vector;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.eredlab.g4.ccl.util.G4Utils;
/**
* Date Utility Class This is used to convert Strings to Dates and Timestamps
*
*
* View Source
*
*
* @author Matt Raible Modified by
* Dan Kibler to correct time
* pattern. Minutes should be mm not MM (MM is month).
* @version $Revision: 1.1 $ $Date: 2006/08/09 02:37:35 $
*/
public class DateUtil {
// ~ Static fields/initializers
// =============================================
private static Log log = LogFactory.getLog(DateUtil.class);
private static String timePattern = "HH:mm";
public static int DATEFORMAT_GB = 0;
public static int DATEFORMAT_ISO = 1;
// ~ Methods
// ================================================================
/**
* This method generates a string representation of a date/time in the
* format you specify on input
*
* @param aMask
* the date pattern the string is in
* @param strDate
* a string representation of a date
* @return a converted Date object
* @see java.text.SimpleDateFormat
* @throws ParseException
*/
public static final Date convertStringToDate(String aMask, String strDate)
throws ParseException {
SimpleDateFormat df = null;
Date date = null;
df = new SimpleDateFormat(aMask);
if (log.isDebugEnabled()) {
log.debug("converting '" + strDate + "' to date with mask '" + aMask + "'");
}
try {
date = df.parse(strDate);
} catch (ParseException pe) {
// log.error("ParseException: " + pe);
throw new ParseException(pe.getMessage(), pe.getErrorOffset());
}
return (date);
}
/**
* format 'YYYY-MM-DD' to date()
*/
public static final Date convertStringToDateFromPage(String strDate) {
Date d = null;
try {
d = convertStringToDate("yyyy-MM-dd", strDate);
} catch (ParseException pe) {
;
}
return d;
}
/**
* 获取当前系统日期,并转换成String类型的
* @param strDate
* @return time
*/
public static final String DateToStringFormat() {
SimpleDateFormat sdFormat = new SimpleDateFormat("yyyy-MM-dd");
String time = sdFormat.format(new Date());
return time;
}
/**
* This method returns the current date time in the format: MM/dd/yyyy HH:MM
* a
*
* @param theTime
* the current time
* @return the current date/time
*/
public static String getTimeNow(Date theTime) {
return getDateTime(timePattern, theTime);
}
/**
* This method generates a string representation of a date's date/time in
* the format you specify on input
*
* @param aMask
* the date pattern the string is in
* @param aDate
* a date object
* @return a formatted string representation of the date
*
* @see java.text.SimpleDateFormat
*/
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);
}
/*
* @author:lt
*
* @date:2005-11-16 10:42
*
* @desc:获取时刻,用于数据存储
*/
public static Date getTime() {
Date time = null;
Calendar rightNow = Calendar.getInstance();
time = rightNow.getTime();
return time;
}
/*
* @author:zsz
*
* @date:2009-7-7
*
* @desc:获取时刻
*/
public final static String getRightNowLongStr() {
String value = null;
Calendar rightNow = Calendar.getInstance();
value = String.valueOf(rightNow.get(Calendar.YEAR))
+ String.valueOf((rightNow.get(Calendar.MONTH) + 1))
+ String.valueOf(rightNow.get(Calendar.DAY_OF_MONTH))
+ String.valueOf(rightNow.get(Calendar.HOUR_OF_DAY))
+ String.valueOf(rightNow.get(Calendar.MINUTE))
+ String.valueOf(rightNow.get(Calendar.SECOND))
+ String.valueOf(rightNow.get(Calendar.MILLISECOND));
return value;
}
/**
* 按指定格式,将日期式字符串转换为日期对象
*
* @param dateStr
* @param format
* @return Date
*/
public static Date formatStringToDate(String dateStr, String format) {
Date d = null;
try {
if (dateStr != null && !"".equals(dateStr)) {
SimpleDateFormat fmt = new SimpleDateFormat(format);
d = fmt.parse(dateStr);
}
} catch (ParseException e) {
e.printStackTrace();
}
return d;
}
/**
* 按指定格式,将日期对象转换为日期字符串
*
* @param date
* @param format
* @return String
*/
public static String formatDateToString(Date date, String format) {
SimpleDateFormat fmt = new SimpleDateFormat(format);
return fmt.format(date);
}
/**
* java.util.Date --> java.sql.Date
*/
public static java.sql.Date convertSqlDate(java.util.Date dateTime) {
if (dateTime == null)
return null;
else
return new java.sql.Date(dateTime.getTime());
}
/**
* java.sql.Date --> java.util.Date
*/
public static java.util.Date convertUtilDate(java.sql.Date dateTime) {
if (dateTime == null)
return null;
else
return new java.util.Date(dateTime.getTime());
}
/**
* 得到一个日期对象的字符串表示(yyyy-mm-dd hh:mm:ss).
* 当时、分、秒都为0时,表示的时间字符串中只含年、月、日形式(yyyy-mm-dd)
*
* @param date
* 日期对象
*
* @return date的字符串表示
*/
static public final String dateToString(java.util.Date date, boolean bHaveHour) {
Calendar cal = getStaticCalendars(date);// (java.sql.Timestamp)value;
if (!bHaveHour
|| (cal.get(Calendar.HOUR_OF_DAY) == 0 && cal.get(Calendar.MINUTE) == 0 && cal
.get(Calendar.SECOND) == 0)) {
int year = cal.get(Calendar.YEAR);// ((java.sql.Timestamp)value).getYear()
// + 1900;
int month = cal.get(Calendar.MONTH) + 1;// ((java.sql.Timestamp)value).getMonth()
// + 1;
int day = cal.get(Calendar.DAY_OF_MONTH);// ((java.sql.Timestamp)value).getDate();
return "" + year + (month < 10 ? "-0" : "-") + month + (day < 10 ? "-0" : "-") + day;
}
return date.toString();
}
/***************************************************************************
* 得到静态的日期.
**************************************************************************/
private static Calendar staticCal;
/**
* 传入一个日期型参数date,和想要获取几天前或几天后日期的参数day 返回一个类型为Date的日期
*/
public static final Date getBackDate(Date date, int day) throws ParseException {
Date backDate = new Date();
GregorianCalendar ca = new GregorianCalendar();
ca.setTime(date);
ca.add(Calendar.DAY_OF_MONTH, day);
backDate = ca.getTime();
return backDate;
}
/**
* 传入一个日期型参数date,和想要获取几月前或几月后日期的参数month 返回一个类型为Date的日期
*/
public static final Date getBackMonths(Date date, int month) throws ParseException {
Date backDate = new Date();
GregorianCalendar ca = new GregorianCalendar();
ca.setTime(date);
ca.add(Calendar.MONTH, month);
backDate = ca.getTime();
return backDate;
}
/***************************************************************************
* 日期运算.
**************************************************************************/
/**
* 求两个日期之间相差的天数(日期相减)
*
* @param date1
* 相减的第一的日期
* @param date2
* 相减的第二的日期
* @return date1 与 date2 之间相差的天数
*/
static public int diffDate(Calendar date1, Calendar date2) {
/*
* return (int)((
* toLongTime(date1.get(Calendar.YEAR),date1.get(Calendar.
* MONTH)+1,date1.get(Calendar.DATE)) -
* toLongTime(date2.get(Calendar.YEAR
* ),date2.get(Calendar.MONTH)+1,date2.get(Calendar.DATE)) )/
* (24*60*60*1000));
*/
return diffDate(date1.getTime(), date2.getTime());
}
static public int diffDate(java.util.Date date1, java.util.Date date2) {
return (int) ((date1.getTime() - date2.getTime()) / (24 * 60 * 60 * 1000));
}
// 计算年份
public static int diffYear(java.util.Date date) {
Calendar rightNow = Calendar.getInstance();
Calendar cal = getStaticCalendars(date);
int year = cal.get(Calendar.YEAR);
int month = cal.get(Calendar.MONTH);
int nowyear = rightNow.get(Calendar.YEAR);
int nowmonth = rightNow.get(Calendar.MONTH);
int diffyear = nowmonth >= month ? nowyear - year : nowyear - year - 1;
return diffyear;
}
public final static int[] getDateElements(java.util.Date date) {
if (date == null)
return null;
Calendar cal = getStaticCalendars(date);
int ymd[] = new int[7];
ymd[0] = cal.get(Calendar.YEAR);
ymd[1] = cal.get(Calendar.MONTH) + 1;
ymd[2] = cal.get(Calendar.DATE);
ymd[3] = cal.get(Calendar.HOUR_OF_DAY);
ymd[4] = cal.get(Calendar.MINUTE);
ymd[5] = cal.get(Calendar.SECOND);
return ymd;
}
static public int getDefaultHolidays(int year, int month) {
GregorianCalendar cal = new GregorianCalendar(year, month - 1, 1);
// System.out.println("cal="+cal.);
int x = 0;
for (int d = 0;; d++) {
int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK);
if (dayOfWeek == Calendar.SUNDAY || dayOfWeek == Calendar.SATURDAY)
x |= (1 << d);
// System.out.println("d="+(d+1)+",dayOfWeek="+dayOfWeek);
cal.add(Calendar.DAY_OF_YEAR, 1);
if (cal.get(Calendar.MONTH) + 1 != month)
break;
}
return x;
}
/**
* 求某一日期加或减(day为负数)后的日期
*/
static public java.util.Date incDate(java.util.Date date, int day) {
Calendar cal = new GregorianCalendar();
cal.setTime(date);
cal.add(Calendar.DAY_OF_YEAR, day);
return cal.getTime();
}
/**
* 求某一日期加或减(day为负数)后的日期
*/
static public void incYearMonthDay(int ymd[], int idx0, int day) {
java.util.Date date = incDate(toDate(ymd[idx0 + 0], ymd[idx0 + 1], ymd[idx0 + 2]), day);
ymd[idx0 + 0] = getDateYear(date);
ymd[idx0 + 1] = getDateMonth(date);
ymd[idx0 + 2] = getDateDay(date);
}
/**
* 从一个 java.util.Date
对象得到一个表示该日期的临时 Calendar
* 对象. 该对象只作为临时使用。
*
* @param date
* java.util.Date
对象
* @return 表示 date 的 Calendar
对象.
*/
public final static Calendar getStaticCalendars(java.util.Date date) {
if (staticCal == null)
staticCal = new GregorianCalendar();
if (date != null)
staticCal.setTime(date);
return staticCal;
// utcCal = new GregorianCalendar(TimeZone.getTimeZone("GMT"));
// defaultCenturyStart = staticCal.get(Calendar.YEAR) - 80;
}
/**
* 得到一个静态的 Calendar 临时对象
*
* @return 一个静态的 Calendar 临时对象
*/
public final static Calendar getStaticCalendars() {
return getStaticCalendars(null);
}
/**
* 得到一个静态的 给定日期和时间的 Calendar 临时对象,
*
* @param time
* 给定Calendar 临时对象表示的日期和时间
* @return 一个静态的给定日期和时间(long time) Calendar 临时对象
*/
public final static Calendar getStaticCalendars(long time) {
Calendar cal = getStaticCalendars(null);
if (cal != null)
cal.setTime(new java.util.Date(time));
return cal;
}
/***************************************************************************
* 日期clone
**************************************************************************/
/**
* 从一个 java.util.Date
对象clone一个相同类型的对象. 目前只考虑 java.util.Date,
* java.sql.Date , java.sql.Timestamp 三中类型
*
* @param date
* 被clone的 java.util.Date
对象
* @return clone的结果
*/
public final static java.util.Date cloneDate(java.util.Date date) {
long t = date.getTime();
if (date instanceof java.sql.Timestamp)
return new java.sql.Timestamp(t);
if (date instanceof java.sql.Date)
return new java.sql.Date(t);
if (date instanceof java.sql.Time)
return new java.sql.Time(t);
return new java.util.Date(t);
}
/***************************************************************************
* 日期到字符串转换.
**************************************************************************/
static public final String dateToString(java.util.Date date) {
return dateToString(date, false);
}
/** 将int表示成的日期取出并表示成字符串 */
public final static String toStringFromIntDate(int date, int baseYear) {
return "" + (baseYear + (date >> 9)) + '-' + ((date >> 5) & 0xf) + '-' + (date & 0x1f);
}
public final static String toStringFromIntTime(int time) {
return "" + (time / (60 * 6000)) + ':' + ((time % (60 * 6000)) / 6000) + ':'
+ ((time % 6000) / 100);
}
static public final String getSN(String split) {
String value = null;
Calendar rightNow = Calendar.getInstance();
value = String.valueOf(rightNow.get(Calendar.YEAR)) + split
+ String.valueOf((rightNow.get(Calendar.MONTH) + 1)) + split
+ String.valueOf(rightNow.get(Calendar.DAY_OF_MONTH));
return value;
}
/***************************************************************************
* 日期到long转换.
**************************************************************************/
/**
* 从给定的 year,mongth,day 得到时间的long值表示(a point in time that is time
* milliseconds after January 1, 1970 00:00:00 GMT).
*
* @param year
* 年
* @param month
* 月
* @param day
* 日
* @return 给定的 year,mongth,day 得到时间的long值表示
*/
public final static long toLongTime(int year, int month, int day) {
return toDate(year, month, day).getTime();
}
public final static long toLongTime(int year, int month, int day, int hour, int min, int sec) {
if (staticCal == null)
staticCal = new GregorianCalendar();
staticCal.clear();
staticCal.set(Calendar.YEAR, year);
staticCal.set(Calendar.MONTH, month - 1);
staticCal.set(Calendar.DAY_OF_MONTH, day); // day-1??
staticCal.set(Calendar.HOUR_OF_DAY, hour);
staticCal.set(Calendar.MINUTE, min);
staticCal.set(Calendar.SECOND, sec);
return staticCal.getTime().getTime();
}
/***************************************************************************
* Date产生.
**************************************************************************/
/**
* 从年月日得到一个Date对象
*/
public final static java.util.Date toDate(int year, int month, int day) {
if (staticCal == null)
staticCal = new GregorianCalendar();
staticCal.clear();
staticCal.set(Calendar.YEAR, year);
staticCal.set(Calendar.MONTH, month - 1);
staticCal.set(Calendar.DAY_OF_MONTH, day); // day-1??
return staticCal.getTime();// .getTime();
}
/**
* 从表示日期的字符串("2000-9-7")得到一个Date对象
*/
public final static java.util.Date toDate(String text) {
if (text == null)
return null;
text = text.trim();
if (text.length() == 0)
return null;
int r[] = new int[3];
if (!parseDate(text, r))
return null;
return toDate(r[0], r[1], r[2]);
}
/**
* 从表示日期的字符串("2000-9-7")得到一个Date对象
*/
@SuppressWarnings("deprecation")
public final static java.sql.Date toSqlDate(String text) {
if (text == null)
return null;
text = text.trim();
if (text.length() == 0)
return null;
int r[] = new int[3];
if (!parseDate(text, r))
return null;
@SuppressWarnings("unused")
java.sql.Date value = null;
@SuppressWarnings("unused")
Calendar rightNow = Calendar.getInstance();
int year = r[0] - 1900;
int month = r[1] - 1;
int day = r[2];
return new java.sql.Date(year, month, day);
}
/***************************************************************************
* Date数据类型的解析.
**************************************************************************/
public final static boolean parseDate(String text, int ret[]) {
int tmp[] = new int[6];
if (!parseDateTime(text + " 0:0:0", tmp))
return false;
if (ret != null)
for (int i = 0; i < 3; i++)
ret[i] = tmp[i];
return true;
}
/**
* "yyyy-mm-dd hh:mm:ss"
*/
public final static boolean parseDateTime(String text, int ret[]) {
// char [] sep = new char[] { '-','-',' ',':',':' };
if (ret == null)
ret = new int[6]; // tmp only
text = text.trim();
int start = 0;
try {
for (int i = 0; i < 5; i++) {
char sep = i == 2 ? ' ' : (i < 2 ? '-' : ':');
int p = text.indexOf(sep, start);
if (p <= start)
return false;
ret[i] = Integer.parseInt(text.substring(start, p));
start = p + 1;
}
ret[5] = Integer.parseInt(text.substring(start));
} catch (Exception ex) {
return false;
}
if (ret[0] >= 0 && ret[0] < 100) {
if (ret[0] >= 70)
ret[0] += 1900;
else
ret[0] += 2000;
}
return ret[0] >= 1900 && ret[0] < 2999 && ret[1] >= 1 && ret[1] <= 12 && ret[2] >= 1
&& ret[2] <= getDaysOfMonth(ret[0], ret[1]);
}
/***************************************************************************
* 日期各个分项获得.
**************************************************************************/
/**
* 求给定 某年、某月的最大天数.例如getDaysOfMonth(2000,1)范围31,getDaysOfMonth(2000,2)返回28
*
* @param year
* 年
* @param month
* 月
* @return 给定年、月的最大天数(1月返回31,2月返回28或29,3月返回31,...,12月返回31)
*/
static public int getDaysOfMonth(int year, int month) {
return (int) ((toLongTime(month == 12 ? (year + 1) : year, month == 12 ? 1 : (month + 1), 1) - toLongTime(
year, month, 1)) / (24 * 60 * 60 * 1000));
}
/**
* 从一个 java.util.Date
对象得到一个表示该日期的月份
*
* @param date
* java.util.Date
对象,从中取月份
* @return 日期 date 表示的月份
*/
public final static int getDateMonth(java.util.Date date) {
if (date == null)
return 0;
return getStaticCalendars(date).get(Calendar.MONTH) + 1;
}
/**
* 从一个 java.util.Date
对象得到一个表示该日期的日
*
* @param date
* java.util.Date
对象,从中取日
* @return 日期 date 表示的日
*/
public final static int getDateDay(java.util.Date date) {
if (date == null)
return 0;
return getStaticCalendars(date).get(Calendar.DATE);
}
/**
* 从一个 java.util.Date
对象得到一个表示该日期的年份
*
* @param date
* java.util.Date
对象,从中取年份
* @return 日期 date 表示的年份
*/
public final static int getDateYear(java.util.Date date) {
if (date == null)
return 0;
return getStaticCalendars(date).get(Calendar.YEAR);
}
public final static String getDateYear() {
Calendar rightNow = Calendar.getInstance();
return String.valueOf(rightNow.get(Calendar.YEAR));
}
public final static String getRightNow() {
String value = null;
Calendar rightNow = Calendar.getInstance();
value = String.valueOf(rightNow.get(Calendar.YEAR)) + "-"
+ String.valueOf((rightNow.get(Calendar.MONTH) + 1)) + "-"
+ String.valueOf(rightNow.get(Calendar.DAY_OF_MONTH));
return value;
}
public final static String getRightNowTime() {
String value = null;
Calendar rightNow = Calendar.getInstance();
value = String.valueOf(rightNow.get(Calendar.YEAR)) + "-"
+ String.valueOf((rightNow.get(Calendar.MONTH) + 1)) + "-"
+ String.valueOf(rightNow.get(Calendar.DAY_OF_MONTH)) + " "
+ String.valueOf(rightNow.get(Calendar.HOUR_OF_DAY)) + ":"
+ String.valueOf(rightNow.get(Calendar.MINUTE)) + ":"
+ String.valueOf(rightNow.get(Calendar.SECOND));
return value;
}
/*
* @author:zxc
*
* @date:2005-12-21 00:03
*
* @desc:获取时刻,用于数据存储
*/
public final static java.sql.Date getRightNowSql() {
return getRightNowSql(0, 0, 0);
}
public final static java.sql.Date getRightNowSql(int monthwarp) {
return getRightNowSql(0, monthwarp, -1);
}
@SuppressWarnings("deprecation")
public final static java.sql.Date getRightNowSql(int yearwarp, int monthwarp, int daywarp) {
@SuppressWarnings("unused")
java.sql.Date value = null;
Calendar rightNow = Calendar.getInstance();
int year = rightNow.get(Calendar.YEAR) - 1900 + yearwarp;
int month = rightNow.get(Calendar.MONTH) + monthwarp;
int day = rightNow.get(Calendar.DAY_OF_MONTH) + daywarp;
return new java.sql.Date(year, month, day);
}
public static String getToday4Web() {
String today = null;
Calendar rightNow = Calendar.getInstance();
today = rightNow.get(Calendar.YEAR) + "-" + (rightNow.get(Calendar.MONTH) + 1) + "-"
+ rightNow.get(Calendar.DAY_OF_MONTH);
return today;
}
public final static String get(String value, int type) {
String result = null;
if (value != null && !value.trim().equals(""))
result = value;
else {
Calendar rightNow = Calendar.getInstance();
if (type == Calendar.YEAR)
result = String.valueOf(rightNow.get(Calendar.YEAR));
else if (type == Calendar.MONTH)
result = String.valueOf((rightNow.get(Calendar.MONTH) + 1));
else if (type == Calendar.DATE)
result = String.valueOf(rightNow.get(Calendar.YEAR)) + "-"
+ String.valueOf((rightNow.get(Calendar.MONTH) + 1)) + "-"
+ String.valueOf(rightNow.get(Calendar.DAY_OF_MONTH));
}
return result;
}
static public String emitCalendar(String appYear, String appMonth, String appDay,
String onclick, String elementOnclick, int elementHeight,
int width) {
Calendar rightNow = Calendar.getInstance();
rightNow.set(Calendar.MONTH, (Integer.parseInt(appMonth) - 1));
rightNow.set(Calendar.YEAR, Integer.parseInt(appYear));
rightNow.set(Calendar.DAY_OF_MONTH, 1);
int index = 0;
int size = rightNow.getActualMaximum(Calendar.DAY_OF_MONTH);
int beginIndex = rightNow.get(Calendar.DAY_OF_WEEK) - 1;
beginIndex = (beginIndex == 0) ? 6 : beginIndex - 1;
int tempIndex = 0;
int counter = 0;
@SuppressWarnings("unused")
int subIndex = 0;
@SuppressWarnings("unused")
int subSize = 20;
StringBuffer sb = new StringBuffer();
sb
.append(" \n");
sb.append(" \n");
sb.append(" \n");
sb.append(" ");
sb.append(appYear);
sb.append(" 年 ");
sb.append(appMonth);
sb.append(" 月 \n");
sb.append(" \n");
sb.append(" \n");
sb.append(" \n");
sb
.append(" 一 \n");
sb
.append(" 二 \n");
sb
.append(" 三 \n");
sb
.append(" 四 \n");
sb
.append(" 五 \n");
sb
.append(" 六 \n");
sb
.append(" 日 \n");
for (; tempIndex < beginIndex; ++tempIndex) {
if (tempIndex == 0) {
sb.append(" \n");
}
sb.append(" \n");
else
sb
.append(" class=\"calendarRow\" valign=\"top\" > \n");
++counter;
}
for (; index < size; ++index) {
if (counter % 7 == 0) {
sb.append(" \n");
}
sb.append(" \n");
} else if (rightNow.get(Calendar.DAY_OF_WEEK) == 7
|| rightNow.get(Calendar.DAY_OF_WEEK) == 1) {
sb.append(" class=\"calendarLittleWeekend\" valign=\"top\" > \n");
} else {
sb.append(" class=\"calendarRow\" valign=\"top\" > \n");
}
sb.append(" ");
if (appDay.equals(String.valueOf(index + 1)))
sb.append(" \n");
sb.append(index + 1);
if (appDay.equals(String.valueOf(index + 1)))
sb.append(" ");
sb.append(" \n");
sb.append(" \n");
if (counter % 7 == 6) {
sb.append(" \n");
}
rightNow.add(Calendar.DAY_OF_MONTH, 1);
++counter;
}
while (counter % 7 != 0) {
sb.append(" \n");
else
sb
.append(" class=\"calendarRow\" valign=\"top\" > \n");
++counter;
}
sb.append(" \n");
sb.append("
\n");
return sb.toString();
}
static public Vector emitCalendar(String appYear, String appMonth, String appDay) {
Calendar rightNow = Calendar.getInstance();
rightNow.set(Calendar.MONTH, (Integer.parseInt(appMonth) - 1));
rightNow.set(Calendar.YEAR, Integer.parseInt(appYear));
rightNow.set(Calendar.DAY_OF_MONTH, Integer.parseInt(appDay));
int index = 0;
int beginIndex = Integer.parseInt(appDay);
int size = rightNow.getActualMaximum(Calendar.DAY_OF_MONTH);
@SuppressWarnings("unused")
int tempIndex = 0;
@SuppressWarnings("unused")
int counter = 0;
@SuppressWarnings("unused")
int subIndex = 0;
Vector> records = new Vector>();
HashMap sub = null;
for (index = beginIndex; index < size; ++index) {
sub = new HashMap();
sub.put("year", appYear);
sub.put("month", appMonth);
sub.put("day", String.valueOf(index));
records.add(sub);
}
// 要包括下月的开始的几天
if (beginIndex > 1) {
rightNow.add(Calendar.MONTH, 1);
appYear = String.valueOf(rightNow.get(Calendar.YEAR));
appMonth = String.valueOf(rightNow.get(Calendar.MONTH) + 1);
for (index = 1; index < beginIndex; ++index) {
sub = new HashMap();
sub.put("year", appYear);
sub.put("month", appMonth);
sub.put("day", String.valueOf(index));
records.add(sub);
}
}
return records;
}
static public String formatDate(String appYear, String appMonth, String appDay, int len) {
String appDate = appYear + "-";
if (len == 2)
appMonth = (appMonth.length() == 2) ? appMonth : "0" + appMonth;
if (len == 2)
appDay = (appDay.length() == 2) ? appDay : "0" + appDay;
appDate = appDate + appMonth + "-" + appDay;
return appDate;
}
public final static String getDayOfWeek(int text) {
String map[] = { "零", "日", "一", "二", "三", "四", "五", "六" };
if (text > 0 && text < map.length)
return map[text];
else
return "";
}
public final static String emitDate(String value, boolean isShow) {
if ((value == null) || (value.trim().equals("")) || (value.trim().equals("1900-01-01"))) {
if (isShow)
return " ";
else
return "";
} else {
return value;
}
}
public final static HashMap parseDateTime(Calendar rightNow) {
HashMap result = new HashMap();
result.put("date", String.valueOf(rightNow.get(Calendar.YEAR)) + "-"
+ String.valueOf(rightNow.get(Calendar.MONTH) + 1) + "-"
+ String.valueOf(rightNow.get(Calendar.DAY_OF_MONTH)));
@SuppressWarnings("unused")
String hour = null;
@SuppressWarnings("unused")
String minute = null;
@SuppressWarnings("unused")
String second = null;
result.put("time", String.valueOf(rightNow.get(Calendar.HOUR_OF_DAY)) + ":"
+ String.valueOf(rightNow.get(Calendar.MINUTE)) + ":"
+ String.valueOf(rightNow.get(Calendar.SECOND)));
result.put("hour", String.valueOf(rightNow.get(Calendar.HOUR_OF_DAY)));
result.put("minute", String.valueOf(rightNow.get(Calendar.MINUTE)));
result.put("second", String.valueOf(rightNow.get(Calendar.SECOND)));
return result;
}
/***************************************************************************
* get 时间间隔的所有年、月
*/
@SuppressWarnings("unchecked")
public final static Vector getTimeseries(int startYear, int startMonth, int endYear,
int endMonth) {
Vector> result = new Vector();
Calendar rightNow = Calendar.getInstance();
rightNow.set(Calendar.YEAR, startYear);
rightNow.set(Calendar.MONTH, startMonth - 1);
Vector term = null;
@SuppressWarnings("unused")
int year = startYear;
int month = startMonth;
int max = endYear * 100 + endMonth;
int value = 0;
while (value <= max) {
term = new Vector();
term.add(String.valueOf(rightNow.get(Calendar.YEAR)));
term.add(String.valueOf(rightNow.get(Calendar.MONTH) + 1));
result.add(term);
rightNow.add(Calendar.MONTH, 1);
year = rightNow.get(Calendar.YEAR);
month = rightNow.get(Calendar.MONTH) + 1;
value = rightNow.get(Calendar.YEAR) * 100 + month;
}
return result;
}
public final static String toDay(int hour) {
String value = "0";
if (hour > 0) {
value = String.valueOf(hour / 8);
hour = hour % 8;
if (hour > 0) {
if (hour == 2)
value = value + ".25";
else if (hour == 4)
value = value + ".5";
else if (hour == 6)
value = value + ".75";
else
value = String.valueOf(hour / 8 + 1);
}
}
return value;
}
/* 给对应的时间字段增加一个value值 */
public final static String add(String appYear, String appMonth, String appDate, int type,
int value) {
Calendar rightNow = Calendar.getInstance();
rightNow.set(Calendar.YEAR, Integer.parseInt(appYear));
rightNow.set(Calendar.MONTH, Integer.parseInt(appMonth) - 1);
rightNow.set(Calendar.DAY_OF_MONTH, Integer.parseInt(appDate));
rightNow.add(type, value);
return String.valueOf(rightNow.get(Calendar.YEAR)) + "-"
+ String.valueOf(rightNow.get(Calendar.MONTH) + 1) + "-"
+ String.valueOf(rightNow.get(Calendar.DAY_OF_MONTH));
}
public final static String toStringDate(Calendar rightNow) {
return String.valueOf(rightNow.get(Calendar.YEAR)) + "-"
+ String.valueOf(rightNow.get(Calendar.MONTH) + 1) + "-"
+ String.valueOf(rightNow.get(Calendar.DAY_OF_MONTH));
}
/**
* 计算一个考勤周期的开始日期和结束日期
*/
public final static HashMap getTimecardCyc(String appYear, String appMonth, String calMode,
String beginDay) {
HashMap term = new HashMap();
calMode = (calMode == null) ? "1" : calMode;
Calendar rightNow = Calendar.getInstance();
rightNow.set(Calendar.YEAR, Integer.parseInt(appYear));
rightNow.set(Calendar.MONTH, (Integer.parseInt(appMonth) - 1));
if (calMode.equals("1")) {
rightNow.add(Calendar.MONTH, -1);
}
Vector days = emitCalendar(String.valueOf(rightNow.get(Calendar.YEAR)), String
.valueOf(rightNow.get(Calendar.MONTH) + 1), beginDay);
HashMap sub = (HashMap) days.elementAt(0);
String startDate = (String) sub.get("year") + "-" + (String) sub.get("month") + "-"
+ (String) sub.get("day");
sub = (HashMap) days.elementAt(days.size() - 1);
String endDate = (String) sub.get("year") + "-" + (String) sub.get("month") + "-"
+ (String) sub.get("day");
term.put("startDate", startDate);
term.put("endDate", endDate);
return term;
}
/**
* 找一个日期的对日
*/
public static Date getNextMatchDate(Date start, Date last, long frequence) {
int freq = (int) frequence;
Calendar startDate = getCalendar(start);
Calendar lastdate = getCalendar(last);
lastdate.set(Calendar.DATE, 1);
lastdate.add(Calendar.MONTH, freq);
int startDay = startDate.get(Calendar.DATE);
int maxDay = DateUtil.getDaysInMonth(lastdate.get(Calendar.YEAR), lastdate
.get(Calendar.MONTH)
+ freq);
if (maxDay > startDay) {
lastdate.set(Calendar.DATE, startDay);
} else {
lastdate.set(Calendar.DATE, maxDay);
}
return lastdate.getTime();
}
/**
* 找一个日期的对日
*/
public static long getMatchDate(long start, long last, long frequence) {
Date startdate = DateUtil.getDateFromLong(start);
Date lastdate = DateUtil.getDateFromLong(last);
Date matchdate = getNextMatchDate(startdate, lastdate, frequence);
return DateUtil.getLongFromDate(matchdate);
}
public static int getDaysInMonth(int year, int mon) {
java.util.GregorianCalendar date = new java.util.GregorianCalendar(year, mon, 1);
date.add(Calendar.DATE, -1);
return (date.get(Calendar.DAY_OF_MONTH));
}
public static long getLastDay(long today) {
try {
Date date = getDateFromLong(today);
GregorianCalendar gc = new GregorianCalendar();
gc.setTime(date);
gc.add(5, -1);
gc.set(gc.get(Calendar.YEAR), gc.get(Calendar.MONTH), gc.get(Calendar.DATE));
return getLongFromDate(gc.getTime());
} catch (Exception e) {
e.printStackTrace();
}
return 0;
}
/**
* 根据string 类型日期返回date类
*/
public static Date getDateFromLong(long day) {
try {
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
Date date = sdf.parse(day + "");
return date;
} catch (Exception e) {
e.printStackTrace();
}
return new Date();
}
/*
* 根据date类日期返回yyyymmdd类型的日期
*
*/
public static long getLongFromDate(Date date) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
GregorianCalendar gc = new GregorianCalendar();
gc.setTime(date);
gc.set(gc.get(Calendar.YEAR), gc.get(Calendar.MONTH), gc.get(Calendar.DATE));
String dateString = sdf.format(gc.getTime());
Long l = Long.parseLong(dateString);
return l;
}
public static int getDaysBetween(long beginDate, long endDate) {
try {
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
Date bDate = sdf.parse(beginDate + "");
Date eDate = sdf.parse(endDate + "");
Calendar d1 = new GregorianCalendar();
d1.setTime(bDate);
Calendar d2 = new GregorianCalendar();
d2.setTime(eDate);
int days = d2.get(Calendar.DAY_OF_YEAR) - d1.get(Calendar.DAY_OF_YEAR);
int y2 = d2.get(Calendar.YEAR);
if (d1.get(Calendar.YEAR) != y2)
{
d1 = (Calendar) d1.clone();
do {
days += d1.getActualMaximum(Calendar.DAY_OF_YEAR);// 得到当年的实际天数
d1.add(Calendar.YEAR, 1);
} while (d1.get(Calendar.YEAR) != y2);
}
return days;
} catch (Exception e) {
e.printStackTrace();
}
return 0;
}
/*
* 把Date转化成Calendar
*/
public static Calendar getCalendar(Date date) {
Calendar cal = new GregorianCalendar();
cal.setTime(date);
return cal;
}
public static Long getLongToday() {
Date todayDate = new Date();
return getLongFromDate(todayDate);
}
public static String getDateStr(String pattern) {
return formatDateToString(new Date(), pattern);
}
/**
* 将日期字符串转化成日期(自动判断格式24H)
* @param strDate
* @return
* @throws ParseException
*/
public static Date parseStringToDate(String strDate) {
Date d = null;
String separator = String.valueOf(strDate.charAt(4));
String pattern = "yyyyMMdd";
if (!separator.matches("\\d*")) {
pattern = "yyyy" + separator + "MM" + separator + "dd";
if (strDate.length() < 10) {
pattern = "yyyy" + separator + "M" + separator + "d";
}
} else {
if (strDate.length() < 8) {
pattern = "yyyyMd";
}
}
pattern += " HH:mm:ss.SSS";
d = formatStringToDate(strDate, pattern.substring(0, Math.min(pattern.length(), strDate
.length())));
return d;
}
/**
* 获得系统当前日期并格式化成yyyy-MM-dd HH:mm:ss---->date
* @return
*/
public static String getNewDate() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String date = sdf.format(new Date());
return date;
}
/**
* 把日期格式化为yyyy-mm-dd格式的数据
* 如:20130412------>2013-04-12
* @param StringDate
* @return
*/
public static String formatDate(String StringDate) {
if (G4Utils.isNotEmpty(StringDate)) {
if (StringDate.indexOf("-") <= 0) {
StringDate = StringDate.substring(0, 4) + "-" + StringDate.substring(4, 6) + "-"
+ StringDate.substring(6, StringDate.length());
}
}
return StringDate;
}
/**
* 把日期格式化为yyyymmdd格式的数据
* @return
*/
public static String dateFormat() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
String date = sdf.format(new Date());
return date;
}
}