Package edu.zzuli.util

Source Code of edu.zzuli.util.DateUtil

package edu.zzuli.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

import edu.zzuli.common.MisException;

public class DateUtil {
  /**
   * 名称:long2StrDate 功能:该函数将8位或12位或14位的数值型日期时间转换为10位或16位或19带格式的日期时间 型如:
   * 20040101 转换为 2004/01/01 或 200401010101 转换为 2004/01/01 01:01 或
   * 20040101010101 转换为 2004/01/01 01:01:01 输入参数:Long date 十二位或十四位的数字日期时间
   * 返回参数:String fmDate 16位或19位带格式的日期
   */
  public static String long2StrDate(String date) {
    if (date == null || date.equals(""))
      return "";
    else
      return long2StrDate(Long.valueOf(date));
  }

  public static String int2StrDate(Integer date) {
    if (date == null)
      return "";
    return long2StrDate(Long.valueOf(date));
  }

  public static String long2StrDate(Number date) {
    String fmDate = new String();
    if (date == null) {
      fmDate = "";
    } else {
      fmDate = date.toString();
      String year = "";
      String month = "";
      String day = "";
      String hour = "";
      String minute = "";
      // 日期格式不合法则转化为空串
      if (fmDate.length() < 8) {
        fmDate = "";
      }
      if (fmDate.length() >= 8) {
        year = fmDate.substring(0, 4);
        month = fmDate.substring(4, 6);
        day = fmDate.substring(6, 8);
        fmDate = year + "/" + month + "/" + day;
      }
      if ((date.toString()).length() >= 12) {
        hour = (date.toString()).substring(8, 10);
        minute = (date.toString()).substring(10, 12);
        fmDate = fmDate + " " + hour + ":" + minute;
      }
      if ((date.toString()).length() == 14) {
        fmDate = fmDate + ":" + (date.toString()).substring(12, 14);
      }
    }
    return fmDate;
  }

  public static String int2StrOnlyDate(Number date) {
    String fmDate = new String();
    if (date == null) {
      fmDate = "";
    } else {
      String year = "";
      String month = "";
      String day = "";
      fmDate = date.toString();
      if (fmDate.length() < 8) {
        fmDate = "";
      }
      if (fmDate.length() >= 8) {
        year = fmDate.substring(0, 4);
        month = fmDate.substring(4, 6);
        day = fmDate.substring(6, 8);
        fmDate = year + "/" + month + "/" + day;
      }
    }
    return fmDate;
  }

  public static String int2StrCnDate(Integer date) {
    if (date == null)
      return "";
    return long2StrCnDate(Long.valueOf(date));
  }

  public static String long2StrCnDate(Long date) {
    String fmDate = new String();
    if (date == null) {
      fmDate = "";
    } else {
      fmDate = date.toString();
      String year = "";
      String month = "";
      String day = "";
      String hour = "";
      String minute = "";
      // 日期格式不合法则转化为空串
      if (fmDate.length() < 8) {
        fmDate = "";
      }
      if (fmDate.length() >= 8) {
        year = fmDate.substring(0, 4);
        month = fmDate.substring(4, 6);
        day = fmDate.substring(6, 8);
        fmDate = year + "年" + month + "月" + day + "日";
      }
      if ((date.toString()).length() >= 10) {
        hour = (date.toString()).substring(8, 10);
        fmDate = fmDate + hour + "时";
      }

      if ((date.toString()).length() >= 12) {
        minute = (date.toString()).substring(10, 12);
        fmDate = fmDate + minute + "分";
      }

      if ((date.toString()).length() == 14) {
        fmDate = fmDate + (date.toString()).substring(12, 14) + "秒";
      }
    }
    return fmDate;
  }

