• Skip to main content
  • Skip to primary sidebar
  • Skip to secondary sidebar
  • Skip to footer

Computer Notes

Library
    • Computer Fundamental
    • Computer Memory
    • DBMS Tutorial
    • Operating System
    • Computer Networking
    • C Programming
    • C++ Programming
    • Java Programming
    • C# Programming
    • SQL Tutorial
    • Management Tutorial
    • Computer Graphics
    • Compiler Design
    • Style Sheet
    • JavaScript Tutorial
    • Html Tutorial
    • Wordpress Tutorial
    • Python Tutorial
    • PHP Tutorial
    • JSP Tutorial
    • AngularJS Tutorial
    • Data Structures
    • E Commerce Tutorial
    • Visual Basic
    • Structs2 Tutorial
    • Digital Electronics
    • Internet Terms
    • Servlet Tutorial
    • Software Engineering
    • Interviews Questions
    • Basic Terms
    • Troubleshooting
Menu

Header Right

Home » Java » Structures » Date Format Example in Java
Next →
← Prev

Date Format Example in Java

By Dinesh Thakur

We can change the format of the date in two steps: First, we create a formatter with the getDateInstance method. Then, we invoke the format method, which returns a String containing the formatted date.

Example:

Date t;
String dt;
DateFormat df;
df = DateFormat.getDateInstance(DateFormat.DEFAULT, currentLocale);
t = new Date();
dt = df.format(t);
System.out.println(dt + " " + currentLocale.toString());

The format of the dates vary with Locale. Since DateFormat is locale sensitive, it takes care of the formatting details for each Locale.

15 Avr 94        fr_FR

15.4.1994        de_DE

15-Apr-94        en_US

The DEFAULT style is one of the predefined formatting styles, The DateFormat class provides following styles :

• DEFAULT

• SHORT

• MEDIUM

• LONG

• FULL

The following table shows how dates are formatted for each style with the U.S. and French locales:

Sample Date Formats

Style                            U.S. Locale                             French Locale

DEFAULT                   15-Apr-94                                15 avr 94

SHORT                        04/15/94                                 15/04/94

MEDIUM                     15-Apr-94                                15avr 94

LONG                          April 15, 1994                         15 avril 1994

FULL                           Friday, April 15, 1994             vendredi, 15 avril 1994

Times

We can even format times with the getTimelnstance method:

DateFormat tf = DateFormat.getTimeInstance(DateFormat. DEFAULT, currentLocale);

The table that follows shows the various predefined format styles for the U.S. and German locales:

Sample Time Formats

Style                U.S. Locale                 German Locale

DEFAULT        3:58:45PM                  15:58:45

SHORT            3:58PM                       15:58

MEDIUM          3:58:45PM                  15:58:45

LONG               3:58:45PM PDT          15:58:45GMT+02:00

FULL               3:58:45oclock PM PDT 15.58Uhr GMT+02:00

Both Dates and Times

To display a date and time in the same String, we create the formatter with the getDateTimeInstance method. The first parameter is the date style, and the second is the time style. The third parameter is the Locale.

DateFormat fm = DateFormat.getDateTimeInstance(DateFormat.LONG,

DateFormat.LONG, currentLocale);

The following table shows the date and time formatting styles for the U.S. and French locales:

Sample Date and Time Formats

Style                U.S. Locale                                 French Locale

DEFAULT       25-Jun-981:32:19 PM                  25 jun 98 22:32:20

SHORT            6/25/981:32 PM                           25/06/9822:32

MEDIUM         25-Jun-98 1:32:19PM                   25 jun 98 22:32:20

LONG              June 25,19981:32:19 PM PDT     25 jun 199822:32:20GMT+02:00

FULL               Thursday, June 25,19981:32:19   25 jun 199822 h 32

  0’clock PM PDT                              GMT+02:00

To create our own customized formats, we can also use the SimpleDateFormat class. When we create a SimpleDateFormat object, we specify a pattern String. The ontents of the pattern String determine the format of the date and time.

Example:

Date t;
String s;
SimpleDateFormat df;
df = new SimpleDateFormat(pattern, currentLocale);
t = new Date();
s = df.format(t);
System.out.println(pattern +” ”+ s);

