• DateUtil


    /**
    * www.yiji.com Inc.
    * Copyright (c) 2011 All Rights Reserved.
    */
    package com.yjf.borncrm.common.util;

    import java.text.DateFormat;
    import java.text.SimpleDateFormat;
    import java.util.Calendar;
    import java.util.Date;

    import com.yjf.common.lang.util.StringUtil;
    import com.yjf.common.log.Logger;
    import com.yjf.common.log.LoggerFactory;

    /**
    *
    * @Filename DateUtil.java
    *
    * @Description
    *
    * @Version 1.0
    *
    * @Author peigen
    *
    * @Email qchunhai@yiji.com
    *
    * @History <li>Author: qichunhai</li> <li>Date: 2012-10-24</li> <li>Version:
    * 1.0</li> <li>Content: create</li>
    *
    */
    public class DateUtil {

    static final Logger logger = LoggerFactory.getLogger(DateUtil.class);

    /**
    * 格式化日期为带时分秒的格式
    * @param date
    * @return
    */
    public static final String formatDateTime(Date date) {
    return formatDateTime(com.yjf.common.lang.util.DateUtil.simple, date);
    }

    public static final long formatLong(Date date) {
    if (date == null)
    return 0l;
    return NumberUtil
    .parseLong(formatDateTime(com.yjf.common.lang.util.DateUtil.dtShort, date));
    }

    public static final String formatDateTime(String formatString, Date date) {
    try {
    return com.yjf.common.lang.util.DateUtil.getFormat(formatString).format(date);
    } catch (Exception e) {
    logger.error(e.getMessage() + " date =" + date);
    }
    return null;
    }

    public static final Date parseDateTime(String formatString, String dateString) {
    try {
    return com.yjf.common.lang.util.DateUtil.getFormat(formatString).parse(dateString);
    } catch (Exception e) {
    logger.error(e.getMessage() + " date =" + dateString);
    }
    return null;
    }

    /**
    * 格式化日期为天 2012-01-01
    * @param date
    * @return
    */
    public static final String formatDay(Date date) {
    return formatDateTime(com.yjf.common.lang.util.DateUtil.dtSimple, date);
    }

    /**
    * 把日期字符串 转换当天的开始时间点00:00:00
    * @param date
    * @return
    */
    public static final Date parseDayBegin(String date) {
    if (StringUtil.isNotEmpty(date) && date.length() == 10)
    date = date + " 00:00:00";
    return parseDateTime(com.yjf.common.lang.util.DateUtil.simple, date);
    }

    /**
    * 把日期字符串 转换当天的最后时间点23:59:59
    * @param date 格式 yyyy-MM-dd
    * @return
    */
    public static final Date parseDayEnd(String date) {
    if (StringUtil.isNotEmpty(date) && date.length() == 10)
    date = date + " 23:59:59";
    return parseDateTime(com.yjf.common.lang.util.DateUtil.simple, date);
    }

    /**
    * 把日期字符串 转换当天的最后时间点23:59:59
    * @param date 格式 yyyy-MM-dd
    * @return
    */
    public static final Date parseDayEnd(Date date) {
    if (date == null)
    return null;
    String strDate = formatDay(date);
    return parseDayEnd(strDate);
    }

    /**
    * 把日期字符串 转换当天的开始时间点00:00:00
    * @param date
    * @return
    */
    public static final Date parseDayBegin(Date date) {
    if (date == null)
    return null;
    String strDate = formatDay(date);
    return parseDayBegin(strDate);
    }

    /**
    * @param date 格式 yyyy-MM-dd HH:mm:ss
    * @return
    */
    public static final Date parseDateTime(String date) {
    return parseDateTime(com.yjf.common.lang.util.DateUtil.simple, date);
    }

    /**
    * @param date 格式 yyyy-MM-dd
    * @return
    */
    public static final Date parseDtSimpleTime(String date) {
    return parseDateTime(com.yjf.common.lang.util.DateUtil.dtSimple, date);
    }

    /**
    * 校验结束时间减去开始时间是否在minutes分钟以内 countMinutes = end-start; countMinutes >
    * minutes ,return true; countMinutes <= minutes ,return false;
    * @param start
    * @param end
    * @param minutes
    * @return
    */
    public static boolean checkMinute(Date start, Date end, int minutes) {
    if ((start == null) || (end == null)) {
    return false;
    }
    long countMinutes = ((end.getTime() - start.getTime()) / (1000 * 60));
    return countMinutes > minutes;
    }

    /**
    * 获取当前的年份
    * @return
    */
    public static String getThisYear() {
    Date date = new Date();
    String dateStr = com.yjf.common.lang.util.DateUtil.dtSimpleFormat(date);
    String thisYear = StringUtil.substring(dateStr, 0, 4);
    return thisYear;
    }

    /**
    * 判断date到今天是否超过days个工作日 (该方法只处理了星期六 星期天)
    * @param date 时间
    * @param days 天数
    * @return
    */
    public static boolean isSurpassByDay(Date date, int days) {
    boolean result = false;
    Date withholdDay = date;
    Date newToday = new Date();
    Date increaseDay = withholdDay;
    if (!(com.yjf.common.lang.util.DateUtil.isDefaultWorkingDay(withholdDay))) {
    withholdDay = com.yjf.common.lang.util.DateUtil.getEndTimeOfTheDate(withholdDay);
    }
    for (int i = 0; i < days;) {
    increaseDay = com.yjf.common.lang.util.DateUtil.increaseDate(increaseDay, 1);
    if (com.yjf.common.lang.util.DateUtil.isDefaultWorkingDay(increaseDay)) {
    i++;
    }
    }
    if (increaseDay.getTime() < newToday.getTime()) {
    result = true;
    }
    return result;
    }

    /**
    * 判断date到endDate是否超过days个工作日 (该方法只处理了星期六 星期天)
    * @param date
    * @param endDate
    * @param days
    * @return 超过返回true
    */
    public static boolean isSurpassByDay(Date date, Date endDate, int days) {
    boolean result = false;
    Date withholdDay = date;
    Date increaseDay = withholdDay;
    if (!(com.yjf.common.lang.util.DateUtil.isDefaultWorkingDay(withholdDay))) {
    withholdDay = com.yjf.common.lang.util.DateUtil.getEndTimeOfTheDate(withholdDay);
    }
    for (int i = 0; i < days;) {
    increaseDay = com.yjf.common.lang.util.DateUtil.increaseDate(increaseDay, 1);
    if (com.yjf.common.lang.util.DateUtil.isDefaultWorkingDay(increaseDay)) {
    i++;
    }
    }
    if (increaseDay.getTime() < endDate.getTime()) {
    result = true;
    }
    return result;
    }

    /**
    * 计算今天,转换格式后贴在页面上
    * @param date
    * @param endDate
    * @param days
    * @return
    */
    public static String getHeadMessage() {
    String message = "";
    Date date = new Date();
    // message += com.yjf.common.lang.util.DateUtil.dtSimpleChineseFormat(date);
    // message += " ";
    // message += com.yjf.common.lang.util.DateUtil.getWeekDay(date);
    message = DateFormat.getDateInstance(DateFormat.FULL).format(date);
    return message;
    }

    public static Date addDate(Date d,long day){
    long time = d.getTime();
    day = day*24*60*60*1000;
    time-=day;
    return new Date(time);
    }

    /**
    * 获取前天
    *
    * @return
    */
    public static Date beforeYesterday() {
    Calendar cad = Calendar.getInstance();
    cad.setTime(new Date());
    cad.add(Calendar.DATE, -2);
    return cad.getTime();
    }

    /**
    * 获取昨天
    * @return
    */
    public static Date yesterday() {
    Calendar cad = Calendar.getInstance();
    cad.setTime(new Date());
    cad.add(Calendar.DATE, -1);
    return cad.getTime();
    }

    /**
    * 获取前?天
    */
    public static Date yesterdayBefore(int n){
    Calendar c=Calendar.getInstance();
    c.setTime(new Date());
    c.add(Calendar.DATE,-n);
    return c.getTime();
    }
    /**
    * 获取一个月前开始的时间
    * @return
    */
    public static Date getOneMonthBegin() {

    Calendar cal = Calendar.getInstance();
    cal.set(Calendar.MONTH, cal.get(Calendar.MONTH) - 1);
    cal.set(Calendar.DATE, 1);
    return cal.getTime();
    }

    /**
    * 获取一个月前结束的时间
    * @return
    */
    public static Date getOneMonthEnd() {

    Calendar cal = Calendar.getInstance();
    cal.set(Calendar.MONTH, cal.get(Calendar.MONTH));
    cal.set(Calendar.DAY_OF_MONTH, 0);
    return cal.getTime();
    }

    /**
    * 获取二个月前开始的时间
    * @return
    */
    public static Date getTwoMonthBegin() {

    Calendar cal = Calendar.getInstance();
    cal.set(Calendar.MONTH, cal.get(Calendar.MONTH) - 2);
    cal.set(Calendar.DATE, 1);
    return cal.getTime();
    }

    /**
    * 获取二个月前结束的时间
    * @return
    */
    public static Date getTwoMonthEnd() {

    Calendar cal = Calendar.getInstance();
    cal.set(Calendar.MONTH, cal.get(Calendar.MONTH) - 1);
    cal.set(Calendar.DAY_OF_MONTH, 0);
    return cal.getTime();
    }

    /**
    * 获取本月开始的时间
    * @return
    */
    public static Date getThisMonthBegin() {

    Calendar cal = Calendar.getInstance();
    cal.set(Calendar.MONTH, cal.get(Calendar.MONTH) );
    cal.set(Calendar.DATE, 1);

    return cal.getTime();
    }

    /**
    * 获取本月初第一天的同一时间
    * @return
    */
    public static Date getMonthBegin() {

    Calendar cal = Calendar.getInstance();
    cal.set(Calendar.DAY_OF_MONTH, 1);
    return cal.getTime();
    }

    /**
    * 获取当月初第一天的同一时间
    * @return
    */
    public static Date getMonthBegin(Date date) {

    Calendar cal = Calendar.getInstance();
    cal.setTime(date);
    cal.set(Calendar.DAY_OF_MONTH, 1);
    return cal.getTime();
    }


    /**
    * 获取该月份的天数
    */
    public static int getDaysOfMonth(Date date){
    Calendar cal = Calendar.getInstance();
    cal.setTime(date);
    return cal.getActualMaximum(Calendar.DATE);

    }

    /**
    * 获取当月莫一天的同一时间
    * @return
    */
    public static Date getDayOfMonth(Date date,int dayN) {
    Calendar cal = Calendar.getInstance();
    cal.setTime(date);
    cal.set(Calendar.DAY_OF_MONTH, dayN);
    return cal.getTime();
    }

    /**
    * 获取当月最后一天的同一时间
    */
    public static Date getMonthEnd(Date date){
    int nDays=DateUtil.getDaysOfMonth(date);
    Calendar cal = Calendar.getInstance();
    cal.setTime(date);
    cal.set(Calendar.DAY_OF_MONTH, nDays);
    return cal.getTime();
    }

    /**
    * 获取?个月前时间
    * @return
    */
    public static Date getNMonth(int n) {

    Calendar cal = Calendar.getInstance();
    cal.set(Calendar.MONTH, cal.get(Calendar.MONTH) - n);

    return cal.getTime();
    }

    /**
    * 字符串转换成日期
    * @param str
    * @return date
    */
    public static Date strToDate(String str) {

    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
    Date date = null;
    try {
    date = format.parse(str);
    } catch (Exception e) {
    e.printStackTrace();
    }
    return date;
    }
    }

  • 相关阅读:
    248.Strobogrammatic Number III
    git Changes not staged for commit,部分修改文件不能确认
    python 文件运行时隐藏命令窗口,图形化界面隐藏cmd窗口
    python 处理配置文件
    odoo 内嵌报表的方式,iframe
    odoo 多个数据库http请求指定数据库
    jasperreportserver 免登录访问,匿名访问报表
    python 图形化界面点击按钮卡死
    python 字符反转
    pentaho 相关介绍
  • 原文地址:https://www.cnblogs.com/xuehen/p/4739584.html
Copyright © 2020-2023  润新知