Results 1 to 2 of 2
Thread: Simple Date Format
- 09-29-2008, 04:01 AM #1
Member
- Join Date
- Sep 2008
- Posts
- 5
- Rep Power
- 0
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:
The following snippets of 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
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.Java Code:formatter.applyPattern("h:mm:ss"); lblShort.setText(formatter.format(currentTime));
- 09-29-2008, 04:46 AM #2
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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
-
[SOLVED] Update jTable with new data (especially Date value Format)
By gustio in forum New To JavaReplies: 2Last Post: 08-12-2008, 12:26 PM -
How to format the date in particular pattern
By Java Tip in forum java.textReplies: 0Last Post: 04-04-2008, 02:35 PM -
How to format the date in particular pattern
By JavaBean in forum Java TipReplies: 0Last Post: 10-04-2007, 09:28 PM -
Date format display in CSV file after exporting
By latha in forum Advanced JavaReplies: 0Last Post: 08-03-2007, 08:09 AM -
problems with Date format
By tommy in forum New To JavaReplies: 1Last Post: 07-25-2007, 08:38 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks