好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

Time Format Example

Time Format Example

This example shows how to format time using Format class. In this program we use a pattern of special characters to time format.

Time Format Example

      

This example shows how to format time using Format class. In this program we use a pattern of special characters to time format.

Description of the code :

SimpleDateFormat() : SimpleDateFormat class use for formatting and parsing dates. It allows for formatting date into text , parsing text into date and normalization.

format() : This method used for format Date class object into date / time and appends the it into the StringBuffer.

TimeFormat.java


import  java.text.*;
import  java.util.*;

public class  TimeFormat  {

   public static  void  main ( String args []) {

   String s;
   Format formatter;
   Date date =  new  Date () ;

   // Time formate 01:12:53 AM
   formatter =  new  SimpleDateFormat ( "hh:mm:ss a" ) ;
   s = formatter.format ( date ) ;
   System.out.println ( "hh:mm:ss a formate : "  + s ) ;

   //Time formate 14.36.33
   formatter =  new  SimpleDateFormat ( "HH.mm.ss" ) ;
   s = formatter.format ( date ) ;
   System.out.println ( "HH.mm.ss : formate : "  + s ) ;
   }
}



Output

hh:mm:ss a formate :  04 : 31 : 16  PM
HH.mm.ss : formate :  16.31.16

查看更多关于Time Format Example的详细内容...

  阅读:35次