Improve Calendar helpers.

This commit is contained in:
Christian P. MOMON 2010-08-15 16:04:13 +02:00
parent 4dc4db245f
commit fd6f407159
2 changed files with 143 additions and 7 deletions

17
src/fr/devinsy/util/DateHelper.java Executable file → Normal file
View file

@ -13,13 +13,18 @@ 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)$";
@ -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));
}
//

View file

@ -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);
}
}