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.
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);
}
}
**************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 |
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.
Ask Questions? Discuss: Java Date Format Example
Post your Comment