Java时间日期相关开发

选择框有近3个月,最近半年等,传入数值,然后转换成时间格式传入后台

事例:

private OrderSearchVO getSearchOrder(OrderSearchVO orderSearchVO)

    {
        String thisTime = DateUtils.formatDate(System.currentTimeMillis(), "yyyy-MM-dd");
        SimpleDateFormat matter = new SimpleDateFormat("yyyy-MM-dd");
        Calendar c = Calendar.getInstance();

        // 最近三个月
        if (orderSearchVO.getTimeSelect() == 3)
        {
            c.add(Calendar.MONTH, -3);
            orderSearchVO.setStartCreateTime(matter.format(c.getTime()));
            orderSearchVO.setEndCreateTime(thisTime);
        }
        // 最近六个月
        else if (orderSearchVO.getTimeSelect() == 6)
        {
            c.add(Calendar.MONTH, -6);
            orderSearchVO.setStartCreateTime(matter.format(c.getTime()));
            orderSearchVO.setEndCreateTime(thisTime);
        }
        // 今年内
        else if (orderSearchVO.getTimeSelect() == 12)
        {
            orderSearchVO.setStartCreateTime(DateUtils.formatDate(System.currentTimeMillis(), "yyyy")+"-01-01");
            orderSearchVO.setEndCreateTime(thisTime);
        }
        // 2014年
        else if (orderSearchVO.getTimeSelect() == 24)
        {
            orderSearchVO.setStartCreateTime("2014-01-01");
            orderSearchVO.setEndCreateTime("2014-12-31");
        }
        // 2014年以前
        else if (orderSearchVO.getTimeSelect() == 48)
        {
            orderSearchVO.setEndCreateTime("2014-12-31");
        }

        return orderSearchVO;
    }

    /**
     * @description: 获取范围时间的开始与结束
     * @param times
     * @return
     * @author: wangchong
     * @createTime:2015年6月18日 上午8:36:29
     */
    private Map<String, Object> formatTime(String[] times)
    {
        Map<String, Object> returnMap = new HashMap<String, Object>();

        String startTime = times[0].trim().replaceAll("/", "-");
        String endTime = times[1].trim().replaceAll("/", "-");

        returnMap.put("startTime", startTime);
        returnMap.put("endTime", endTime);

        return returnMap;
    }


/**
     * 判断当前是否是开市,true是开市,false是闭市
     *
     * @param date
     * @return
     */
    @SuppressWarnings("deprecation")
    public Boolean isHolidayDate(long date)
    {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String dateTime = format.format(date);

        Map<String, Object> map = new HashMap<String, Object>();
        map.put("holiday", dateTime);
        map.put("type", 0);
        int count = holidayDao.countHolidayDate(map);

        Map<String, Object> fwMap = new HashMap<String, Object>();
        fwMap.put("holiday", dateTime);
        fwMap.put("type", 1);

        //某一天被设为节假日,进行统计
        int fwCount = holidayDao.countHolidayDate(fwMap);

        Date day = null;
        try
        {
            day = format.parse(format.format(date));
        }
        catch (ParseException e)
        {
            e.printStackTrace();
        }

        if (count > 0)  // 设定为节假日 闭市
        {
            return false;
        }
        else if (fwCount == 0 && (day.getDay() == 0 || day.getDay() == 6)) // 双休 且没有设为工作日 闭市
        {
            return false;
        }
        else
        {
            SimpleDateFormat formatTime = new SimpleDateFormat("HH:mm");
            SystemParameter spStart = systemParameterDao.findparamNameById("startTime");
            SystemParameter spEnd = systemParameterDao.findparamNameById("endTime");

            Date time = null;
            Date startTime = null;
            Date endTime = null;
            try
            {
                time = formatTime.parse(formatTime.format(date));
                startTime = formatTime.parse(spStart.getParamValue());
                endTime = formatTime.parse(spEnd.getParamValue());
            }
            catch (ParseException e)
            {
                e.printStackTrace();
            }

            if (time.after(startTime) && time.before(endTime))   // 开闭市时间
            {
                return true;
            }

            else
            {
                return false;
            }
        }
    }

    @SuppressWarnings("deprecation")
    public Boolean isHoliday(long date)
    {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String dateTime = format.format(date);

        Map<String, Object> map = new HashMap<String, Object>();
        map.put("holiday", dateTime);
        map.put("type", 0);
        int count = holidayDao.countHolidayDate(map);

        Map<String, Object> fwMap = new HashMap<String, Object>();
        fwMap.put("holiday", dateTime);
        fwMap.put("type", 1);
        int fwCount = holidayDao.countHolidayDate(fwMap);

        Date day = null;
        try
        {
            day = format.parse(format.format(date));
        }
        catch (ParseException e)
        {
            e.printStackTrace();
        }

        if (count > 0)
        {
            return false;
        }
        else if (fwCount == 0 && (day.getDay() == 0 || day.getDay() == 6))
        {
            return false;
        }
        return true;
    }

    /**
     * 获得上个工作日的同时刻时间
     *
     * @return
     */
    @Override
    public long getLastWorkDate()
    {
        long currentTime = System.currentTimeMillis();
        long lastWorkDate = currentTime - 86400000;
        while (!isHoliday(lastWorkDate))
        {
            lastWorkDate = lastWorkDate - 86400000;
        }
        return lastWorkDate;
    }

DateUtil日期工具类

package cn.mysteel.util;

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

public abstract class DateUtils
{
  public static final String DATE_FROMAT1 = "yyyy-MM-dd";
  public static final String DATE_FROMAT2 = "yyyy-MM-dd HH:mm:ss";

  public static Date getDate(String s)
  {
    return getDate(s, null);
  }

  public static Date getJustDate(String s)
  {
    return getDate(s, "yyyy-MM-dd");
  }

  public static Date getDate(long date)
  {
    return getDate(date, null);
  }

  public static Date getJustDate(long date)
  {
    return getDate(date, "yyyy-MM-dd");
  }

  public static Date getDate(long date, String format)
  {
    if (StringUtils.isEmpty(format))
    {
      format = "yyyy-MM-dd HH:mm:ss";
    }

    return getDate(formatDate(new Date(date), format), format);
  }

  public static Date getDate(String s, String format)
  {
    Date date;
    try
    {
      if (StringUtils.isEmpty(format))
      {
        format = "yyyy-MM-dd HH:mm:ss";
      }

      date = new SimpleDateFormat(format).parse(s);
    }
    catch (Exception e)
    {
      Date date;
      date = new Date(0L);
    }

    return date;
  }

  public static String formatDate(long date, String format)
  {
    return formatDate(new Date(date), format);
  }

  public static String formatDate(long date)
  {
    return formatDate(new Date(date), null);
  }

  public static String formatJustDate(long date)
  {
    return formatDate(new Date(date), "yyyy-MM-dd");
  }

  public static String formatDate(Date date, String format)
  {
    if (StringUtils.isEmpty(format))
    {
      format = "yyyy-MM-dd HH:mm:ss";
    }

    return new SimpleDateFormat(format).format(date);
  }
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值