[Opensource] SimpleDateFormat

Sarit.Seal at ITCINFOTECH.COM Sarit.Seal at ITCINFOTECH.COM
Wed Mar 12 00:14:21 PST 2003


java.lang.text has a class called SimpleDateFormat you can use that class
to format the date.

I have used this class to do the required formatting.

      public int getDaysThisYear()
      {
            Calendar calCurrent = Calendar.getInstance();
            calCurrent.set(  Calendar.DATE, 01);
            calCurrent.set(  Calendar.MONTH, Calendar.JANUARY);
            calCurrent.add(  Calendar.YEAR, 1);
            calCurrent.add(  Calendar.DATE, -1);
            return calCurrent.get(Calendar.DAY_OF_YEAR);
      }

      public int getDaysThisMonth()
      {
            Calendar calCurrent = Calendar.getInstance();
            calCurrent.set(  Calendar.DATE, 1);
            calCurrent.add(  Calendar.MONTH, 1);
            calCurrent.add(  Calendar.DATE, -1);
            return calCurrent.get(Calendar.DATE);
      }

      public String ConvertDatetoStringinFormat( java.util.Date dtInDate,
String sinFormat )
      {
            SimpleDateFormat sdfFormatter = new SimpleDateFormat(
sinFormat, Locale.US );
            java.util.Date dtcurrDateTime = dtInDate;

            String sDateString = sdfFormatter.format( dtInDate
).toUpperCase();

            return sDateString;
      }

      public String ConvertDatetoString( java.util.Date dtInDate )
      {
            SimpleDateFormat sdfFormatter = new SimpleDateFormat(
LopsConstants.sDateTimeFormatJava, Locale.US );
            java.util.Date dtcurrDateTime = dtInDate;

            String sDateString = sdfFormatter.format(dtInDate).toUpperCase
();

            return sDateString;
      }

      public String convertStringDatetoFormat(  String sInDate,

String sDateTimeFormat,

String sDateTimeFormatNew )
      {
            SimpleDateFormat sdfFormatter = new
SimpleDateFormat(sDateTimeFormat, Locale.US);
            SimpleDateFormat sdfFormatterNew = new SimpleDateFormat(
sDateTimeFormatNew, Locale.US );

            Calendar calCurrent = Calendar.getInstance();
            ParsePosition ppParse = new ParsePosition(0);
            java.util.Date dtDate = sdfFormatter.parse(sInDate, ppParse);
            calCurrent.setTime( dtDate );

            return sdfFormatterNew.format(calCurrent.getTime()).toUpperCase
();
      }

      public String getSystemDate()
      {
            SimpleDateFormat sdfFormatter = new SimpleDateFormat
(LopsConstants.sDateTimeFormatJava, Locale.US);
            java.util.Date dtcurrDateTime = new java.util.Date();
            String sDateString =
sdfFormatter.format(dtcurrDateTime).toUpperCase();

            return sDateString;
      }

      public String getSystemDate( String sDateTimeFormat )
      {
            SimpleDateFormat sdfFormatter = new SimpleDateFormat(
sDateTimeFormat, Locale.US );
            java.util.Date dtcurrDateTime = new java.util.Date();
            String sDateString =
sdfFormatter.format(dtcurrDateTime).toUpperCase();

            return sDateString;
      }

      public String[] getDayNamesShrt()
      {
            String sDayNames[] = new String[7];
            SimpleDateFormat sdfFormatter = new
SimpleDateFormat(LopsConstants.sDayNameShrtJava, Locale.US);
            Calendar calCurrent = Calendar.getInstance();

            // add 1 day as the starting day number of the week is 0
            calCurrent.add(Calendar.DATE,
(calCurrent.get(Calendar.DAY_OF_WEEK) * (-1) )+

LopsConstants.iStartDayOfWeek);

            for( int iDayNo = 0; iDayNo < 7; iDayNo ++)
            {
                  sDayNames[iDayNo] =
sdfFormatter.format(calCurrent.getTime()).toUpperCase();
                  calCurrent.add(Calendar.DATE, 1);
            }

            return sDayNames;
      }

      public String[] getDayNamesLong()
      {
            String sDayNames[] = new String[7];
            SimpleDateFormat sdfFormatter = new
SimpleDateFormat(LopsConstants.sDayNameLongJava, Locale.US);
            Calendar calCurrent = Calendar.getInstance();

            // add 1 day as the starting day number of the week is 0
            calCurrent.add(Calendar.DATE,
(calCurrent.get(Calendar.DAY_OF_WEEK) * (-1) )+

LopsConstants.iStartDayOfWeek);

            for( int iDayNo = 0; iDayNo < 7; iDayNo ++)
            {
                  sDayNames[iDayNo] =
sdfFormatter.format(calCurrent.getTime()).toUpperCase();
                  calCurrent.add(Calendar.DATE, 1);
            }

            return sDayNames;
      }

      // Month Number starts from 0 - January to 11 December
      public String[] getMnthNamesShrt()
      {
            String sMnthNames[] = new String[12];
            SimpleDateFormat sdfFormatter = new
SimpleDateFormat(LopsConstants.sMnthNameShrtJava, Locale.US);
            Calendar calCurrent = Calendar.getInstance();

            // add 1 day as the starting day number of the week is 0
            calCurrent.add(Calendar.MONTH, (calCurrent.get(Calendar.MONTH)
* (-1) ));

            for( int iMnthNo = 0; iMnthNo < 12; iMnthNo ++)
            {
                  sMnthNames[iMnthNo] =
sdfFormatter.format(calCurrent.getTime()).toUpperCase();
                  calCurrent.add(Calendar.MONTH, 1);
            }

            return sMnthNames;
      }

      public String[] getMnthNamesLong()
      {
            String sMnthNames[] = new String[12];
            SimpleDateFormat sdfFormatter = new
SimpleDateFormat(LopsConstants.sMnthNameLongJava, Locale.US);
            Calendar calCurrent = Calendar.getInstance();

            // add 1 day as the starting day number of the week is 0
            calCurrent.add(Calendar.MONTH, (calCurrent.get(Calendar.MONTH)
* (-1) ));

            for( int iMnthNo = 0; iMnthNo < 12; iMnthNo ++)
            {
                  sMnthNames[iMnthNo] =
sdfFormatter.format(calCurrent.getTime()).toUpperCase();
                  calCurrent.add(Calendar.MONTH, 1);
            }
            return sMnthNames;
      }

      public String getFirstDateOfMnth( String sInDate, String
sDateTimeFormat )
      {
            SimpleDateFormat sdfFormatter = new
SimpleDateFormat(sDateTimeFormat, Locale.US);
            Calendar calCurrent = Calendar.getInstance();
            ParsePosition ppParse = new ParsePosition(0);
            java.util.Date dtDate = sdfFormatter.parse(sInDate, ppParse);
            calCurrent.setTime( dtDate );
            calCurrent.set(Calendar.DATE, 1);
            return ConvertDatetoString( calCurrent.getTime() );
      }

      public String getLastDateOfMnth( String sInDate, String
sDateTimeFormat )
      {
            SimpleDateFormat sdfFormatter = new
SimpleDateFormat(sDateTimeFormat, Locale.US);
            Calendar calCurrent = Calendar.getInstance();
            ParsePosition ppParse = new ParsePosition(0);
            java.util.Date dtDate = sdfFormatter.parse(sInDate, ppParse);
            calCurrent.setTime( dtDate );
            calCurrent.set(  Calendar.DATE, 1);
            calCurrent.add(  Calendar.MONTH, 1);
            calCurrent.add(  Calendar.DATE, -1);
            return ConvertDatetoString( calCurrent.getTime() );
      }

      public int getDayNumb( String sInDate, String sDateTimeFormat )
      {
            SimpleDateFormat sdfFormatter = new
SimpleDateFormat(sDateTimeFormat, Locale.US);
            Calendar calCurrent = Calendar.getInstance();
            ParsePosition ppParse = new ParsePosition(0);
            java.util.Date dtDate = sdfFormatter.parse(sInDate, ppParse);
            calCurrent.setTime( dtDate );

            return calCurrent.get(Calendar.DAY_OF_WEEK);
      }

      public int getDayofMonth( String sInDate, String sDateTimeFormat )
      {
            SimpleDateFormat sdfFormatter = new
SimpleDateFormat(sDateTimeFormat, Locale.US);
            Calendar calCurrent = Calendar.getInstance();
            ParsePosition ppParse = new ParsePosition(0);
            java.util.Date dtDate = sdfFormatter.parse(sInDate, ppParse);
            calCurrent.setTime( dtDate );

            return calCurrent.get(Calendar.DAY_OF_MONTH);
      }

      public int getMonth( String sInDate, String sDateTimeFormat )
      {
            SimpleDateFormat sdfFormatter = new
SimpleDateFormat(sDateTimeFormat, Locale.US);
            Calendar calCurrent = Calendar.getInstance();
            ParsePosition ppParse = new ParsePosition(0);
            java.util.Date dtDate = sdfFormatter.parse(sInDate, ppParse);
            calCurrent.setTime( dtDate );

            return calCurrent.get(Calendar.MONTH);
      }

      public String addDay( String sInDate, String sDateTimeFormat, int
iInDays )
      {
            SimpleDateFormat sdfFormatter = new
SimpleDateFormat(sDateTimeFormat, Locale.US);
            Calendar calCurrent = Calendar.getInstance();
            ParsePosition ppParse = new ParsePosition(0);
            java.util.Date dtDate = sdfFormatter.parse(sInDate, ppParse);

            calCurrent.setTime( dtDate );
            calCurrent.add(Calendar.DATE, iInDays);
            return ConvertDatetoString( calCurrent.getTime() );
      }









                                                                                                                                    
                      "Ben Switzer"                                                                                                 
                      <ben at site85.com>            To:       <opensource at jcorporate.com>                                             
                      Sent by:                    cc:                                                                               
                      opensource-admin at jco        Subject:  RE: [Opensource] SimpleDateFormat                                       
                      rporate.com                                                                                                   
                                                                                                                                    
                                                                                                                                    
                      12/03/2003 08:04                                                                                              
                      Please respond to                                                                                             
                      opensource                                                                                                    
                                                                                                                                    
                                                                                                                                    