  public static String long2StrCnDate(Number date) {
    String fmDate = new String();
    if (date == null) {
      fmDate = "";
    } else {
      fmDate = String.valueOf(date.longValue());
      String year = "";
      String month = "";
      String day = "";
      String hour = "";
      String minute = "";
      // 日期格式不合法则转化为空串
      if (fmDate.length() < 8) {
        fmDate = "";
      }
      if (fmDate.length() >= 8) {
        year = fmDate.substring(0, 4);
        month = fmDate.substring(4, 6);
        day = fmDate.substring(6, 8);
        fmDate = year + "年" + month + "月" + day + "日";
      }
      if ((date.toString()).length() >= 12) {
        hour = (date.toString()).substring(8, 10);
        minute = (date.toString()).substring(10, 12);
        fmDate = fmDate + hour + "时" + minute + "分";
      }
      if ((date.toString()).length() == 14) {
        fmDate = fmDate + (date.toString()).substring(12, 14) + "秒";
      }
    }
    return fmDate;
  }

  public static String long2StrCnDate(Number date, int length) {
    if (date != null) {
      Long l = Long.valueOf(String.valueOf(date.longValue()).substring(0,
          length));
      return long2StrCnDate(l);
    }
    return null;
  }

  /**
   * 名称:str2LongDate 功能:该函数将10位或16位或19位带格式的日期时间转换为8位或12位或14位的数值型日期时间
   * 型如:2004/01/01 转换为 20040101 或 2004/01/01 01:01 转换为 200401010101 或
   * 2004/01/01 01:01:01 转换为 20040101010101 如果 16位或19位带格式的日期时间 非法 ,则返回 0
   * 输入参数:String strFmDate 16位或19位带格式的日期时间 返回参数:Long longDate 12位或14位的数值型日期时间
   */
  public static Integer str2IntDate(String strFmDate) {
    Long date = str2LongDate(strFmDate);
    if (date == null)
      return null;
    return Integer.valueOf(date.intValue());
  }

  public static Long str2LongDate(String strFmDate) {
    if (strFmDate == null || strFmDate.length() == 0) {
      return null;
    }
    Long longDate = new Long(0);
    if (strFmDate.matches("^([12]\\d{3}/[01]\\d/[0123]\\d)$")) {
      longDate = Long.valueOf(strFmDate.replaceAll("/", ""));
    }
    if (strFmDate
        .matches("^([12]\\d{3}/[01]\\d/[0123]\\d\\s[012]\\d:[0-5]\\d)$")) {
      strFmDate = strFmDate.replaceAll("/", "").replaceAll(":", "")
          .replaceAll("\\s", "");
      longDate = Long.valueOf(strFmDate);
    }
    if (strFmDate
        .matches("^([12]\\d{3}/[01]\\d/[0123]\\d\\s[012]\\d:[0-5]\\d:[0-5]\\d)$")) {
      strFmDate = strFmDate.replaceAll("/", "").replaceAll(":", "")
          .replaceAll("\\s", "");
      longDate = Long.valueOf(strFmDate);
    }
    return longDate;
  }

  /**
   * 编写人: 名称:getCurrentDateView 功能:得到系统当前时间显示,格式:yyyy/mm/dd主要是为页面显示用 输入参数:
   * 返回参数:String:系统当前时间
   */
  public static String getCurrentDateView() {
    // 获得当前日期
    Calendar cldCurrent = Calendar.getInstance();
    // 获得年月日
    String strYear = String.valueOf(cldCurrent.get(Calendar.YEAR));
    String strMonth = String.valueOf(cldCurrent.get(Calendar.MONTH) + 1);
    String strDate = String.valueOf(cldCurrent.get(Calendar.DATE));
    // 整理格式
    if (strMonth.length() < 2) {
      strMonth = "0" + strMonth;
    }
    if (strDate.length() < 2) {
      strDate = "0" + strDate;
    }
    // 得出当天日期的字符串
    String StrCurrentCalendar = strYear + "/" + strMonth + "/" + strDate;
    return StrCurrentCalendar;
  }

