Presentation is loading. Please wait.

Presentation is loading. Please wait.

Processing Date and Time Date class Date currentTime = new Date(); DateFormat class The DateFormat class can be used to format date and time in a number.

Similar presentations


Presentation on theme: "Processing Date and Time Date class Date currentTime = new Date(); DateFormat class The DateFormat class can be used to format date and time in a number."— Presentation transcript:

1 Processing Date and Time Date class Date currentTime = new Date(); DateFormat class The DateFormat class can be used to format date and time in a number of styles To format date and time, simply create an instance of DateFormat using one of the three static methods getdateInstance, getTimeInstance, getDateTimeInstance And apply the format(Date) method on the instance

2 Example of Date class package datetime; import javax.swing.*; import java.awt.*; import java.util.*; class clock extends Jframe { private JLabel cl; private Date time; public clock() { String str; time = new Date(); str = time.toString() ; cl = new JLabel(str); Container com = getContentPane(); com.add(cl); } public class datetime { public static void main(String[] args) { clock currentTime = new clock(); currentTime.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE ); currentTime.setSize(100,50); currentTime.setVisible(true); }

3 DateFormat class String format( Date date)- format a Date into a String DateFormat getDateInstance – gets the date formatter with the default formatting style DateFormat getDateInstance(int style) – gets the date formatter with the given formatting style DateFormat getdateTimeInstance(int Dstyle, int Tstyl) – gets the date and time formatter with the given date and time formatting styles DateFormat getInstance() – get a default date and time formatter that uses the SHORT style for both the date and time

4 Date Style and time Style The date style and time style are one of the following constants: DateFormat.SHORT – numeric 7/24/98 and 4:49pm DateFormat.MEDIUM – longer 24-Jul-98 and 4:52:09 PM DateFormat.LONG – even longer July,24 1998 and 4:53:16 PM EST DateFormat.FULL – completely specified Friday, July 24, 1998 and 4:53:13 o’clock PM EST

5 Example – the string in label DateFormat form = DateFormat.getDateTimeInstance( DateFormat.SHORT, DateFormat.SHORT); Str = form.format(time);

6 TimeZone class Represent a time zone offset and figure out daylight savings The international time zone ID is a string in the form of country/city like Europe/Berlin, Asia/Taipei, and America/Washington Static method getDefault() to contain the default time zone on the host machine

7 SimpleDateFormat class Enables you to choose any user-defined pattern for date and time formatting. public SimpleDateDormat(String pattern) SimpleDateFormat from = new SimpleDateFormat(“yyyy.MM.dd G ‘at’ hh:mm:ss z”); Date current = new Date(); String dateString = form.format(current); System.out.println(“Current time is” + dateString);

8 Processing Date and Time Timer class java.swing.Timer Timer(int delay, ActionListener) – Create a Timer with a specified delay and an Actionlistener addActionListener( ActionListener listen) – add an ActionListener to the timer void start() – starts this timer Void stop() – stop this timer Void setDelay( int delay) – sets a new delay value for this timer

9 Add a timer Add a listener to clock Class clock extends Jframe implements ActionListener { … public void actionPerformed(ActionEvent e) { Date t = new Date(); String str = t.toString(); //or the same format lbclock.setText(str); } Create the Timer object and start the timer in main Stattic void main(String[] arg) { clock time = new clock(); Timer tick = new Timer(1000, time); tick.start(); … }


Download ppt "Processing Date and Time Date class Date currentTime = new Date(); DateFormat class The DateFormat class can be used to format date and time in a number."

Similar presentations


Ads by Google