More Tutorials| Bioinformatics| Open Source| Photoshop| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
Date and Time Format Example 
 

This Example shows you date and time format according to the locale. In the code given below we are displaying data and time format according to the locale.

 

Date and Time Format Example

                         

This Example shows you date and time format according to the locale. In the code given below we are displaying data and time format according to the locale.

Methods used in this example are described below :

DateFormat.getDateInstance() : DateFormat class is used for date and time format and getDateInstance() method is used for gets DateFormat class object with the given formatting style for the given locale.

DateFormat.format() : format method is used for format Date into date/time String.

DateFormatExample.java


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

public class DateFormatExample {

    public void displayDate(Locale locale) {

        DateFormat dateFormatter = DateFormat.getDateInstance(DateFormat.DEFAULT,
                                   locale);
        Date today = new Date();
        String dateOut = dateFormatter.format(today);

        System.out.println(dateOut + "   " + locale.toString());
    }

    public void showDate(Locale locale) {

        Date today = new Date();
        String result;
        DateFormat formatter;

        int[] styles = {DateFormat.DEFAULT, DateFormat.SHORT,
            DateFormat.MEDIUM, DateFormat.LONG, DateFormat.FULL
        };

        System.out.println();
        System.out.println("Locale: " + locale.toString());
        System.out.println();

        for (int k = 0; k < styles.length; k++) {
            formatter = DateFormat.getDateInstance(styles[k], locale);
            result = formatter.format(today);
            System.out.println(result);
        }
    }

    public void showTime(Locale locale) {

        Date today = new Date();
        String result;
        DateFormat formatter;

        int[] styles = {DateFormat.DEFAULT, DateFormat.SHORT,
            DateFormat.MEDIUM, DateFormat.LONG, DateFormat.FULL
        };

        System.out.println();

        for (int k = 0; k < styles.length; k++) {
            formatter = DateFormat.getTimeInstance(styles[k], locale);
            result = formatter.format(today);
            System.out.println(result);
        }
    }

    public static void main(String args[]) {

        Locale[] locales = new Locale[]{new Locale("fr""FR")new Locale("en""IN")};

        DateFormatExample[] dateFormat = new DateFormatExample[locales.length];

        for (int i = 0; i < locales.length; i++) {
            dateFormat[inew DateFormatExample();
            dateFormat[i].displayDate(locales[i]);
        }

        for (int i = 0; i < locales.length; i++) {
            dateFormat[i].showDate(locales[i]);
            dateFormat[i].showTime(locales[i]);
        }
    }
}

Output :

10 sept. 2008   fr_FR
10 Sep, 2008   en_IN

Locale: fr_FR

10 sept. 2008
10/09/08
10 sept. 2008
10 septembre 2008
mercredi 10 septembre 2008

11:28:02
11:28
11:28:02
11:28:02 IST
11 28 IST

Locale: en_IN

10 Sep, 2008
10/9/08
10 Sep, 2008
10 September, 2008
Wednesday, 10 September, 2008

11:28:02 AM
11:28 AM
11:28:02 AM
11:28:02 AM IST
11:28:02 AM IST

Download code

                         

» View all related tutorials
Related Tags: c orm formatting form format locale currency this local show for example to exam ws sh e in rm cal

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Training Courses
Tell A Friend
Your Friend Name
Software Solutions
Least Viewed
Most Rated
Recently Viewed
Search Tutorials

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.