  /**
   * 编写人: 名称:getCurrentDateTimeView 功能:得到系统当前时间显示,格式:yyyy/mm/dd hh:mm主要是为页面显示用
   * 输入参数: 返回参数:String:系统当前时间
   */
  public static String getCurrentDateTimeView() {
    // 获得当前日期
    Calendar cldCurrent = Calendar.getInstance();
    // 获得年月日
    String strYear = String.valueOf(cldCurrent.get(Calendar.YEAR));
    String strMonth = String.valueOf(cldCurrent.get(Calendar.MONTH) + 1);
    String strDate = String.valueOf(cldCurrent.get(Calendar.DATE));
    String srtHours = String.valueOf(cldCurrent.get(Calendar.HOUR_OF_DAY));
    String strMinute = String.valueOf(cldCurrent.get(Calendar.MINUTE));
    // 整理格式
    if (strMonth.length() < 2) {
      strMonth = "0" + strMonth;
    }
    if (strDate.length() < 2) {
      strDate = "0" + strDate;
    }
    if (srtHours.length() < 2) {
      srtHours = "0" + srtHours;
    }
    if (strMinute.length() < 2) {
      strMinute = "0" + strMinute;
    }
    // 得出当天日期时间的字符串
    String StrCurrentCalendar = strYear + "/" + strMonth + "/" + strDate
        + " " + srtHours + ":" + strMinute;
    return StrCurrentCalendar;
  }

  public static Integer getIntCurrDate() {
    // 获得当前日期
    Calendar cldCurrent = Calendar.getInstance();
    // 获得年月日
    String strYear = String.valueOf(cldCurrent.get(Calendar.YEAR));
    String strMonth = String.valueOf(cldCurrent.get(Calendar.MONTH) + 1);
    String strDate = String.valueOf(cldCurrent.get(Calendar.DATE));
    // 整理格式
    if (strMonth.length() < 2) {
      strMonth = "0" + strMonth;
    }
    if (strDate.length() < 2) {
      strDate = "0" + strDate;
    }
    return Integer.valueOf(strYear + strMonth + strDate);
  }

  public static Integer getIntFirstDayOfMonth() {
    // 获得当前日期
    Calendar cldCurrent = Calendar.getInstance();
    // 获得年月日
    String strYear = String.valueOf(cldCurrent.get(Calendar.YEAR));
    String strMonth = String.valueOf(cldCurrent.get(Calendar.MONTH) + 1);

    // 整理格式
    if (strMonth.length() < 2) {
      strMonth = "0" + strMonth;
    }
    return Integer.valueOf(strYear + strMonth + "01");
  }

  public static Integer getIntCurrYear() {
    // 获得当前日期
    Calendar cldCurrent = Calendar.getInstance();
    // 获得年月日
    String strYear = String.valueOf(cldCurrent.get(Calendar.YEAR));
    return Integer.valueOf(strYear);
  }

  public static Long getLongCurrDateTime() {
    // 获得当前日期
    Calendar cldCurrent = Calendar.getInstance();
    // 获得年月日
    String strYear = String.valueOf(cldCurrent.get(Calendar.YEAR));
    String strMonth = String.valueOf(cldCurrent.get(Calendar.MONTH) + 1);
    String strDate = String.valueOf(cldCurrent.get(Calendar.DATE));
    String srtHours = String.valueOf(cldCurrent.get(Calendar.HOUR_OF_DAY));
    String strMinute = String.valueOf(cldCurrent.get(Calendar.MINUTE));
    // 整理格式
    if (strMonth.length() < 2) {
      strMonth = "0" + strMonth;
    }
    if (strDate.length() < 2) {
      strDate = "0" + strDate;
    }
    if (srtHours.length() < 2) {
      srtHours = "0" + srtHours;
    }
    if (strMinute.length() < 2) {
      strMinute = "0" + strMinute;
    }
    return Long
        .valueOf(strYear + strMonth + strDate + srtHours + strMinute);
  }

