Results 1 to 3 of 3
  1. #1
    vhannierose is offline Member
    Join Date
    Nov 2007
    Location
    taguig city, philippines
    Posts
    4
    Rep Power
    0

    Post Hi! Pls help me with this

    Hi Everybody!

    I'm just a newbie here at Java forums as well as I'm just also learning Java right now.

    Do you know resources online or books as my reference while I'm just learning java.

    Also, Can you help me with this?? Thank you very much.. Any suggestions??

    Java Code:
    package com.jds.architecture.utilities;
    
    import java.text.DateFormat;
    import java.text.SimpleDateFormat;
    import java.util.Calendar;
    import java.util.Date;
    
    
    /**
     * Transform strategy object used to transform a <code>Calendar</code> instance
     * to a <code>String</code>.  This class uses the <code>java.text.DateFormat</code>
     * to provide the formatting.
     * 
     * The overriden method <code>transform(Object target)</code> accepts a <code>java.util.Calendar</code>
     * or <code>java.util.Date</code>object representing the date to be transformed.  RuntimeExceptions will be thrown when any other 
     * argument is passed to the method.
     * 
     * Classes that implement the <code>TransformStrategy</code> interface should be 
     * passed to <code>Transformer</code> objects via their constructor or to the 
     * <code>Transformer.transform(TransformStrategy, Object)</code> method.
     * 
     * @see Transformer
     * @see TransformStrategy
     */
    public class CalendarToString implements TransformStrategy{
    	
    	private DateFormat df;
    	
    	/**
    	 * Constructs a new strategy object using the specified format for its transform rule.
    	 * 
    	 * @param format format to be used by this transfrom strategy
    	 * @see java.text.DateFormat
    	 */
    	public CalendarToString(int format) {
    		
    		df = DateFormat.getDateInstance(format);
    		
    	}
    	
    	/**
    	 * Constructs a new strategy object using the default format DateFormat.FULL for its 
    	 * transform rule.
    	 */
    	public CalendarToString(){
    	
    		//df = DateFormat.getDateInstance(DateFormat.FULL);
    		df = new SimpleDateFormat("dd-MMM-yy");
    		
    	}
    	
    	/** 
    	 * Returns the specified argument <code>Calendar<code> instance as a string
    	 * 
    	 * @param target <code>Calendar</code> instance to transform
    	 * @return formatted string representing the <code>Calendar</code> instance
    	 * 
    	 */
    	public Object transform(Object target) {
    			
    	    
            return null;
    	}
    
    }
    Last edited by JavaBean; 11-06-2007 at 08:20 AM. Reason: Code placed inside [code] tag.

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    Java Code:
    import java.text.DateFormat;
    import java.text.SimpleDateFormat;
    import java.util.Calendar;
    import java.util.Date;
    
    public class CalendarToStringTest {
        public static void main(String[] args) {
            Calendar calendar = Calendar.getInstance();
            Date now = calendar.getTime();
            System.out.println("now = " + now);
            CalendarToString ctsDefault = new CalendarToString();
            System.out.println("ctsDefault(now)      = " + ctsDefault.transform(now));
            System.out.println("ctsDefault(calendar) = " +
                                ctsDefault.transform(calendar));
            CalendarToString ctsMedium = new CalendarToString(DateFormat.MEDIUM);
            System.out.println("ctsMedium(now)       = " + ctsMedium.transform(now));
            System.out.println("ctsMedium(calendar)  = " +
                                ctsMedium.transform(calendar));
            // Test IllegalArgumentException.
            new CalendarToString().transform("hello world");
        }
    }
    
        /**
         * Transform strategy object used to transform a <code>Calendar</code> instance
         * to a <code>String</code>.  This class uses the <code>java.text.DateFormat</code>
         * to provide the formatting.
         * 
         * The overriden method <code>transform(Object target)</code> accepts a <code>java.util.Calendar</code>
         * or <code>java.util.Date</code>object representing the date to be transformed.
         * RuntimeExceptions will be thrown when any other argument is passed to the method.
         * 
         * Classes that implement the <code>TransformStrategy</code> interface should be 
         * passed to <code>Transformer</code> objects via their constructor or to the 
         * <code>Transformer.transform(TransformStrategy, Object)</code> method.
         * 
         * @see Transformer
         * @see TransformStrategy
         */
    class CalendarToString implements TransformStrategy {
        private DateFormat df;
    
        /**
         * Constructs a new strategy object using the specified format for its
         * transform rule.
         * 
         * @param format format to be used by this transfrom strategy
         * @see java.text.DateFormat
         */
        public CalendarToString(int format) {
            df = DateFormat.getDateInstance(format);
        }
    
        /**
         * Constructs a new strategy object using the default format DateFormat.FULL for its 
         * transform rule.
         */
        public CalendarToString() {
            df = DateFormat.getDateInstance(DateFormat.FULL);
            //df = new SimpleDateFormat("dd-MMM-yy");
        }
    
        /** 
         * Returns the specified argument <code>Calendar<code> instance as a string
         * 
         * @param target <code>Calendar</code> instance to transform
         * @return formatted string representing the <code>Calendar</code> instance
         * 
         */
        public Object transform(Object target) {
            String parsed = null;
            if(target instanceof Date)
                parsed = df.format((Date)target);
            else if(target instanceof Calendar)
                parsed = df.format(((Calendar)target).getTime());
            else
                throw new IllegalArgumentException("CalendarToString.transform for: " +
                                                    target.getClass().getName());
    
            return parsed;
        }
    }
    
    interface TransformStrategy {
        public Object transform(Object o);
    }

  3. #3
    assamhammad is offline Member
    Join Date
    Nov 2007
    Posts
    7
    Rep Power
    0

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •