KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > sellwin > db > DateUtils


1 package sellwin.db;
2
3 import java.text.*;
4 import sellwin.utils.*;
5
6 // SellWin http://sourceforge.net/projects/sellwincrm
7
//Contact support@open-app.com for commercial help with SellWin
8
//This software is provided "AS IS", without a warranty of any kind.
9

10
11 /**
12  * some simple routines to format java dates into
13  * Oracle 8 or MYSQL compliant date and date-time strings
14  * for use in building SQL statements in Java
15  */

16 public class DateUtils {
17     private static SimpleDateFormat mysqlFormatter =
18         new SimpleDateFormat("yyyy-MM-dd");
19     private static SimpleDateFormat mysqlDateTimeFormatter =
20         new SimpleDateFormat("yyyy-MM-dd HH:mm");
21
22     private static SimpleDateFormat oracleFormatter =
23         new SimpleDateFormat("dd-MMM-yy");
24     private static SimpleDateFormat oracleDateTimeFormatter =
25         new SimpleDateFormat("dd-MMM-yyyy HH:mm");
26
27     /**
28      * build a string in 'dd-mmm-yy' format using
29      * a Java date
30      * @param dt the java.util.Date to convert
31      * @return the formatted String
32      */

33     public static String JavaDoc format(int DB_TYPE, java.util.Date JavaDoc dt) {
34         if (DB_TYPE == Prefs.ORACLE)
35             return oracleFormatter.format(dt);
36         else
37             return mysqlFormatter.format(dt);
38     }
39
40     /**
41      * build a formatted string that contains an
42      * Oracle date-time compliant string
43      * e.g. insert into junk values (to_date('01-may-01 18:19', 'dd-mon-YY hh24:mi'));
44      * @param dt the Date to convert
45      * @return the formatted String
46      */

47     public static String JavaDoc formatDateTime(int DB_TYPE, java.util.Date JavaDoc dt) {
48         if (DB_TYPE == Prefs.ORACLE)
49             return "to_date('" + oracleDateTimeFormatter.format(dt) + "','dd-mon-yyyy hh24:mi')";
50         else
51             return "'" + mysqlDateTimeFormatter.format(dt) + "'";
52     }
53 }
54
Popular Tags