The following table shows the output generated when the U.S. Locale is specified:

Pattern                                                    Output 

dd.MM.yy                                                  09.04.98

yyyy.MM.dd G ‘at’ hh:mm:ss z              1998.04.09AD at 06:15:55PDT

EEE,MMM d, “yy                                     Thu, Apr 9, ’98

h:mm a                                                       6:15 PM

H:mm                                                         18:15

H:mm:ss:SSS                                            18:15:55:624

K:mm a,z                                                   6:15PM,PDT

yyyy.MMMMM.dd GGG hh:mm aaa  1998.April09 AD 06:15 PM

The SimpleDateFormat class is locale sensitive. If we instantiate SimpleDateFormat without a Locale parameter, it will format the date and time according to the default Locale. Both the pattern and the Locale determine the format. SimpleDateFormat formats a date and time differently if the Locale varies.

today = new Date();

result = formatter.format(today);

System.out.println(“Locale:” + currentLocale.toString());

System.out.println(“Result::”+ result);

When the currentLocale is set to different values, the preceding code example generates this output:

Locale: fr_FR

Result: ven 10 avr 98

Locale: de_DE

Result: Fr 10 Apr 98

Locale: en_US

Result: Thu 9 Apr 98

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class DateFormatExample
{
    public static void main( String[] args)
    throws Exception
    {
        Date t = Calendar.getInstance().getTime();
        DateFormat shortf =
        SimpleDateFormat.getDateInstance(SimpleDateFormat.SHORT );
        DateFormat longf = SimpleDateFormat.getDateInstance(SimpleDateFormat.LONG );
   DateFormat mediumf =SimpleDateFormat.getDateTimeInstance(SimpleDateFormat.MEDIUM, SimpleDateFormat.LONG );
        System.out.println("Short format :"+ shortf.format(t) );
        System.out.println("Long format :"+ longf.format(t) );
        System.out.println("Medium format :"+ mediumf.format(t) );
        String dateAsText = shortf.format(t);
        Date textAsDate = shortf.parse(dateAsText);
        System.out.println( textAsDate );
    }
}

Date Format Example

You’ll also like:

  1. Print System Date and Time in Java Example
  2. MySql Date Function in Java Servlet
  3. SQL DATE Function
  4. C Program Date is Valid or Not
  5. MySql FORMAT Functon in Java Servlet
Next →
← Prev
Like/Subscribe us for latest updates     

About Dinesh Thakur
Dinesh ThakurDinesh Thakur holds an B.C.A, MCDBA, MCSD certifications. Dinesh authors the hugely popular Computer Notes blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps.

Dinesh Thakur is a Freelance Writer who helps different clients from all over the globe. Dinesh has written over 500+ blogs, 30+ eBooks, and 10000+ Posts for all types of clients.


For any type of query or something that you think is missing, please feel free to Contact us.


Primary Sidebar

Java Tutorials

Java Tutorials

  • Java - Home
  • Java - IDE
  • Java - Features
  • Java - History
  • Java - this Keyword
  • Java - Tokens
  • Java - Jump Statements
  • Java - Control Statements
  • Java - Literals
  • Java - Data Types
  • Java - Type Casting
  • Java - Constant
  • Java - Differences
  • Java - Keyword
  • Java - Static Keyword
  • Java - Variable Scope
  • Java - Identifiers
  • Java - Nested For Loop
  • Java - Vector
  • Java - Type Conversion Vs Casting
  • Java - Access Protection
  • Java - Implicit Type Conversion
  • Java - Type Casting
  • Java - Call by Value Vs Reference
  • Java - Collections
  • Java - Garbage Collection
  • Java - Scanner Class
  • Java - this Keyword
  • Java - Final Keyword
  • Java - Access Modifiers
  • Java - Design Patterns in Java

OOPS Concepts

  • Java - OOPS Concepts
  • Java - Characteristics of OOP
  • Java - OOPS Benefits
  • Java - Procedural Vs OOP's
  • Java - Polymorphism
  • Java - Encapsulation
  • Java - Multithreading
  • Java - Serialization

Java Operator & Types

  • Java - Operator
  • Java - Logical Operators
  • Java - Conditional Operator
  • Java - Assignment Operator
  • Java - Shift Operators
  • Java - Bitwise Complement Operator

