KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > outerj > daisy > repository > query > QueryHelper


1 /*
2  * Copyright 2004 Outerthought bvba and Schaubroeck nv
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.outerj.daisy.repository.query;
17
18 import java.util.Date JavaDoc;
19 import java.text.SimpleDateFormat JavaDoc;
20
21 /**
22  * Helper methods for correctly formatting values when programatically assembling queries.
23  */

24 public final class QueryHelper {
25     private static final String JavaDoc QUOTE = "'";
26
27     public static String JavaDoc formatDateTime(Date JavaDoc date) {
28         SimpleDateFormat JavaDoc dateTimeFormat = new SimpleDateFormat JavaDoc("yyyy-MM-dd HH:mm:ss");
29         return QUOTE + dateTimeFormat.format(date) + QUOTE;
30     }
31
32     public static String JavaDoc formatDate(Date JavaDoc date) {
33         SimpleDateFormat JavaDoc dateTimeFormat = new SimpleDateFormat JavaDoc("yyyy-MM-dd");
34         return QUOTE + dateTimeFormat.format(date) + QUOTE;
35     }
36
37     public static String JavaDoc formatTime(Date JavaDoc date) {
38         SimpleDateFormat JavaDoc dateTimeFormat = new SimpleDateFormat JavaDoc("HH:mm:ss");
39         return QUOTE + dateTimeFormat.format(date) + QUOTE;
40     }
41
42     public static String JavaDoc formatString(String JavaDoc text) {
43         StringBuffer JavaDoc escaped = new StringBuffer JavaDoc(text.length() + 10);
44         for (int i = 0; i < text.length(); i++) {
45             char c = text.charAt(i);
46             if (c == '\'') {
47                 escaped.append("''");
48             } else {
49                 escaped.append(c);
50             }
51         }
52         return QUOTE + escaped + QUOTE;
53     }
54
55 }
56
Popular Tags