Results 1 to 2 of 2
  1. #1
    arimakidd is offline Member
    Join Date
    Sep 2008
    Posts
    5
    Rep Power
    0

    Question Simple Date Format

    I need some help understanding the SimpleDateFormat class. I really don't get it when it comes to this class. Have a look at this code:
    Java Code:
    import java.awt.*;
    import java.applet.*;
    import java.util.*;
    import java.text.*;
    
    public class Clock4 extends Applet implements Runnable{
    
      Label lblShort = new Label();
      Label lblMedium = new Label();
      Label lblLong = new Label();
      Label lblHuge = new Label();
    
      Thread looper;
      SimpleDateFormat formatter = new SimpleDateFormat("hh:mm:ss");
    
      public void init(){
    
        setFont (new Font ("SansSerif", Font.PLAIN, 14));
        setLayout(new GridLayout(0,2));
        add (new Label("h:mm:ss"));
        add (lblShort);
        add (new Label("EEE, MMM d, ''yy"));
        add (lblMedium);
        add (new Label("hh 'o''clock' a, zzzz"));
        add (lblLong);
        add (new Label("yyyyy.MMMMM.dd GGG hh:mm aaa"));
        add (lblHuge);
    
      } // end init
    
      public void start(){
        if (looper == null){
          looper = new Thread(this);
          looper.start();
        } // end if
      } // end start
    
      public void stop(){
        looper = null;
      } // end stop
    
      public void run(){
        while (true){
          Date currentTime = new Date();
    
          formatter.applyPattern("h:mm:ss");
          lblShort.setText(formatter.format(currentTime));
    
          formatter.applyPattern("EEE, MMM d, ''yy");
          lblMedium.setText(formatter.format(currentTime));
    
          formatter.applyPattern("hh 'o''clock' a, zzzz");
          lblLong.setText(formatter.format(currentTime));
    
          formatter.applyPattern("yyyyy.MMMMM.dd GGG hh:mm aaa");
          lblHuge.setText(formatter.format(currentTime));
    
        } // end while
      } // end run
    
    } // end class def
    The following snippets of code:
    Java Code:
    formatter.applyPattern("h:mm:ss");
    lblShort.setText(formatter.format(currentTime));
    I don't understand how the applyPattern method carries over to the other line. Its the other line that outputs the new format to the label. How is that working? Is the applyPattern("h:mm:ss") stored in the formatter reference and so then applied to the date object "currentTime"? I don't understand how a pattern is defined when the SimpleDateFormat reference was instantiated and then another pattern can be applied using the applyPattern method using the object "formatter"? Can someone explain how this works or point me to a site that does. The API does not help at all.

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    In simple words, SimpleDateFormat class have overloaded method name applyPattern(). It convert the string pattern in to date format. Best thing is look at the SimpleDateFormat class. Are you working on any IDE? If so you can simply look at those overloaded methods and how they translate in to date format.

Similar Threads

  1. Replies: 2
    Last Post: 08-12-2008, 12:26 PM
  2. How to format the date in particular pattern
    By Java Tip in forum java.text
    Replies: 0
    Last Post: 04-04-2008, 02:35 PM
  3. How to format the date in particular pattern
    By JavaBean in forum Java Tip
    Replies: 0
    Last Post: 10-04-2007, 09:28 PM
  4. Date format display in CSV file after exporting
    By latha in forum Advanced Java
    Replies: 0
    Last Post: 08-03-2007, 08:09 AM
  5. problems with Date format
    By tommy in forum New To Java
    Replies: 1
    Last Post: 07-25-2007, 08:38 PM

Posting Permissions

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