Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Java Javadate Time Format Example

Related Tutorials


 
 

Share on Google+Share on Google+

Time Format Example

Advertisement
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

Download code
Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: October 13, 2008

Related Tutorials

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.
DMCA.com