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.


 
Ads

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

Download code

Advertisements

Share on Google+Share on Google+

Time Format Example

Posted on: October 13, 2008 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Advertisements

Ads
 

Discuss: Time Format Example   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:1
Drew
August 26, 2012
Thank you!

Thank you! This was a very helpful tutorial.

Ads

 

Ads