jp.co.golorp.emarf.util.DateUtil.java Source code

Java tutorial

Introduction

Here is the source code for jp.co.golorp.emarf.util.DateUtil.java

Source

/*
 * Apache License, Version 2.0???????
 * ?????????????
 *
 * ??http://www.apache.org/licenses/LICENSE-2.0?????
 *
 * ???????????????
 * ????????
 * ??????????????????????
 *
 * ????????????????????????
 */
package jp.co.golorp.emarf.util;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.time.DateUtils;

/**
 * 
 *
 * @author oukuf@golorp
 */
public class DateUtil extends DateUtils {

    /** ID? */
    private static final ThreadLocal<String> SESSION_ID = new ThreadLocal<String>();

    /**
     * @param sessionId
     *            ??ID
     */
    public static final void setSessionId(final String sessionId) {
        SESSION_ID.set(sessionId);
    }

    /** ? */
    private static String serverDateValue;

    /**  */
    private static final Map<String, Calendar> SESSION_DATE = new HashMap<String, Calendar>();

    /**
     * ?
     *
     * @param datetime
     *            
     * @return ??
     */
    public static final boolean setServerDate(final String datetime) {

        try {

            if (StringUtil.isBlank(datetime)) {
                serverDateValue = null;
            } else {
                serverDateValue = datetime;
            }

            return true;

        } catch (Exception e) {
            return false;
        }
    }

    /**
     * ID?
     *
     * @param datetime
     *            
     * @return ??
     */
    public static final boolean setSessionDate(final String datetime) {
        try {
            if (StringUtil.isBlank(datetime)) {
                SESSION_DATE.remove(SESSION_ID.get());
            } else {
                Calendar sessionDate = Calendar.getInstance();
                sessionDate.setTime(ParseUtil.toDate(datetime));
                SESSION_DATE.put(SESSION_ID.get(), sessionDate);
            }
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    /**
     * @return ???????
     */
    public static final Date getDate() {

        // 
        Calendar sessionDate = SESSION_DATE.get(SESSION_ID.get());
        if (sessionDate != null) {
            return sessionDate.getTime();
        }

        // ?
        if (serverDateValue != null) {
            Calendar serverDate = Calendar.getInstance();
            serverDate.setTime(ParseUtil.toDate(serverDateValue));
            return serverDate.getTime();
        }

        // 
        return Calendar.getInstance().getTime();
    }

    /**
     * @return 
     */
    public static final String getYmd() {
        SimpleDateFormat sdf = new SimpleDateFormat();
        sdf.applyPattern("yyyy/MM/dd");
        return sdf.format(getDate());
    }

    /**
     * @return 
     */
    public static final String getYmdHmsS() {
        SimpleDateFormat sdf = new SimpleDateFormat();
        sdf.applyPattern("yyyy/MM/dd HH:mm:ss.SSS");
        return sdf.format(getDate());
    }

    /**
     * @return 
     */
    public static final String getYmdHms() {
        SimpleDateFormat sdf = new SimpleDateFormat();
        sdf.applyPattern("yyyy/MM/dd HH:mm:ss");
        return sdf.format(getDate());
    }

    /**
     * @return 4??
     */
    public static final String getYYYY() {
        SimpleDateFormat sdf = new SimpleDateFormat();
        sdf.applyPattern("yyyy");
        return sdf.format(getDate());
    }

    /**
     * @return 2??
     */
    public static final String getMM() {
        SimpleDateFormat sdf = new SimpleDateFormat();
        sdf.applyPattern("MM");
        return sdf.format(getDate());
    }

    /**
     * @return 1??
     */
    public static final int getM() {
        return Integer.parseInt(getMM());
    }

    /**
     * @return 2??
     */
    public static final String getDD() {
        SimpleDateFormat sdf = new SimpleDateFormat();
        sdf.applyPattern("dd");
        return sdf.format(getDate());
    }

    /**
     * @return 1??
     */
    public static final int getD() {
        return Integer.parseInt(getDD());
    }

    /**
     * @return 2??
     */
    public static final String getHH() {
        SimpleDateFormat sdf = new SimpleDateFormat();
        sdf.applyPattern("HH");
        return sdf.format(getDate());
    }

    /**
     * @return 1??
     */
    public static final int getH() {
        return Integer.parseInt(getHH());
    }

    /**
     * @return 2??
     */
    public static final String getNN() {
        SimpleDateFormat sdf = new SimpleDateFormat();
        sdf.applyPattern("mm");
        return sdf.format(getDate());
    }

    /**
     * @return 1??
     */
    public static final int getN() {
        return Integer.parseInt(getNN());
    }

    /**
     * @param y
     *            
     * @param m
     *            
     * @return 
     */
    public static int getEnd(final int y, final int m) {
        Calendar now = Calendar.getInstance();
        now.set(Calendar.YEAR, y);
        now.set(Calendar.MONTH, m);
        now.set(Calendar.DATE, 0);
        int end = now.get(Calendar.DATE);
        return end;
    }

    /**
     * @param y
     *            
     * @param m
     *            
     * @param d
     *            
     * @return 1:, 2:, 3:?, 4:, 5:, 6:, 7:
     */
    public static int getDayOfWeek(final int y, final int m, final int d) {
        Calendar now = Calendar.getInstance();
        now.set(Calendar.YEAR, y);
        now.set(Calendar.MONTH, m - 1);
        now.set(Calendar.DATE, d);
        int dayOfWeek = now.get(Calendar.DAY_OF_WEEK);
        return dayOfWeek;
    }

    /**
     * @return 1:, 2:, 3:?, 4:, 5:, 6:, 7:
     */
    public static int getDayOfWeek() {
        Calendar now = Calendar.getInstance();
        now.setTime(DateUtil.getDate());
        int dayOfWeek = now.get(Calendar.DAY_OF_WEEK);
        return dayOfWeek;
    }

}