From fd6f407159ccdbf10e57804c84ad932d947b7a3e Mon Sep 17 00:00:00 2001 From: "Christian P. MOMON" Date: Sun, 15 Aug 2010 16:04:13 +0200 Subject: [PATCH] Improve Calendar helpers. --- src/fr/devinsy/util/DateHelper.java | 19 ++-- src/fr/devinsy/util/DateTimeHelper.java | 131 ++++++++++++++++++++++++ 2 files changed, 143 insertions(+), 7 deletions(-) mode change 100755 => 100644 src/fr/devinsy/util/DateHelper.java create mode 100644 src/fr/devinsy/util/DateTimeHelper.java diff --git a/src/fr/devinsy/util/DateHelper.java b/src/fr/devinsy/util/DateHelper.java old mode 100755 new mode 100644 index 8a4dade..bdccd42 --- a/src/fr/devinsy/util/DateHelper.java +++ b/src/fr/devinsy/util/DateHelper.java @@ -13,19 +13,24 @@ import java.util.regex.Pattern; /** * This class groups function to help in Calendar manipulation. + * + * SimpleDateFormat is not used cause does not thread safe? */ public class DateHelper { - public String LINE_SEPARATOR = "\n"; - //static private org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger (DateHelper.class); + static final String EUROPEAN_DATE_FORMAT = "%02/%02/%04"; + static final String RAW_DATE_FORMAT = "%04%02%02"; + static final String ISO_DATE_FORMAT = "%04-%02-%02"; + static final String AMERICAN_DATE_FORMAT = "%02/%02/%04"; + static final String EUROPEAN_DATE_PATTERN = "^([0123]{0,1}\\d)/([01]{0,1}\\d)/(\\d\\d\\d\\d)$"; static final String RAW_DATE_PATTERN = "^(\\d\\d\\d\\d)([01]\\d)([0123]\\d)$"; static final String ISO_DATE_PATTERN = "^(\\d\\d\\d\\d)-([01]\\d)-([0123]\\d)$"; static final String AMERICAN_DATE_PATTERN = "^([01]{0,1}\\d)/([0123]{0,1}\\d)/(\\d\\d\\d\\d)$"; - + /** * */ @@ -39,7 +44,7 @@ public class DateHelper } else { - result = String.format ("%02d/%02d/%04d", time.get(Calendar.DAY_OF_MONTH), time.get(Calendar.MONTH) + 1, time.get(Calendar.YEAR)); + result = String.format (EUROPEAN_DATE_FORMAT, time.get(Calendar.DAY_OF_MONTH), time.get(Calendar.MONTH) + 1, time.get(Calendar.YEAR)); } // @@ -60,7 +65,7 @@ public class DateHelper } else { - result = String.format ("%04d-%02d-%02d", time.get(Calendar.YEAR), time.get(Calendar.MONTH) + 1, time.get(Calendar.DAY_OF_MONTH)); + result = String.format (ISO_DATE_FORMAT, time.get(Calendar.YEAR), time.get(Calendar.MONTH) + 1, time.get(Calendar.DAY_OF_MONTH)); } // @@ -81,7 +86,7 @@ public class DateHelper } else { - result = String.format ("%02d/%02d/%04d", time.get(Calendar.MONTH) + 1, time.get(Calendar.DAY_OF_MONTH), time.get(Calendar.DAY_OF_MONTH)); + result = String.format (AMERICAN_DATE_FORMAT, time.get(Calendar.MONTH) + 1, time.get(Calendar.DAY_OF_MONTH), time.get(Calendar.DAY_OF_MONTH)); } // @@ -102,7 +107,7 @@ public class DateHelper } else { - result = String.format ("%04d%02d%02d", time.get(Calendar.MONTH), time.get(Calendar.DAY_OF_MONTH) + 1, time.get(Calendar.YEAR)); + result = String.format (RAW_DATE_FORMAT, time.get(Calendar.MONTH), time.get(Calendar.DAY_OF_MONTH) + 1, time.get(Calendar.YEAR)); } // diff --git a/src/fr/devinsy/util/DateTimeHelper.java b/src/fr/devinsy/util/DateTimeHelper.java new file mode 100644 index 0000000..259d8cd --- /dev/null +++ b/src/fr/devinsy/util/DateTimeHelper.java @@ -0,0 +1,131 @@ +/** + * @author Christian Momon, June 2010. + * This file is free software under the terms of the GNU Library General Public License + * as published by the Free Software Foundation version 2 or any later version. + */ +package fr.devinsy.util; + +import java.util.Calendar; + + +/** + * This class groups function to help in Calendar manipulation. + */ +public class DateTimeHelper +{ + //static private org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger (DateTimeHelper.class); + + static final String EUROPEAN_DATE_FORMAT = "%02/%02/%04 %02:%02:%02"; + static final String RAW_DATE_FORMAT = "%04%02%02 %02:%02:%02"; + static final String ISO_DATE_FORMAT = "%04-%02-%02 %02:%02:%02"; + static final String AMERICAN_DATE_FORMAT = "%02/%02/%04 %02:%02:%02"; + + static final String EUROPEAN_DATE_PATTERN = "^([0123]{0,1}\\d)/([01]{0,1}\\d)/(\\d\\d\\d\\d)$"; + static final String RAW_DATE_PATTERN = "^(\\d\\d\\d\\d)([01]\\d)([0123]\\d)$"; + static final String ISO_DATE_PATTERN = "^(\\d\\d\\d\\d)-([01]\\d)-([0123]\\d)$"; + static final String AMERICAN_DATE_PATTERN = "^([01]{0,1}\\d)/([0123]{0,1}\\d)/(\\d\\d\\d\\d)$"; + + + /** + * + */ + static public String europeanFormat (Calendar time) + { + String result; + + if (time == null) + { + result = ""; + } + else + { + result = String.format (EUROPEAN_DATE_FORMAT, time.get(Calendar.DAY_OF_MONTH), + time.get(Calendar.MONTH) + 1, + time.get(Calendar.YEAR), + time.get(Calendar.HOUR_OF_DAY), + time.get(Calendar.MINUTE), + time.get(Calendar.SECOND)); + } + + // + return (result); + } + + + /** + * + */ + static public String ISOFormat (Calendar time) + { + String result; + + if (time == null) + { + result = ""; + } + else + { + result = String.format (ISO_DATE_FORMAT, time.get(Calendar.DAY_OF_MONTH), + time.get(Calendar.MONTH) + 1, + time.get(Calendar.YEAR), + time.get(Calendar.HOUR_OF_DAY), + time.get(Calendar.MINUTE), + time.get(Calendar.SECOND)); + } + + // + return (result); + } + + + /** + * + */ + static public String americanFormat (Calendar time) + { + String result; + + if (time == null) + { + result = ""; + } + else + { + result = String.format (AMERICAN_DATE_FORMAT, time.get(Calendar.DAY_OF_MONTH), + time.get(Calendar.MONTH) + 1, + time.get(Calendar.YEAR), + time.get(Calendar.HOUR_OF_DAY), + time.get(Calendar.MINUTE), + time.get(Calendar.SECOND)); + } + + // + return (result); + } + + + /** + * + */ + static public String rawFormat (Calendar time) + { + String result; + + if (time == null) + { + result = ""; + } + else + { + result = String.format (RAW_DATE_FORMAT, time.get(Calendar.DAY_OF_MONTH), + time.get(Calendar.MONTH) + 1, + time.get(Calendar.YEAR), + time.get(Calendar.HOUR_OF_DAY), + time.get(Calendar.MINUTE), + time.get(Calendar.SECOND)); + } + + // + return (result); + } +}