Java tutorial
/** * Project: guahao-portal-web-home * * File Created at 2012-9-26 * * Copyright 2012 Greenline.com Corporation Limited. * All rights reserved. * * This software is the confidential and proprietary information of * Greenline Company. ("Confidential Information"). You shall not * disclose such Confidential Information and shall use it only in * accordance with the terms of the license agreement you entered into * with Greenline.com. */ package com.greenline.guahao.biz.util; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import org.apache.commons.lang.StringUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import com.greenline.guahao.biz.manager.order.dataobject.DaySectionDO; import com.greenline.guahao.biz.manager.search.dataobject.SearchExpertsDO; /** * @Type DateUtils * @Desc * @author jianyun.zheng * @date 2012-9-26 * @Version V1.0 */ public class DateUtils { private static final Log logger = LogFactory.getLog(DateUtils.class); /** * */ private final static String dayNames[] = { "", "", "", "", "", "", "" }; /** * ? */ private final static String apms[] = { "?", "?", "", "" }; /** * ?yyyy-MM-dd */ public static final String DATE_FORMAT_YYYY_MM_DD = "yyyy-MM-dd"; /** * ???0?7? * * @param date ?yyyy-MM-dd * @return 0?1?2... */ public static int getIndexOfCycle(String date) { try { SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd"); Calendar cal = Calendar.getInstance(); cal.setTime(df.parse(date)); int b = getDaysToInt(new Date(), cal.getTime()); int a = -1; if ((b % 7) == 0) { a = (b / 7) - 1; } else { a = b / 7; } return a; } catch (Exception e) { logger.error("???", e); } return -1; } /** * ???0?7? * * @param date * @return int */ public static int getIndexInCycle(String date) { try { SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd"); Calendar cal = Calendar.getInstance(); cal.add(Calendar.DAY_OF_YEAR, 1); // int d = cal.get(Calendar.DAY_OF_WEEK); // cal.setTime(df.parse(date)); int id = cal.get(Calendar.DAY_OF_WEEK); // int idx = id - d; idx = idx < 0 ? idx + 7 : idx; // 7 return idx; } catch (Exception e) { logger.error("???", e); } return -1; } /** * ?() * * @param date1 * @param date2 * @return Long */ public static Long getDaysToLong(Date date1, Date date2) { Long d1 = getYmdTime(date1).getTime() / 1000; Long d2 = getYmdTime(date2).getTime() / 1000; return Math.abs((d2 - d1)) / (24 * 3600); } public static int getDaysToInt(Date date1, Date date2) { return Integer.valueOf(getDaysToLong(date1, date2).toString()); } /** * * * @param date * @return Date */ public static Date getYmdTime(Date date) { if (date == null) { return (new Date()); } Calendar day = Calendar.getInstance(); day.setTime(date); day.set(Calendar.HOUR_OF_DAY, 0); day.set(Calendar.MINUTE, 0); day.set(Calendar.SECOND, 0); day.set(Calendar.MILLISECOND, 0); Date convertTime = day.getTime(); return convertTime; } // ?? 1 24*3600*1000 public static String getTimeDesc(Date date) { if (null == date) { return null; } Calendar now = Calendar.getInstance(); Calendar time = Calendar.getInstance(); time.setTime(date); // ??? long t = (now.getTimeInMillis() - time.getTimeInMillis()) / 1000; // ?2012-01-08?2012-01-08 09:20 if ((t < 0) || (t > ((now.get(Calendar.HOUR_OF_DAY) + 24) * 3600 + now.get(Calendar.MINUTE) + 60 + now.get(Calendar.SECOND))) || (t < 0)) { SimpleDateFormat df = new SimpleDateFormat("yyyy/MM/dd"); return df.format(date); } // 1?" 09:30" if (t > 3600) { SimpleDateFormat df = new SimpleDateFormat("HH:mm"); String str = df.format(date); int day_time = time.get(Calendar.DAY_OF_YEAR);// int now_time = now.get(Calendar.DAY_OF_YEAR);// ? // 1?"11:59" if (day_time < now_time) { str = " ".concat(str); } else { str = " ".concat(str); } return str; } // 11?"XX?" if (t >= 60) { return t / 60 + "?"; } // ?1 return ""; } /** * ??yyyyMMdd * * @author wangbiao * @param d * @return String */ public static String getCurrentDateYmd(Date d) { SimpleDateFormat destsmf = new SimpleDateFormat("yyyyMMdd"); return destsmf.format(d); } /** * yyyy-MM-dd hh:mm:sss ?? * * @author qinying * @param d * @return String */ public static String getYmdhmssDateString(Date d) { if (d == null) { return StringUtils.EMPTY; } try { SimpleDateFormat destsmf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); return destsmf.format(d); } catch (Exception e) { logger.error("??d=" + d, e); } return StringUtils.EMPTY; } /** * yyyy-MM-dd hh:mm:sss ?? * * @author qinying * @param d * @return String */ public static Date getYmdhmssStringToDate(String d) { if (StringUtils.isBlank(d)) { return null; } try { SimpleDateFormat destsmf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); return destsmf.parse(d); } catch (Exception e) { logger.error("??d=" + d, e); } return null; } /** * yyyyMMdd?? * * @author qinying * @param d * @return String */ public static String getYmdDateString(Date d) { SimpleDateFormat destsmf = new SimpleDateFormat("yyyyMMdd"); return destsmf.format(d); } /** * yyyy-MM-dd ?? * * @author qinying * @param d * @return String */ public static String getDateString(Date d, String template) { try { SimpleDateFormat destsmf = new SimpleDateFormat(template); return destsmf.format(d); } catch (Exception e) { logger.error("?", e); } return StringUtils.EMPTY; } /** * yyyyMMdd?? * * @param s * @return s==null?null */ public static Date getYyyyMMddDate(String s) { if (s == null) { return null; } try { return new SimpleDateFormat("yyyyMMdd").parse(s); } catch (ParseException e) { return null; } } /** * yyyyMMdd?? * * @param s * @return s==null?null */ public static Date getYyyyMMddHHDate(String s) { if (s == null) { return null; } try { return new SimpleDateFormat("yyyyMMdd HH:mm:ss").parse(s); } catch (ParseException e) { return null; } } /** * yyyy-MM-dd T HH:mm:ss Z?? * * @param s * @return s==null?null */ public static Date getYyyyMMddTZDate(String s) { if (s == null) { return null; } try { return new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'").parse(s); } catch (ParseException e) { logger.error("?string=" + s, e); return null; } } /** * yyyy-MM-dd?? * * @param s * @return s==null?null */ public static Date getYyyyMMddConnectDate(String s) { if (s == null) { return null; } try { return new SimpleDateFormat("yyyy-MM-dd").parse(s); } catch (ParseException e) { logger.error("?yyyy-MM-dd", e); return null; } } /** * ? */ public static Date getStringToDate(String date) throws ParseException { return new SimpleDateFormat("yyyyMMdd").parse(date); } /** * ? * * @param clinicDate * @return String */ public static Integer getTreatAfterDay(String clinicDate) { if (StringUtils.isNotBlank(clinicDate)) { try { return Integer.valueOf(String.valueOf(getDaysToInt(getStringToDate(clinicDate), new Date()))); } catch (ParseException e) { logger.error(e); } } return null; } /** * ?/? * * @param timeType * @return */ public static String getApmByTimeType(Integer timeType) { String apm = ""; if (DaySectionDO.APM_AM.equals(timeType)) { apm = apms[0]; } else if (DaySectionDO.APM_PM.equals(timeType)) { apm = apms[1]; } else if (DaySectionDO.APM_NT.equals(timeType)) { apm = apms[2]; } else if (DaySectionDO.APM_FL.equals(timeType)) { apm = apms[3]; } return apm; } /** * ? * * @param date * @return */ public static String getWeekDayString(Date date) { Calendar calendar = Calendar.getInstance(); calendar.setTime(date); int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK); return dayNames[dayOfWeek - 1]; } /** * ??? * * @param date * @return */ public static Date getLastDay(Date date) { Calendar calendar = Calendar.getInstance(); calendar.setTime(date); calendar.add(Calendar.DAY_OF_YEAR, 1); return calendar.getTime(); } /** * ????yyyyMMdd * * @param date * @return */ public static String getFromSearchShiftCaseDate(String fromSearchShiftCaseDate) { if (StringUtils.isBlank(fromSearchShiftCaseDate) || SearchExpertsDO.NOT_LIMIT.equals(fromSearchShiftCaseDate) || "??".equals(fromSearchShiftCaseDate)) { return null; } fromSearchShiftCaseDate = fromSearchShiftCaseDate.replaceAll("-", "");// ?yyyy-MM-dd? if (!isNumeric(fromSearchShiftCaseDate) || fromSearchShiftCaseDate.length() != 8) { logger.error("?" + fromSearchShiftCaseDate); return null; } SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd"); try { Date date = sdf.parse(fromSearchShiftCaseDate); SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'"); return simpleDateFormat.format(date); } catch (Exception e) { logger.error("???" + fromSearchShiftCaseDate, e); } return null; } public static String getToSearchShiftCaseDate(String toSearchShiftCaseDate) { if (StringUtils.isBlank(toSearchShiftCaseDate) || SearchExpertsDO.NOT_LIMIT.equals(toSearchShiftCaseDate) || "??".equals(toSearchShiftCaseDate)) { return null; } toSearchShiftCaseDate = toSearchShiftCaseDate.replaceAll("-", "");// ?yyyy-MM-dd? if (!isNumeric(toSearchShiftCaseDate) || toSearchShiftCaseDate.length() != 8) { logger.error("?" + toSearchShiftCaseDate); return null; } SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd"); Calendar c = Calendar.getInstance(); try { Date date = sdf.parse(toSearchShiftCaseDate); c.clear(); c.setTime(date); // 235959 c.set(Calendar.HOUR_OF_DAY, 23); c.set(Calendar.MINUTE, 59); c.set(Calendar.SECOND, 59); SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'"); return simpleDateFormat.format(c.getTime()); } catch (Exception e) { logger.error("???" + toSearchShiftCaseDate, e); } return null; } /** * ???? ??? ?5 * * @param source * @param hour * @return */ public static boolean isAfterHour(Date source, int hour) { Calendar c = Calendar.getInstance(); c.setTime(source); int hourOfDay = c.get(Calendar.HOUR_OF_DAY); if (hourOfDay - hour >= 0) { return true; } else { return false; } } /** * ? * * @param string * @return boolean */ public static boolean isNumeric(String str) { for (int i = str.length(); --i >= 0;) { int chr = str.charAt(i); if (chr < 48 || chr > 57) return false; } return true; } }