날짜 처리 클래스 DateUtil

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("
"); sb.append("
"); sb.append("
"); sb.append("
"); sb.append("
"); sb .append("
"); sb .append("
"); sb .append("
"); sb .append("
"); sb .append("
"); sb .append("
"); sb .append("
"); for (; tempIndex < beginIndex; ++tempIndex) { if (tempIndex == 0) { sb.append("
"); } sb.append("
"); else sb .append(" class=\"calendarRow\" valign=\"top\" > 
"); ++counter; } for (; index < size; ++index) { if (counter % 7 == 0) { sb.append("
"); } sb.append("
"); if (counter % 7 == 6) { sb.append("
"); } rightNow.add(Calendar.DAY_OF_MONTH, 1); ++counter; } while (counter % 7 != 0) { sb.append("
"); else sb .append(" class=\"calendarRow\" valign=\"top\" > 
"); ++counter; } sb.append("
"); sb.append("

"); sb.append(" "); sb.append(appYear); sb.append(" 년. "); sb.append(appMonth); sb.append("  월.
"); sb.append("
 

"); } else if (rightNow.get(Calendar.DAY_OF_WEEK) == 7 || rightNow.get(Calendar.DAY_OF_WEEK) == 1) { sb.append(" class=\"calendarLittleWeekend\" valign=\"top\" >
"); } else { sb.append(" class=\"calendarRow\" valign=\"top\" >
"); } sb.append(" "); if (appDay.equals(String.valueOf(index + 1))) sb.append(" "); sb.append(index + 1); if (appDay.equals(String.valueOf(index + 1))) sb.append(" "); sb.append("
"); sb.append("
 

"); 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; } }

좋은 웹페이지 즐겨찾기