Home Java Example Java Util Java Date Format Example

Related Tutorials


 
 

Share on Google+Share on Google+

Java Date Format Example

Advertisement
In this tutorial you will learn that how to format the date into the different local

Java Date Format Example

You can format date in many ways. In this tutorial Format class is used to format the java date in to the different formats. This class is of java.text.Format package in java. You can use the Format class in the following ways.

1. Create an object of Format class.
2. Set The required date format as
3. Now Create the Date class object.
4. Get The Formatted date String of you date as
Format dateFormat;
dateFormat = new SimpleDateFormat("E dd MMM yyyy HH mm ss Z");
Date date = new Date();
String newDateString = dateFormat.format(date);

You can format the date into specific locale. For example following date is set in the French Languge

dateFormat = new SimpleDateFormat("E dd MMM yy HH mm ss Z", Locale.FRENCH);

A Simple Example is given below

SampleInterfaceImp.java

import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

public class DateFormaterClass {

	public static void main(String[] args) {
		Date date = new Date();
		Format dateFormat;
		System.out.println("\n**************Simple Date Formate**************");
		System.out.println(date);
		System.out.println("\n**************Formated Date**************");
		dateFormat = new SimpleDateFormat("E dd MMM yyyy HH mm ss Z");
		String newDateString = dateFormat.format(date);
		System.out.println(newDateString);

		System.out.println("\n**************In French Style**************");
		dateFormat = new SimpleDateFormat("E dd MMM yy HH mm ss Z",
				Locale.FRENCH);
		String franchDateString = dateFormat.format(date);
		System.out.println(franchDateString);

		System.out.println("\n**************In German Style**************");
		dateFormat = new SimpleDateFormat("E dd MMM yy HH mm ss Z",
				Locale.GERMAN);
		String chineseString = dateFormat.format(date);
		System.out.println(chineseString);

		System.out.println("\n**************In German Style**************");
		dateFormat = new SimpleDateFormat("E dd MMM yy HH mm ss Z",
				Locale.ITALIAN);
		String italianDateString = dateFormat.format(date);
		System.out.println(italianDateString);

	}
}


When you run this application it will display message as shown below:



**************Simple Date Formate**************
Sat Jul 02 17:45:30 GMT+05:30 2011

**************Formated Date**************
Sat 02 Jul 2011 17 45 30 +0530

**************In French Style**************
sam. 02 juil. 11 17 45 30 +0530

**************In German Style**************
Sa 02 Jul 11 17 45 30 +0530

**************In German Style**************
sab 02 lug 11 17 45 30 +0530

Download Select Source Code

Advertisement

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: July 23, 2011

Related Tutorials

Discuss: Java Date Format Example  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:1
Mohammed
December 8, 2011
Format of time

How can I format current date in this format 2012-12-07T15:20:32+04:00? I have tried SimpleDateFormat formatter2 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ"), but I get 2012-12-07T15:20:32+0400. Note, there is no column in +0400. Please help.
DMCA.com