Online Training Spring Hibernate Training in Delhi R Programming Training Course Big Data and Hadoop Training Java Training

Java Date Format Example

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

Ads

Tutorials   
How to read properties file in Java? Generate random numbers in Java Cloneable Interface in Java What is the flaw with the Stack class? List In Java Java Stack Example HashSet In Java Enumeration in java How to generate random number in java Iterator in java Queue in java Transient Java Keyword clone method in Java Java Queue example Logger in Java Java current date - How to get current date in Java Collection : LinkedList Example Collection : ArrayList Example Random alpha numeric string of fixed size Java List Iterator Example The Hashtable Class The BitSet class Java Vector Java Enumeration Data Structures in Java Java Transient Variable Java Date Format Example Java Properties File Example Map.Entry Java Interface Java Property File java preferences,Change the value of Preferences by user Retrieving a Preference Node Java hasNext Write Keys and Values to the Properties file in Java Get All Keys and Values of the Properties files in Java Properties file in Java Properties file in Java Determining If a Preference Node Contains a Specific Value Change the value of Preferences by user Clone method example in Java Implementing a Simple Event Notifier Determining When a Preference Node Is Added or Removed Listening for Changes to Preference Values in a Preference Node Importing Preferences Finding a Preference in a Preference Tree Retrieving the Parent and Child Nodes of a Preference Node Determining If a Preference Node Exists Removing a Preference Node Creating a Preference Node Getting the Maximum Size of a Preference Key and Value
Ads

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

Advertisements

Share on Google+Share on Google+

Java Date Format Example

Posted on: July 23, 2011 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Advertisements

Ads
 

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.

Ads

 

Ads