Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: dods/util/Util.java


1   package dods.util;
2   
3   
4   /*-----------------------------------------------------------------------------
5    * Enhydra Java Application Server
6    * Copyright 1997-1999 Lutris Technologies, Inc.
7    * All rights reserved.
8    *
9    * Redistribution and use in source and binary forms, with or without
10   * modification, are permitted provided that the following conditions
11   * are met:
12   * 1. Redistributions of source code must retain the above copyright
13   *    notice, this list of conditions and the following disclaimer.
14   * 2. Redistributions in binary form must reproduce the above copyright
15   *    notice, this list of conditions and the following disclaimer in
16   *    the documentation and/or other materials provided with the distribution.
17   * 3. All advertising materials mentioning features or use of this software
18   *    must display the following acknowledgement:
19   *      This product includes Enhydra software developed by Lutris
20   *      Technologies, Inc. and its contributors.
21   * 4. Neither the name of Lutris Technologies nor the names of its contributors
22   *    may be used to endorse or promote products derived from this software
23   *    without specific prior written permission.
24   *
25   * THIS SOFTWARE IS PROVIDED BY LUTRIS TECHNOLOGIES AND CONTRIBUTORS ``AS IS''
26   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28   * ARE DISCLAIMED.  IN NO EVENT SHALL LUTRIS TECHNOLOGIES OR CONTRIBUTORS BE
29   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35   * POSSIBILITY OF SUCH DAMAGE.
36   *
37   *-----------------------------------------------------------------------------
38   *
39   * This class Copyright 1999 Plugged In Communications Pty Ltd 
40   * All rights reserved. Released under Enhydra Licence 10 August 1999
41   *
42   *-----------------------------------------------------------------------------
43   *
44   * Author      : Chris Ryan (cryan@pisoftware.com)
45   *
46   * Description : This class provides various static convenience methods
47   *               for often used processes.
48   *
49   */
50  
51  import java.text.*;
52  
53  public final class Util {
54  
55  
56  /**
57   * Constructor.  Don't want anyone to invoke one of these.
58   */
59    protected Util() {
60  
61    }
62  
63  
64  /**
65   * This method replaces all instances of one string to
66   * another string within the main string.  For example,
67   * if the main string is "Foo bar baz", and the string
68   * to replace is "ba" with "cu", then the returned
69   * result will be "Foo cur cuz".
70   *
71   * @param main_value  The value to do the replacing on
72   * @param search_value  The value to search for
73   * @param sub_value  The value to replace any found values
74   * @return see comment.
75   */
76    public static String replace(String main_value, String search_value, String sub_value) {
77  
78      int pos = 0;
79      int old_pos = 0;
80      StringBuffer result = new StringBuffer();
81  
82      while ((old_pos < main_value.length()) && ((pos = main_value.indexOf(search_value, old_pos)) != -1)) {
83  
84        // add everything from the old position to here
85        if (pos != old_pos) 
86          result.append(main_value.substring(old_pos, pos));
87  
88        // add the replacement value
89        result.append(sub_value);
90  
91        // move the old position along
92        old_pos = pos + search_value.length();
93  
94      }
95  
96      // did we get to the end of the string?
97      if (old_pos <= main_value.length()) {
98  
99        // yes, add the remainder to the result
100       result.append(main_value.substring(old_pos));
101 
102     }
103 
104     return result.toString();
105 
106   }
107 
108 
109 /**
110  * This method attempts to convert the string passed into
111  * a date.
112  *
113  * @param datestr  The string to convert
114  * @return The converted string, or null if any errors occur
115  */
116   public static java.util.Date stringToDate(String datestr) {
117 
118     // basic checks...
119     if (datestr == null || datestr.length() == 0)
120       return null;
121 
122     // These are the date formats we can accept
123     String[] formats = { 
124       "yyyy/MM/dd H:mm:ss a",    "yyyy/MM/dd K:mm:ss",
125       "d/M/yyyy H:mm:ss a",    "d/M/yyyy K:mm:ss",
126       "MM/dd/yyyy H:mm:ss a",    "MM/dd/yyyy K:mm:ss",
127       "yyyy-MM-dd H:mm:ss a",    "yyyy MM dd H:mm:ss a", 
128       "yyyy-MM-dd H:mm:ss",    "yyyy MM dd H:mm:ss",
129       "yyyy-MM-dd H:mm a",    "yyyy MM dd H:mm a",
130       "yyyy-MM-dd H:mm",    "yyyy MM dd H:mm",
131       "yyyy/MM/dd",      "dd/MM/yyyy",
132       "yyyy-MM-dd",      "yyyy MM dd",
133       "MMM dd yyyy h:mm:ss a",  "MMM dd, yyyy h:mm:ss a",
134       "MMM dd yyyy h:mm:ss",    "MMM dd, yyyy h:mm:ss", 
135       "MMM dd yyyy h:mm a",    "MMM dd, yyyy h:mm a",
136       "MMM dd yyyy h:mm",    "MMM dd, yyyy h:mm" };
137 
138     java.util.Date result = null;
139 
140     // try each format...
141     for ( int i = 0; i < formats.length; i++ ) {
142 
143       // create the date formatter with this format string
144       SimpleDateFormat df = new SimpleDateFormat( formats[ i ] );
145 
146       try {
147 
148         // parse the string with this date formatter
149         result = df.parse( datestr );
150 
151         // jump out if we get here, since we've got a valid result
152         break;
153       } catch ( ParseException e ) {
154 
155         // ignore this - if the string can't be parsed we'll get this exception
156       }
157 
158     }
159 
160     // return the parsed date... hopefully
161     return result;
162 
163   }
164 
165 
166 }