Hmm... it does.

I'll give it a go.  Thanks.

Ben

-----Original Message-----
From: opensource-admin at jcorporate.com
[mailto:opensource-admin at jcorporate.com] On Behalf Of Michael Rimov
Sent: Tuesday, March 11, 2003 6:20 PM
To: opensource at jcorporate.com
Subject: Re: [Opensource] SimpleDateFormat


At 02:37 PM 3/11/2003 -0500, you wrote:
>Good day all.
>
>I've been contemplating how best to format dates on an application wide

>basis.  What I've done is as follows:

Ben,

Instead, why don't you create a wrapper object that wraps a DBObject to
get
what you want.  By implementing a quick 'factory' method, you could end
up
with code like so:

WrapperObject wrap = WrapperObject.wrap(DBObject obj);
String formattedDate = wrap.getDateField(String fieldName);

Something along these lines would give you two advantages:

1 - Proper division of labor.  The wrapper does what it's supposed to...

which is provide date formatting.

2 - We avoid yet more methods in DBObject, which I'm trying to split up
anyway.

3 - No patching the code while you wait for it to show up in CVS :)

Does this help?
                                                         -Mike


_______________________________________________
Opensource mailing list
Opensource at jcorporate.com
http://mail.jcorporate.com/mailman/listinfo/opensource
Archives: http://mail.jcorporate.com/pipermail/opensource/

_______________________________________________
Opensource mailing list
Opensource at jcorporate.com
http://mail.jcorporate.com/mailman/listinfo/opensource
Archives: http://mail.jcorporate.com/pipermail/opensource/


Disclaimer: This  communication  is  for the exclusive use of the intended
recipient(s) and  shall  not  attach
any liability on the originator or ITC Infotech India Ltd./its  Holding
company/ its Subsidiaries/ its Group
Companies. If you are the addressee, the contents of this e-mail are
intended for your use only and it
shall  not be forwarded to any third party, without first obtaining written
authorisation  from the originator
or ITC Infotech India Ltd./ its Holding company/its  Subsidiaries/ its
Group Companies. It may contain
information which is confidential and legally privileged and the same shall
not be used or dealt with  by
any  third  party  in  any manner whatsoever without the specific consent
of  ITC  Infotech India  Ltd./its
Holding company/ its Subsidiaries/ its Group Companies.







More information about the Opensource mailing list