Java Constructor & Types

  • Java - Constructor
  • Java - Copy Constructor
  • Java - String Constructors
  • Java - Parameterized Constructor

Java Array

  • Java - Array
  • Java - Accessing Array Elements
  • Java - ArrayList
  • Java - Passing Arrays to Methods
  • Java - Wrapper Class
  • Java - Singleton Class
  • Java - Access Specifiers
  • Java - Substring

Java Inheritance & Interfaces

  • Java - Inheritance
  • Java - Multilevel Inheritance
  • Java - Single Inheritance
  • Java - Abstract Class
  • Java - Abstraction
  • Java - Interfaces
  • Java - Extending Interfaces
  • Java - Method Overriding
  • Java - Method Overloading
  • Java - Super Keyword
  • Java - Multiple Inheritance

Exception Handling Tutorials

  • Java - Exception Handling
  • Java - Exception-Handling Advantages
  • Java - Final, Finally and Finalize

Data Structures

  • Java - Data Structures
  • Java - Bubble Sort

Advance Java

  • Java - Applet Life Cycle
  • Java - Applet Explaination
  • Java - Thread Model
  • Java - RMI Architecture
  • Java - Applet
  • Java - Swing Features
  • Java - Choice and list Control
  • Java - JFrame with Multiple JPanels
  • Java - Java Adapter Classes
  • Java - AWT Vs Swing
  • Java - Checkbox
  • Java - Byte Stream Classes
  • Java - Character Stream Classes
  • Java - Change Color of Applet
  • Java - Passing Parameters
  • Java - Html Applet Tag
  • Java - JComboBox
  • Java - CardLayout
  • Java - Keyboard Events
  • Java - Applet Run From CLI
  • Java - Applet Update Method
  • Java - Applet Display Methods
  • Java - Event Handling
  • Java - Scrollbar
  • Java - JFrame ContentPane Layout
  • Java - Class Rectangle
  • Java - Event Handling Model

Java programs

  • Java - Armstrong Number
  • Java - Program Structure
  • Java - Java Programs Types
  • Java - Font Class
  • Java - repaint()
  • Java - Thread Priority
  • Java - 1D Array
  • Java - 3x3 Matrix
  • Java - drawline()
  • Java - Prime Number Program
  • Java - Copy Data
  • Java - Calculate Area of Rectangle
  • Java - Strong Number Program
  • Java - Swap Elements of an Array
  • Java - Parameterized Constructor
  • Java - ActionListener
  • Java - Print Number
  • Java - Find Average Program
  • Java - Simple and Compound Interest
  • Java - Area of Rectangle
  • Java - Default Constructor Program
  • Java - Single Inheritance Program
  • Java - Array of Objects
  • Java - Passing 2D Array
  • Java - Compute the Bill
  • Java - BufferedReader Example
  • Java - Sum of First N Number
  • Java - Check Number
  • Java - Sum of Two 3x3 Matrices
  • Java - Calculate Circumference
  • Java - Perfect Number Program
  • Java - Factorial Program
  • Java - Reverse a String

Other Links

  • Java - PDF Version

Footer

Basic Course

  • Computer Fundamental
  • Computer Networking
  • Operating System
  • Database System
  • Computer Graphics
  • Management System
  • Software Engineering
  • Digital Electronics
  • Electronic Commerce
  • Compiler Design
  • Troubleshooting

Programming

  • Java Programming
  • Structured Query (SQL)
  • C Programming
  • C++ Programming
  • Visual Basic
  • Data Structures
  • Struts 2
  • Java Servlet
  • C# Programming
  • Basic Terms
  • Interviews

World Wide Web

  • Internet
  • Java Script
  • HTML Language
  • Cascading Style Sheet
  • Java Server Pages
  • Wordpress
  • PHP
  • Python Tutorial
  • AngularJS
  • Troubleshooting

 About Us |  Contact Us |  FAQ

Dinesh Thakur is a Technology Columinist and founder of Computer Notes.

Copyright © 2025. All Rights Reserved.

APPLY FOR ONLINE JOB IN BIGGEST CRYPTO COMPANIES
APPLY NOW