KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > service > calendar > RecurrenceUtil


1 /*
2  * $Id: RecurrenceUtil.java 5462 2005-08-05 18:35:48Z jonesde $
3  *
4  * Copyright (c) 2001 The Open For Business Project - www.ofbiz.org
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
21  * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  *
24  */

25 package org.ofbiz.service.calendar;
26
27 import java.text.ParsePosition JavaDoc;
28 import java.text.SimpleDateFormat JavaDoc;
29 import java.util.ArrayList JavaDoc;
30 import java.util.Calendar JavaDoc;
31 import java.util.Date JavaDoc;
32 import java.util.Iterator JavaDoc;
33 import java.util.List JavaDoc;
34
35 /**
36  * Recurrence Utilities
37  *
38  * @author <a HREF="mailto:jaz@ofbiz.org">Andy Zeneski</a>
39  * @version $Rev: 5462 $
40  * @since 2.0
41  */

42 public class RecurrenceUtil {
43
44     /** Returns a Date object from a String. */
45     public static Date JavaDoc parseDate(String JavaDoc dateStr) {
46         String JavaDoc formatString = new String JavaDoc();
47
48         if (dateStr.length() == 16)
49             dateStr = dateStr.substring(0, 14);
50         if (dateStr.length() == 15)
51             formatString = "yyyyMMdd'T'hhmmss";
52         if (dateStr.length() == 8)
53             formatString = "yyyyMMdd";
54
55         SimpleDateFormat JavaDoc formatter = new SimpleDateFormat JavaDoc(formatString);
56         ParsePosition JavaDoc pos = new ParsePosition JavaDoc(0);
57
58         return formatter.parse(dateStr, pos);
59     }
60
61     /** Returns a List of parsed date strings. */
62     public static List JavaDoc parseDateList(List JavaDoc dateList) {
63         List JavaDoc newList = new ArrayList JavaDoc();
64
65         if (dateList == null)
66             return newList;
67         Iterator JavaDoc i = dateList.iterator();
68
69         while (i.hasNext())
70             newList.add(parseDate((String JavaDoc) i.next()));
71         return newList;
72     }
73
74     /** Returns a String from a Date object */
75     public static String JavaDoc formatDate(Date JavaDoc date) {
76         String JavaDoc formatString = new String JavaDoc();
77         Calendar JavaDoc cal = Calendar.getInstance();
78
79         cal.setTime(date);
80         if (cal.isSet(Calendar.MINUTE))
81             formatString = "yyyyMMdd'T'hhmmss";
82         else
83             formatString = "yyyyMMdd";
84         SimpleDateFormat JavaDoc formatter = new SimpleDateFormat JavaDoc(formatString);
85
86         return formatter.format(date);
87     }
88
89     /** Returns a Llist of date strings from a List of Date objects */
90     public static List JavaDoc formatDateList(List JavaDoc dateList) {
91         List JavaDoc newList = new ArrayList JavaDoc();
92         Iterator JavaDoc i = dateList.iterator();
93
94         while (i.hasNext())
95             newList.add(formatDate((Date JavaDoc) i.next()));
96         return newList;
97     }
98
99     /** Returns the time as of now. */
100     public static long now() {
101         return (new Date JavaDoc()).getTime();
102     }
103
104 }
105
106
Popular Tags