Java Util – Date example with formatter

Java Util – Date example with formatter

Problem Statement:

Java code to print the date and/or time in a locale-sensitive or otherwise-specified format.

java

Solution/ Answer:

Use

java.text.DateFormat.

 

Discussion

To print the date in the correct format for the specified locale use Java Util and Java text package classes. Simply use the default DateFormat formatter, which can be obtained by calling

DateFormat.getInstance( ):

Code:

package stixsjava.date;

import java.text.DateFormat;
import java.util.Date;
import java.util.Locale;
public class STXDateFormat {
  public static void main(String[] args) {
    Date today = new Date();
    DateFormat df = DateFormat.getInstance();
    System.out.println("Before Applying formatting:" + df.format(today));
    DateFormat dateFormat_US = DateFormat.getDateInstance(DateFormat.FULL, Locale.US);
    System.out.println("Before Applying formatting:" +dateFormat_US.format(today));
  }
}

dateformatter

Console Output:

Before Applying formatting:10/15/14 12:24 AM
Before Applying formatting:Wednesday, October 15, 2014

 

Syntax of DateFormat.getDateInstance()

DateFormat java.text.DateFormat.getDateInstance(int style, Locale aLocale)
DateFormat

DateFormat

getInstance

getInstance

Example:

DateFormat dateFormat_US = DateFormat.getDateInstance(
                       DateFormat.FULL, Locale.US);

Five codes—FULL, LONG, MEDIUM, SHORT and DEFAULT—can be passed to decide how verbose a format should be.

——————————————————————————–

You may also like this

——————————————————————————–

About TechnoStixs.com

TechnoStixs

TechnoStixs.com is a dedicated weblog for Java/J2EE and web developers. We take pride in our work. Every publication is carefully analyzed, written and tested to ease the understanding of the topic or subject.

We cover Java Core Technologies, eCommerce, J2EE Frameworks, Web Service, Build Tools, Unit Test Frameworks and Others.

1,466 total views, 2 views today

Add comment