  public static Long getLongCurrDateTime8() {
    // 获得当前日期
    Calendar cldCurrent = Calendar.getInstance();
    // 获得年月日
    String strYear = String.valueOf(cldCurrent.get(Calendar.YEAR));
    String strMonth = String.valueOf(cldCurrent.get(Calendar.MONTH) + 1);
    String strDate = String.valueOf(cldCurrent.get(Calendar.DATE));
    String srtHours = String.valueOf(cldCurrent.get(Calendar.HOUR_OF_DAY));
    String strMinute = String.valueOf(cldCurrent.get(Calendar.MINUTE));
    // 整理格式
    if (strMonth.length() < 2) {
      strMonth = "0" + strMonth;
    }
    if (strDate.length() < 2) {
      strDate = "0" + strDate;
    }
    if (srtHours.length() < 2) {
      srtHours = "0" + srtHours;
    }
    if (strMinute.length() < 2) {
      strMinute = "0" + strMinute;
    }
    return Long.valueOf(strYear + strMonth + strDate);
  }

  /**
   * 作者:程伟平 创建时间:Feb 29, 2008
   * <p>
   *
   * 获得系统当前时间。
   *
   * @return String 日期格式:yyyyMMddHHmm 示例:200802261310
   */
  public static String getCurrentDateTime() {
    return getDateTime(0, 0);
  }

  /**
   * 作者:程伟平 创建时间:Feb 26, 2008
   * <p>
   *
   * 获得相对于系统当前时间的前(后)month个月、前(后)date个日的时间。
   * 说明:当month的值为负时,为当前时间的前month个月,当month的值为正时
   * ,为当前时间的后month个月;当date的值为负时,为当前时间的前date个日,当date的值为正时,为当前时间的后date个日。
   *
   * @param month
   *            前(后)个月
   * @param date
   *            前(后)个日
   * @return String 日期格式:yyyyMMddHHmm 示例:200802261310
   */
  public static String getDateTime(int month, int date) {
    // 获得当前日期
    Calendar calendar = Calendar.getInstance();
    if (month != 0) {
      calendar.add(Calendar.MONTH, month);
    }
    if (date != 0) {
      calendar.add(Calendar.DATE, date);
    }
    // 获得年月日
    String strYear = String.valueOf(calendar.get(Calendar.YEAR));
    String strMonth = String.valueOf(calendar.get(Calendar.MONTH) + 1);
    String strDate = String.valueOf(calendar.get(Calendar.DATE));
    String srtHours = String.valueOf(calendar.get(Calendar.HOUR_OF_DAY));
    String strMinute = String.valueOf(calendar.get(Calendar.MINUTE));
    // 整理格式
    if (strMonth.length() < 2) {
      strMonth = "0" + strMonth;
    }
    if (strDate.length() < 2) {
      strDate = "0" + strDate;
    }
    if (srtHours.length() < 2) {
      srtHours = "0" + srtHours;
    }
    if (strMinute.length() < 2) {
      strMinute = "0" + strMinute;
    }
    return strYear + strMonth + strDate + srtHours + strMinute;
  }

  /**
   * 作者:程伟平 创建时间:Nov 8, 2008
   * <p>
   *
   * 获取当前年的第一天
   *
   * @return String
   */
  public static String getCurrentYearFirstDate() {

    // 获得当前日期
    Calendar calendar = Calendar.getInstance();

    String strYear = String.valueOf(calendar.get(Calendar.YEAR));

    return strYear + "01" + "01" + "00" + "00";

  }

  public static Long getLongCurrDateTime14() {
    return Long.valueOf(formateCalendar(Calendar.getInstance(),
        "yyyyMMddHHmmss"));
  }

  public static Long getLongCurrDateTime12() {
    return Long.valueOf(formateCalendar(Calendar.getInstance(),
        "yyyyMMddHHmm"));
  }

