技術ブログを書いたほうがいいということで書き綴ってみた

技術ブログを書いたほうがいいということで書き綴ってみた

 public String checkDate(String argDate ){
        String retDate = null;
        DateFormat dateFormatter = new SimpleDateFormat("yyyyMMdd");
        Calendar cal = Calendar.getInstance();
        TimeZone jst = TimeZone.getTimeZone("JST");
        cal.setTimeZone(jst);
        if( argDate == null || "".equals(argDate)){
            cal.add(Calendar.DAY_OF_MONTH,-this.mBeforeday);
            retDate = dateFormatter.format(cal.getTime());
        }else{
            try {
                dateFormatter.setLenient(false);
                Date tmpDate = dateFormatter.parse(argDate);
                if(!dateFormatter.format(tmpDate).equalsIgnoreCase(argDate)){
                    return null;
                }
                cal.add(Calendar.DAY_OF_MONTH,-1);
                String yesterday = dateFormatter.format(cal.getTime());
                retDate = argDate;
                if(yesterday.compareTo(retDate)<0){
                    return null;
                }
                convertGMTTime(argDate);
            } catch (ParseException e) {
                return null;
            }
        }
        return retDate;
    }
    public void convertGMTTime(String argDate ) throws ParseException{
        DateFormat dateFormatter = new SimpleDateFormat("yyyyMMdd HH:mm:ss");
        dateFormatter.setTimeZone(TimeZone.getTimeZone("JST"));
        String strStart = argDate + " 00:00:00";
        Date startDate = dateFormatter.parse(strStart);

        Calendar nextCal = Calendar.getInstance();
        nextCal.setTime(startDate);
        nextCal.add(Calendar.DAY_OF_MONTH, 1);
        Date endDate = null;
        endDate = nextCal.getTime();

        DateFormat dateFormatter2 = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        dateFormatter2.setTimeZone(TimeZone.getTimeZone("GMT"));

        String mStart = dateFormatter2.format(startDate);
        String mEnd = dateFormatter2.format(endDate);
        System.out.println(mStart);
        System.out.println(mEnd);
    }
    public void convertGMTTime2(String argDate) throws ParseException {
        // パーサー
        DateFormat parser = new SimpleDateFormat("yyyyMMdd");
        parser.setTimeZone(TimeZone.getTimeZone("GMT"));
        // フォーマッター
        DateFormat formatter = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        formatter.setTimeZone(TimeZone.getTimeZone("GMT"));

        // 開始日時取得
        Date startDate = parser.parse(argDate);
        String mStart = formatter.format(startDate);

        // 終了日時取得
        Calendar nextCal = Calendar.getInstance();
        nextCal.setTime(startDate);
        nextCal.add(Calendar.DAY_OF_MONTH, 1);
        nextCal.add(Calendar.MILLISECOND, -1);
        String mEnd = formatter.format(nextCal.getTime());

        System.out.println(mStart);
        // => 2015/10/12 00:00:00
        System.out.println(mEnd);
        // => 2015/10/12 23:59:59
    }