Java Date Format Example

Ads
 

Java Date Format Example

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

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

Ads

Ads