  public static String transferDate(Date date) {
    if (date == null)
      return null;
    SimpleDateFormat dateFormate = new SimpleDateFormat(
        "yyyy/MM/dd HH:mm:ss");
    return dateFormate.format(date);
  }

  public static Long transferDateToLong(Date date) {
    if (date == null)
      return null;
    SimpleDateFormat dateFormate = new SimpleDateFormat("yyyyMMddHHmmss");
    return Long.valueOf(dateFormate.format(date));
  }

  /**
   * yyyy/MM/dd HH:mm:ss(yyyy年,MM月,dd日,HH时,mm分,ss秒)
   *
   * @param calendar
   * @param format
   * @return
   */
  public static String formateCalendar(Calendar calendar, String format) {
    if (calendar == null)
      return null;
    SimpleDateFormat dateFormate = new SimpleDateFormat(format);
    return dateFormate.format(calendar.getTime());
  }

  /**
   * 将形如20070730形式的日期格式,转换成2007-07-30
   */
  public static String dateFormateStr(String strDate) {
    if (strDate == null)
      return "";
    String fmDate = "";
    String year = "";
    String month = "";
    String day = "";
    if (strDate.length() >= 8) {
      year = strDate.substring(0, 4);
      month = strDate.substring(4, 6);
      day = strDate.substring(6, 8);
      fmDate = year + "-" + month + "-" + day;
    }
    return fmDate;
  }

  public static Calendar long2Calendar(Long time) {
    Calendar calendar = null;
    if (time == null) {
      calendar = Calendar.getInstance();
    } else {
      String strTime = String.valueOf(time);
      int year = 0;
      int month = 0;
      int day = 0;
      int hour = 0;
      int minute = 0;
      int sec = 0;
      if (strTime.length() < 8) {
        throw new MisException("时间格式不合法,不能少与八位数字!");
      }
      if (strTime.length() >= 8) {
        year = Integer.valueOf(strTime.substring(0, 4));
        month = Integer.valueOf(strTime.substring(4, 6)) - 1;
        day = Integer.valueOf(strTime.substring(6, 8));
      }
      if (strTime.length() >= 10) {
        hour = Integer.valueOf(strTime.substring(8, 10));
      }
      if (strTime.length() >= 12) {
        minute = Integer.valueOf(strTime.substring(10, 12));
      }
      if (strTime.length() >= 14) {
        sec = Integer.valueOf(strTime.substring(12, 14));
      }
      calendar = new GregorianCalendar(year, month, day, hour, minute,
          sec);
    }
    return calendar;
  }

  /**
   * 计算时间添加天数计算,给添加评标活动时使用,用于计算评标截止时间 问题编号:[fix-005]
   *
   * @param time
   *            原来的时间
   * @param days
   *            加的天加1天 1.5一天半
   * @return
   */
  public static long calcTimeAddOfDay(long time, double days) {
    SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmm");

    java.util.Date date1 = new Date();
    try {
      date1 = format.parse(String.valueOf(time));
    } catch (ParseException e) {
      e.printStackTrace();
    }
    int addDay = (int) (days * 24);
    long Time = (date1.getTime() / 1000) + (60 * 60 * addDay);

    date1.setTime(Time * 1000);

    String mydate = format.format(date1);

    return Long.parseLong(mydate);
  }

  /**
   * 功能描述:得到给定时间减去给定天数后的时间<br>
   *
   * @param time
   *            时间
   * @param days
   *            天数
   * @return long <BR>
   *         作者:杨凯<BR>
   *         时间:Jun 17, 2008 <br>
   */
  public static long getTimeSubtractOfDay(long time, int days) {
    SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmm");

    java.util.Date date1 = new Date();
    try {
      date1 = format.parse(String.valueOf(time));
    } catch (ParseException e) {
      e.printStackTrace();
    }
    int subtractDay = (int) (days * 24);
    long Time = (date1.getTime() / 1000) - (60 * 60 * subtractDay);

    date1.setTime(Time * 1000);

    String mydate = format.format(date1);

    return Long.parseLong(mydate);
  }

