001 /*
002 * Copyright 2010 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016 package com.hs.mail.imap.schedule;
017
018 import java.util.Date;
019
020 import org.apache.commons.lang.time.DateUtils;
021
022 /**
023 *
024 * @author Won Chul Doh
025 * @since Oct 2, 2010
026 *
027 */
028 public class ScheduleUtils {
029
030 public static Date getDateBefore(String str) {
031 if (str != null) {
032 int sz = str.length();
033 for (int i = 0; i < sz; i++) {
034 char ch = str.charAt(i);
035 if (!Character.isDigit(ch)) {
036 try {
037 int amount = Integer.parseInt(str.substring(0, i));
038 switch (Character.toUpperCase(ch)) {
039 case 'D':
040 return DateUtils.addDays(new Date(), -amount);
041 case 'M':
042 return DateUtils.addMonths(new Date(), -amount);
043 case 'Y':
044 return DateUtils.addYears(new Date(), -amount);
045 }
046 } catch (NumberFormatException e) {
047 break;
048 }
049 }
050 }
051 }
052 return null;
053 }
054
055 public static Date getTimeAfter(String str, Date defaultTime) {
056 if (str != null) {
057 int sz = str.length();
058 for (int i = 0; i < sz; i++) {
059 char ch = str.charAt(i);
060 if (!Character.isDigit(ch)) {
061 try {
062 int amount = Integer.parseInt(str.substring(0, i));
063 switch (Character.toUpperCase(ch)) {
064 case 'H':
065 return DateUtils.addHours(new Date(), amount);
066 case 'M':
067 return DateUtils.addMinutes(new Date(), amount);
068 }
069 } catch (NumberFormatException e) {
070 break;
071 }
072 }
073 }
074 }
075 return defaultTime;
076 }
077
078 }