com.castis.xylophone.adsmadapter.common.util.CiDateUtil.java Source code

Java tutorial

Introduction

Here is the source code for com.castis.xylophone.adsmadapter.common.util.CiDateUtil.java

Source

package com.castis.xylophone.adsmadapter.common.util;

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

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.castis.tambourine.enumeration.InventoryBoxDayCode;
import com.castis.tambourine.util.DateUtil;

public class CiDateUtil {

    static final Log log = LogFactory.getLog(CiDateUtil.class);

    public static int countDayInLicense(String startDate, String endDate, InventoryBoxDayCode dayCode) {
        Calendar start = convertCalendar(startDate, "yyyy-MM-dd");
        Calendar end = convertCalendar(endDate, "yyyy-MM-dd");
        return countDay(start, end, dayCode);
    }

    public static int countDay(Calendar start, Calendar end, InventoryBoxDayCode dayCode) {

        int count = 0;

        while (start.compareTo(end) <= 0) {

            int day = start.get(Calendar.DAY_OF_WEEK);

            if (dayCode != null) {
                if (Calendar.MONDAY <= day && Calendar.FRIDAY >= day) {
                    if (dayCode.equals(InventoryBoxDayCode.WEEKDAY))
                        count++;
                } else {
                    if (dayCode.equals(InventoryBoxDayCode.WEEKEND))
                        count++;
                }
            } else {
                count++;
            }

            start.add(Calendar.DAY_OF_MONTH, 1);
        }

        return count;
    }

    public static boolean checkFutureDate(String date, String format) {
        if (date == null || date.isEmpty() == true)
            return false;

        Date tmpDate = convertDate(date, format);
        Date curDate = DateUtil.getCurDate();

        return tmpDate.after(curDate);
    }

    public static Date convertDate(String srcDateString, String format) {
        Date tmpDate = new Date();
        SimpleDateFormat simple = new SimpleDateFormat(format);
        try {
            tmpDate = simple.parse(srcDateString);
        } catch (ParseException e) {
            log.warn("Parse Exception : src-" + srcDateString + ", format-" + format);
            return null;
        }
        return tmpDate;
    }

    public static Calendar convertCalendar(String srcDateString, String format) {
        Date tmpDate = new Date();
        SimpleDateFormat simple = new SimpleDateFormat(format);
        try {
            tmpDate = simple.parse(srcDateString);
        } catch (ParseException e) {
            log.warn("Parse Exception : src-" + srcDateString + ", format-" + format);
            return null;
        }
        Calendar cal = Calendar.getInstance();
        cal.setTime(tmpDate);

        return cal;
    }

    /**
     * format : "yyyy-MM-dd HH:mm:ss.SSS"
     * */
    public static String getDateString(String format) {

        return getDateString(new Date(), format);
    }

    /**
     * format : "yyyy-MM-dd HH:mm:ss.SSS"
     * */
    public static String getDateString(long date, String format) {
        return getDateString(new Date(date), format);
    }

    /**
     * format : "yyyy-MM-dd HH:mm:ss.SSS"
     * */
    public static String getDateString(Date date, String format) {

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
        String dateString = simpleDateFormat.format(date);

        return dateString;
    }

}