  /**
   * 功能描述:得到当前时间之前或之后的数据<br>
   *
   * @param number
   *            正数为以后时间 负数为之前时间 type 1 年 2 月 3 日 ws 返回的长度
   * @return long <BR>
   *         作者:陈伟<BR>
   *         时间:Jun 17, 2008 <br>
   */
  @SuppressWarnings("deprecation")
  public static long getDateToNow(int number, int type, int ws) {
    Long resultLong = null;
    Calendar cal = Calendar.getInstance();
    Date date = new Date();
    cal.setTime(date);
    System.out.println(cal.getTime().toLocaleString());
    if (type == 1) {
      cal.add(Calendar.YEAR, number);
    } else if (type == 2) {
      cal.add(Calendar.MONTH, number);
    } else if (type == 3) {
      cal.add(Calendar.DAY_OF_MONTH, number);
    }
    resultLong = transferDateToLong(cal.getTime());
    if (ws == 8) {
      resultLong = resultLong / (1000000);
    }
    return resultLong;
  }

  public static void main(String[] args) {
    // System.out.println(str2LongDate("2008年01月12日 14时15分",
    // "YYYY年MM月DD日 HH时FF分"));
  }

  /**
   *@author 张苹苹
   *@date 2009-1-4
   *@discription : 20090104165030时间格式转换成16时50分30秒的格式
   *@param date
   *@return String
   */
  public static String getLongTime(Number date) {
    String fmDate = new String();
    if (date == null) {
      fmDate = "";
    } else {
      fmDate = String.valueOf(date.longValue());
      String hour = "";
      String minute = "";
      String seconds = "";
      // 日期格式不合法则转化为空串
      if (fmDate.length() < 14) {
        fmDate = "";
      }
      if (fmDate.length() == 14) {
        hour = fmDate.substring(8, 10);
        minute = fmDate.substring(10, 12);
        seconds = fmDate.substring(12, 14);
        fmDate = hour + ":" + minute + ":" + seconds;
      }
    }
    return fmDate;
  }

  /**
   * 取得月份的第一天
   *
   * @param strdate
   *            String
   * @return String
   */
  public static Long getMonthBegin(Long time) {
    Calendar cal = long2Calendar(time);
    int year = cal.get(Calendar.YEAR);
    int month = cal.get(Calendar.MONTH);
    cal.set(year, month, 1);
    return calendarToLong8(cal);
  }

  /**
   * 转化日历为长整形
   *
   * @param cal
   * @return
   */
  public static Long calendarToLong8(Calendar cal) {
    int year = cal.get(Calendar.YEAR);
    int month = cal.get(Calendar.MONTH) + 1;
    int date = cal.get(Calendar.DATE);
    String strMonth = month < 10 ? ("0" + month) : String.valueOf(month);
    String strDate = date < 10 ? ("0" + date) : String.valueOf(date);
    return Long.parseLong(year + strMonth + strDate);
  }

  /**
   * @discription : 取得指定月份的最后一天
   * @param time
   * @return Long
   */
  public static Long getMonthEnd(Long time) {
    Calendar cal = long2Calendar(getMonthBegin(time));
    cal.add(Calendar.MONTH, 1);
    cal.add(Calendar.DAY_OF_YEAR, -1);
    @SuppressWarnings("unused")
    int yeah = cal.get(Calendar.YEAR);
    @SuppressWarnings("unused")
    int month = cal.get(Calendar.MONTH);
    @SuppressWarnings("unused")
    int day = cal.get(Calendar.DATE);
    return calendarToLong8(cal);
  }
}
TOP

Related Classes of edu.zzuli.util.DateUtil

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.