Parsing Date Example

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

Parsing Date Example

     

This example shows how to parsing date 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 is used for format Date class object into date / time and appends the it into the StringBuffer.

ParsingDate.java

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

public class ParsingDate {

  public static void main(String args[]) throws Exception {

  String s;
  DateFormat formatter;
  Date date;

  formatter = new SimpleDateFormat("MM/dd/yy");

  // parse date according to the given format

  date = (Dateformatter.parse("1/29/08");
   //
01/29/08
  s = formatter.format(date);
 
  System.out.println(s);

  formatter = new SimpleDateFormat("DD/MM/yy");
  date = (Dateformatter.parse("1/1/08");
  //
01/01/08
  s = formatter.format(date);
  System.out.println(s);

  formatter = new SimpleDateFormat("dd-MMM-yy");
  date = (Dateformatter.parse("29-Jan-08");
//
29-Jan-08
  s = formatter.format(date);
  System.out.println(s);
  }
}

Output:
01/29/08
01/01/08
29-Jan-08

Download code

Advertisements

Ads

 

We have 1000s of tutorials on our website. Search Tutorials tutorials on our website.

Share on Google+Share on Google+

Parsing Date Example

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

Advertisements

 

Discuss: Parsing Date Example  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0

Ads

 

Ads