DateFormat class

 

  Numbering lines in a file.  The sample program below numbers the lines and uses DateFormat to print out numbered lines from any file. 

The pattern creates leading zeroes so the output lines appear as follows:

 

0001: line one

0002: line two

0003: line three

0004: line four etc.

 

 import java.io.*;

 import java.text.*;

 class PrintNumberedLines {

   public static void main(String args[ ]) {

     try {

         File fi = new File( "myinputfile" );

         BufferedReader br = new BufferedReader( new FileReader( fi ));

         PrintWriter pw = new PrintWriter( System.out, true);

         String s;

         double d = 1;

         DecimalFormat pattern = new DecimalFormat( "0000" );

         while ( ( s = br.readLine( )) != null ) {

             pw.println( pattern.format( d++ ) + ": " + s );

          }

      br.close( );

      pw.close( );

    } catch (IOException e) {  }

  }

 }

 

 

  The sample class below shows various ways to format dates and times. Run it and see.

 

import java.util.*;

import java.text.*;

 

public class DateAndTimeExamples

{

                // ( a ) Get a Date

                Date d = new Date();

                String s;

 

                // ( b ) Get a DateFormat

                DateFormat df;

 

public static void main(String[ ] args)

{ DateAndTimeExamples dt = new DateAndTimeExamples( ); }

 

public DateAndTimeExamples( )

{

 

                // HOW TO USE THE PATTERNS

 

                // ( c ) Get a SimpleDateFormat

                SimpleDateFormat sdf;

 

                // ( d ) Set it to a pattern

                sdf = new SimpleDateFormat("MM.dd.yy");

 

                // ( e ) Use it to format your date

                s = sdf.format(d);

 

                // WATCH the various numbered SYSPRINTs here. This is the first.

System.out.println("(1)\t " +d + "\n");

 

                // ( f ) Set it to a formatter.  DATES use getDateInstance

                df = DateFormat.getDateInstance(DateFormat.DEFAULT);

 

                // ( g ) Use it to format your date into a String

                s = df.format(d);

                System.out.println("(2)\t " +s + "\n");

 

                df = DateFormat.getDateInstance(DateFormat.SHORT);

                s = df.format(d);

                System.out.println("(3)\t " +s + "\n");

 

                df = DateFormat.getDateInstance(DateFormat.LONG);

                s = df.format(d);

                System.out.println("(4)\t " +s + "\n");

 

                df = DateFormat.getDateInstance(DateFormat.FULL);

                s = df.format(d);

                System.out.println("(5)\t " +s + "\n");

 

                df = DateFormat.getTimeInstance(DateFormat.SHORT);

                s = df.format(d);

                System.out.println("(6)\t " +s + "\n");

 

                df = DateFormat.getTimeInstance(DateFormat.LONG);

                s = df.format(d);

                System.out.println("(7)\t " +s + "\n");

 

                // DATE AND TIME use getDateTimeInstance(..)

                df = DateFormat.getDateTimeInstance(DateFormat.DEFAULT,DateFormat.DEFAULT);

                s = df.format(d);

                System.out.println("(8)\t " +s + "\n");

 

                df = DateFormat.getDateTimeInstance(DateFormat.SHORT,DateFormat.SHORT);

                s = df.format(d);

                System.out.println("(9)\t " +s + "\n");

 

                df = DateFormat.getDateTimeInstance(DateFormat.LONG,DateFormat.LONG);

                s = df.format(d);

                System.out.println("(10)\t " +s + "\n");

 

                df = DateFormat.getDateTimeInstance(DateFormat.FULL,DateFormat.FULL);

                s = df.format(d);

                System.out.println("(11)\t " +s + "\n");

 

                s = sdf.format(d);

                System.out.println("(12)\t " +s + "\n");

 

                sdf = new SimpleDateFormat("'Minute' mm 'of Hour' kk 'of Day' D 'of Month' MM 'of Year' yyyy");

                s = sdf.format(d);

                System.out.println("(13)\t " +s + "\n");

 

                sdf = new SimpleDateFormat("EEEE, MMMM dd, G yyyy");

                s = sdf.format(d);

                System.out.println("(14)\t " +s + "\n");

 

                sdf = new SimpleDateFormat("a 'on a' EEEE 'in' MMMM");

                s = sdf.format(d);

                System.out.println("(15)\t " +s + "\n");

 

                sdf = new SimpleDateFormat("MM.dd.yy k:mm:ss:SSS ");

                s = sdf.format(d);

                System.out.println("(16)\t " +s + "\n");

}

}//end class