Java tutorial
/* * Copyright 2012 Kazumune Katagiri. (http://d.hatena.ne.jp/nemuzuka) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, * either express or implied. See the License for the specific language * governing permissions and limitations under the License. */ package jp.co.nemuzuka.utils; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Calendar; import java.util.Date; import java.util.List; import java.util.TimeZone; import jp.co.nemuzuka.core.entity.UserTimeZone; import org.apache.commons.lang.StringUtils; /** * ?Utils. * @author k-katagiri */ public class DateTimeUtils { /** . */ public static final String SUNDAY = "sunday"; /** . */ public static final String MONDAY = "monday"; /** ?. */ public static final String TUESDAY = "tuesday"; /** . */ public static final String WEDNESDAY = "wednesday"; /** . */ public static final String THURSDAY = "thursday"; /** . */ public static final String FRIDAY = "friday"; /** . */ public static final String SATURDAY = "saturday"; /** * ??. * ??????Date???? * @param targetYyyyMM ? * @return index 0:??Date index 1:?Date */ public static List<Date> getStartEndDate(String targetYyyyMM) { SimpleDateFormat sdf = createSdf("yyyyMMdd"); sdf.setLenient(false); List<Date> retList = new ArrayList<Date>(); try { Date startDate = sdf.parse(targetYyyyMM + "01"); retList.add(startDate); //1???1???? Date endDate = addMonths(startDate, 1); endDate = addDays(endDate, -1); retList.add(endDate); } catch (ParseException e) { throw new RuntimeException(e); } return retList; } /** * ?List?. * ?????DateList???? * @param targetYyyyMM ? * @return ?List */ public static List<Date> getStartEndDateList(String targetYyyyMM) { List<Date> startEndList = getStartEndDate(targetYyyyMM); return createDateList(startEndList.get(0), startEndList.get(1)); } /** * ??. * ??????Date???? * ????????????????? * ??????????? * @param targetYyyyMM ? * @return index 0:??Date index 1:?Date */ public static List<Date> getStartEndDate4SunDay(String targetYyyyMM) { List<Date> list = getStartEndDate(targetYyyyMM); Date startDate = list.get(0); Calendar calendar = Calendar.getInstance(); calendar.setTimeZone(getTimeZone()); //?? while (true) { calendar.setTime(startDate); if (calendar.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) { //????? break; } //1?? startDate = addDays(startDate, -1); } Date endDate = list.get(1); //? while (true) { calendar.setTime(endDate); if (calendar.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY) { //????? break; } //1? endDate = addDays(endDate, 1); } List<Date> retList = new ArrayList<Date>(); retList.add(startDate); retList.add(endDate); return retList; } /** * ?List?. * ?????DateList???? * ????????????????? * ??????????? * @param targetYyyyMM ? * @return ?List */ public static List<Date> getStartEndDate4SunDayList(String targetYyyyMM) { List<Date> startEndList = getStartEndDate4SunDay(targetYyyyMM); return createDateList(startEndList.get(0), startEndList.get(1)); } /** * ????? * @param targetDate ? * @param cal Calendar * @return */ public static int getLastDay(Date targetDate, Calendar cal) { cal.setTime(targetDate); return cal.getActualMaximum(Calendar.DATE); } /** * . * ???????????????? * @param targethhmm (HHmm) * @param time (??) * @return */ public static String addTime(String targethhmm, int time) { SimpleDateFormat sdf = createSdf("HHmm"); Date date = null; try { date = sdf.parse(targethhmm); } catch (ParseException e) { throw new RuntimeException(e); } date = addMinutes(date, time); return sdf.format(date); } /** * ?????? * @return ?(yyyyMM?) */ public static String getNextMonth() { //?1 Date date = CurrentDateUtils.getInstance().getCurrentDate(); date = addMonths(date, 1); SimpleDateFormat sdf = createSdf("yyyyMM"); return sdf.format(date); } /** * . * ??? * @param targetYyyyMm (yyyyMM?) * @param amount (?????) * @return ?(yyyyMM?) */ public static String addMonth(String targetYyyyMm, int amount) { SimpleDateFormat sdf = createSdf("yyyyMMdd"); Date date; try { date = sdf.parse(targetYyyyMm + "01"); } catch (ParseException e) { throw new RuntimeException(e); } date = addMonths(date, amount); SimpleDateFormat sdf2 = createSdf("yyyyMM"); return sdf2.format(date); } /** * ????? * @return (yyyyMM?) */ public static String getMonth() { Date date = CurrentDateUtils.getInstance().getCurrentDate(); SimpleDateFormat sdf = createSdf("yyyyMM"); return sdf.format(date); } /** * . * ????????? * = ???1???? * @param startDate * @param endDate * @return ???? */ public static int getDays(Date startDate, Date endDate) { if (startDate.getTime() > endDate.getTime()) { // > ????????? throw new RuntimeException(); } Date targetDate = startDate; int days = 1; while (true) { if (targetDate.getTime() >= endDate.getTime()) { break; } targetDate = addDays(targetDate, 1); days++; } return days; } /** * ?. * ???????????? * From <= To???????????? * @param baseFrom From(HHmm?) * @param baseTo To(HHmm?) * @param targetFrom From(HHmm?) * @param targetTo To(HHmm?) * @return ??true */ public static boolean rangeCheck(String baseFrom, String baseTo, String targetFrom, String targetTo) { if (DateTimeChecker.isHourMinute(baseFrom) == false || DateTimeChecker.isHourMinute(baseTo) == false || DateTimeChecker.isHourMinute(baseFrom) == false || DateTimeChecker.isHourMinute(baseTo) == false) { return false; } int baseFromNum = Integer.valueOf(baseFrom); int baseToNum = Integer.valueOf(baseTo); int targetFromNum = Integer.valueOf(targetFrom); int targetToNum = Integer.valueOf(targetTo); if (baseFromNum > baseToNum || targetFromNum > targetToNum) { return false; } //??FromTo????? if (targetFromNum <= baseFromNum && baseFromNum < targetToNum) { return true; } //??FromTo????? if (targetFromNum < baseToNum && baseToNum <= targetToNum) { return true; } //????? if (baseFromNum <= targetFromNum && targetFromNum <= baseToNum && baseFromNum <= targetToNum && targetToNum <= baseToNum) { return true; } return false; } /** * List?. * ????List???? * @param startDate * @param endDate * @return ?List */ public static List<Date> createDateList(Date startDate, Date endDate) { List<Date> retList = new ArrayList<Date>(); Date addTargetDate = startDate; Date endTargetDate = endDate; while (true) { retList.add(addTargetDate); addTargetDate = addDays(addTargetDate, 1); if (addTargetDate.getTime() > endTargetDate.getTime()) { //?????? break; } } return retList; } /** * ?. * ????? * @param targetHhMm ?(HHmm) * @return ? */ public static int convertMinute(String targetHhMm) { String hour = targetHhMm.substring(0, 2); String min = targetHhMm.substring(2); int hourInt = Integer.parseInt(hour); int minInt = Integer.parseInt(min); return hourInt * 60 + minInt; } /** * . * ?2??????? * @param fromHhMm from * @param toHhMm to * @return ? */ public static int calcMin(String fromHhMm, String toHhMm) { int fromInt = convertMinute(fromHhMm); int toInt = convertMinute(toHhMm); return toInt - fromInt; } /** * ???List???????? * @param targetDate ? * @param dateList List * @return index(????-1) */ public static int getListIndex(Date targetDate, List<Date> dateList) { int index = 0; boolean isBreak = false; for (Date target : dateList) { if (target.getTime() == targetDate.getTime()) { isBreak = true; break; } index++; } if (isBreak) { return index; } return -1; } /** * . * @param time (HHmm) * @return ? */ public static String formatTime(String time) { String hh = time.substring(0, 2); String mm = time.substring(2); return hh + ":" + mm; } /** * . * ????? * @param birthDay * @param baseDate * @return */ public static int calcAge(Date birthDay, Date baseDate) { if (birthDay == null || baseDate == null) { throw new IllegalArgumentException("????"); } SimpleDateFormat sdf = createSdf("yyyyMMdd"); int val1 = Integer.valueOf(sdf.format(baseDate)); int val2 = Integer.valueOf(sdf.format(birthDay)); return (val1 - val2) / 10000; } /** * SimpleDateForm?. * ThreadLocal????SimpleDateFormat???? * ???JST??? * @param pattern * @return SimpleDateFormat */ public static SimpleDateFormat createSdf(String pattern) { SimpleDateFormat sdf = new SimpleDateFormat(pattern); sdf.setTimeZone(getTimeZone()); return sdf; } /** * . * ????? * @param date * @param amount * @return ????? */ public static Date addMonths(Date date, int amount) { return add(date, Calendar.MONTH, amount); } /** * . * ????? * @param date * @param amount * @return????? */ public static Date addDays(Date date, int amount) { return add(date, Calendar.DAY_OF_MONTH, amount); } /** * . * ????? * @param date * @param amount * @return ????? */ public static Date addWeeks(Date date, int amount) { return add(date, Calendar.WEEK_OF_YEAR, amount); } /** * . * ????? * @param date * @param amount * @return????? */ public static Date addMinutes(Date date, int amount) { return add(date, Calendar.MINUTE, amount); } /** * . * ???????????? * DateUtils???????????????? * @param date * @param calendarField ? * @param amount * @return */ private static Date add(Date date, int calendarField, int amount) { if (date == null) { throw new IllegalArgumentException("The date must not be null"); } Calendar c = Calendar.getInstance(getTimeZone()); c.setTime(date); c.add(calendarField, amount); return c.getTime(); } /** * ?. * @return ThreadLocal???? */ private static TimeZone getTimeZone() { String timeZone = UserTimeZone.timeZone.get(); if (StringUtils.isEmpty(timeZone)) { timeZone = jp.co.nemuzuka.common.TimeZone.GMT_P_9.getCode(); } return TimeZone.getTimeZone(timeZone); } }