Merge branch 'DAO'
[proteocache.git] / datadb / compbio / cassandra / DateFormatter.java
diff --git a/datadb/compbio/cassandra/DateFormatter.java b/datadb/compbio/cassandra/DateFormatter.java
new file mode 100644 (file)
index 0000000..471f2a8
--- /dev/null
@@ -0,0 +1,62 @@
+package compbio.cassandra;
+
+import java.text.ParseException;
+import java.text.SimpleDateFormat;
+import java.util.Date;
+
+public class DateFormatter {
+       private final static SimpleDateFormat formatYYMMDD = new SimpleDateFormat("yyyy/MM/dd");
+       private final static SimpleDateFormat formatDDMMYY = new SimpleDateFormat("dd/MM/yyyy");
+       
+       /*
+        * convert String date into long date (miliseconds since the epoch start)
+        */
+       public static long DateParsing(String datInput, SimpleDateFormat formatter) {
+               if (datInput == null) {
+                       return 0;
+               }
+               long dateWorkSt = 0;
+
+               try {
+                       dateWorkSt = formatter.parse(datInput).getTime();
+               } catch (ParseException e) {
+                       e.printStackTrace();
+               }
+               return dateWorkSt;
+       }
+       
+       /*
+        * date validator
+        * true - if valid date, false - if invalid
+        */
+       public static boolean isThisDateValid(String dateToValidate, SimpleDateFormat sdf) {
+               if (dateToValidate == null || dateToValidate.equals("")) {
+                       return false;
+               }
+               try {
+                       // if not valid, this will throw ParseException
+                       sdf.setLenient(false);
+                       Date date = sdf.parse(dateToValidate);
+               } catch (ParseException e) {
+                       e.printStackTrace();
+                       return false;
+               }
+               return true;
+       }
+       
+       /*
+        * convert date from long to String
+        */
+       public static String DateLongToString(long indate, SimpleDateFormat formatter) {
+               String dateString = formatter.format(new Date(indate));
+               return dateString;
+       }
+       
+       public static SimpleDateFormat getFormatYYMMDD() {
+               return formatYYMMDD;
+       }
+       
+       public static SimpleDateFormat getFormatDDMMYY() {
+               return formatDDMMYY;
+       }
+}