First, thanks for sharing this. I'm sure many people will find methods of
your class useful.

Second, from a design point of view I have a personal hatred of "utility"
classes like this which simple serve as a collection of public static
methods. There are very few occasions where this is appropriate, but mostly
it is an excuse to lapse into old procedural-style coding habits.

One tends to end up with a class which doesn't "belong" very well anywhere
and provides functionality which should really be on the class it is intended
to supplement.

It is usually much better to use a Wrapper around the class whose
functionality you want to extend, which simply passes through existing
methods and handles the extended functionality.

Rich

On Wednesday 15 May 2002 11:08 am, you wrote:
> Hi guys,
>  Hope this helps to solve many problems using dates. Expect your esteemed
> suggestions and comments for my improvement.
>  i could not send more than 100 lines. so i am sending the file as 2 parts.
>
> import java.util.Date;
> import java.util.Calendar;
> import java.text.SimpleDateFormat;
> public class DateService {
> public final static SimpleDateFormat dateFormatter = new
> SimpleDateFormat("yyyy-M-d");
> public final static SimpleDateFormat timeFormatter = new
> SimpleDateFormat("HH:mm");
> public final static SimpleDateFormat dateTimeFormatter = new
> SimpleDateFormat("yyyy-M-d HH:mm");
> public static Date addDays(Date date, int days){
> Calendar cal = Calendar.getInstance();cal.setTime(date);cal.add(cal.DATE,
> days);return cal.getTime();
> }
> public static Date addMonths(Date date, int months){
> Calendar cal = Calendar.getInstance();cal.setTime(date);
> cal.add(cal.MONTH, months);
>         return cal.getTime();
> }
> public static Date addYears(Date date, int years){
> Calendar cal = Calendar.getInstance();cal.setTime(date);cal.add(cal.YEAR,
> years);
>         return cal.getTime();
> }
> public static Date convertFromSqlDate(java.sql.Date date){
>         return new Date(date.getTime());
> }
> public static java.sql.Date convertToSqlDate(Date date){
>         if (date == null) return null;
>         return new java.sql.Date(date.getTime());
> }
> public static String format(Date date, String simpleFormat) throws
> InvalidDateException{
>         try{
>                 return new SimpleDateFormat(simpleFormat).format(date);
>         } catch (Exception e){  }
> }
> public final static Date getCurrentTime() {
>     return Calendar.getInstance().getTime();
> }
> public static Date getDate(int yy, int mm, int dd) throws
> InvalidDateException{
>         if (yy < 100)
>                 yy += 1900;
>         try {
>                 Calendar cal =
> Calendar.getInstance();cal.clear();cal.set(Calendar.DATE, dd);
>                 cal.set(Calendar.MONTH, mm - 1);cal.set(Calendar.YEAR, yy);
> return cal.getTime();
>         }catch (Exception e){
>                 StringBuffer msg = new StringBuffer(10);
>
> msg.append(yy);msg.append('/');msg.append(mm);msg.append('/');msg.append(dd
>) ;
>         }
> }
> public static Date getDate(String date) throws InvalidDateException {
>         try {
>         return dateFormatter.parse(date);
>     } catch (java.text.ParseException pe) {
>             throw new InvalidDateException(pe.getMessage(),pe);
>     }
> }
> public static Date getDate(String yy, String mm, String dd) throws
> InvalidDateException{
>         try {
>                 int y = Integer.parseInt(yy);   int m =
> Integer.parseInt(mm);
>                 int d = Integer.parseInt(dd);   return getDate(y,m,d);
>         }
>         catch (NumberFormatException e) {
>                 StringBuffer msg = new StringBuffer(10);
>
> msg.append(yy);msg.append('/');msg.append(mm);msg.append('/');msg.append(dd
>) ;
>         }
> }
> public static int getDay(Date date) throws InvalidDateException{
>         try {
>                 Calendar cal = Calendar.getInstance();cal.clear();
>                 cal.setTime(date);return cal.get(Calendar.DATE);
>         } catch (Exception e) {}
> }
> public static int getHour(Date date) throws InvalidDateException{
>         try {
>                 Calendar cal = Calendar.getInstance();  cal.clear();
>                 cal.setTime(date);return cal.get(Calendar.HOUR_OF_DAY);
>         } catch (Exception e) { }
> }
> public static int getMinutes(Date date) throws InvalidDateException{
>         try {
>                 Calendar cal = Calendar.getInstance();cal.clear();
>                 cal.setTime(date);return cal.get(Calendar.MINUTE);
>         } catch (Exception e) { }
> }
> public static int getMonth(Date date) throws InvalidDateException {
>         try {
>                 Calendar cal = Calendar.getInstance();cal.clear();
>                 cal.setTime(date);return cal.get(Calendar.MONTH) + 1;
>         } catch (Exception e){}
> }
>
> Regards
>
> Ramesh Kesavanarayanan
> [EMAIL PROTECTED]
>
> ===========================================================================
> To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
> of the message "signoff EJB-INTEREST".  For general help, send email to
> [EMAIL PROTECTED] and include in the body of the message "help".

==============================================================================
This email and any files transmitted with it are confidential and intended solely for 
the use of the individual or entity to whom they are addressed. All information is the 
view of the individual and not necessarily the company. If you are not the intended 
recipient you are hereby notified that any dissemination, distribution, or copying of 
this communication and its attachments is strictly prohibited. If you have received 
this email in error please notify:
[EMAIL PROTECTED]


==============================================================================

===========================================================================
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff EJB-INTEREST".  For general help, send email to
[EMAIL PROTECTED] and include in the body of the message "help".

Reply via email to