001: /*
002: * $Id: RecurrenceUtil.java,v 1.1 2003/08/17 05:12:42 ajzeneski Exp $
003: *
004: * Copyright (c) 2001 The Open For Business Project - www.ofbiz.org
005: *
006: * Permission is hereby granted, free of charge, to any person obtaining a
007: * copy of this software and associated documentation files (the "Software"),
008: * to deal in the Software without restriction, including without limitation
009: * the rights to use, copy, modify, merge, publish, distribute, sublicense,
010: * and/or sell copies of the Software, and to permit persons to whom the
011: * Software is furnished to do so, subject to the following conditions:
012: *
013: * The above copyright notice and this permission notice shall be included
014: * in all copies or substantial portions of the Software.
015: *
016: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
017: * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
018: * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
019: * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
020: * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
021: * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
022: * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
023: *
024: */
025: package org.ofbiz.service.calendar;
026:
027: import java.text.ParsePosition;
028: import java.text.SimpleDateFormat;
029: import java.util.ArrayList;
030: import java.util.Calendar;
031: import java.util.Date;
032: import java.util.Iterator;
033: import java.util.List;
034:
035: /**
036: * Recurrence Utilities
037: *
038: * @author <a href="mailto:jaz@ofbiz.org">Andy Zeneski</a>
039: * @version $Revision: 1.1 $
040: * @since 2.0
041: */
042: public class RecurrenceUtil {
043:
044: /** Returns a Date object from a String. */
045: public static Date parseDate(String dateStr) {
046: String formatString = new String();
047:
048: if (dateStr.length() == 16)
049: dateStr = dateStr.substring(0, 14);
050: if (dateStr.length() == 15)
051: formatString = "yyyyMMdd'T'hhmmss";
052: if (dateStr.length() == 8)
053: formatString = "yyyyMMdd";
054:
055: SimpleDateFormat formatter = new SimpleDateFormat(formatString);
056: ParsePosition pos = new ParsePosition(0);
057:
058: return formatter.parse(dateStr, pos);
059: }
060:
061: /** Returns a List of parsed date strings. */
062: public static List parseDateList(List dateList) {
063: List newList = new ArrayList();
064:
065: if (dateList == null)
066: return newList;
067: Iterator i = dateList.iterator();
068:
069: while (i.hasNext())
070: newList.add(parseDate((String) i.next()));
071: return newList;
072: }
073:
074: /** Returns a String from a Date object */
075: public static String formatDate(Date date) {
076: String formatString = new String();
077: Calendar cal = Calendar.getInstance();
078:
079: cal.setTime(date);
080: if (cal.isSet(Calendar.MINUTE))
081: formatString = "yyyyMMdd'T'hhmmss";
082: else
083: formatString = "yyyyMMdd";
084: SimpleDateFormat formatter = new SimpleDateFormat(formatString);
085:
086: return formatter.format(date);
087: }
088:
089: /** Returns a Llist of date strings from a List of Date objects */
090: public static List formatDateList(List dateList) {
091: List newList = new ArrayList();
092: Iterator i = dateList.iterator();
093:
094: while (i.hasNext())
095: newList.add(formatDate((Date) i.next()));
096: return newList;
097: }
098:
099: /** Returns the time as of now. */
100: public static long now() {
101: return (new Date()).getTime();
102: }
103:
104: }
|