Custom Date Formatting in Java

In this example we will show you how to use the SimpleDateFormat class to format Date objects using special patterns  to better fit the needs of the application.     The examples below show you how to easily do this in Java.

Custom Date Format Java Code

package com.omega.tutorial;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

public class CustomDateFormatExample {

  public static void main(String[] args) {
    Date myDate = new Date();
    SimpleDateFormat myFormat;
    String result;

    System.out.println("CustomDateFormat Example");
    System.out.println("-------------------------");

    // Default Format
    myFormat = new SimpleDateFormat();
    result = myFormat.format(myDate);
    System.out.println("Pattern #1: " + result);

    // Different date patterns
    myFormat = new SimpleDateFormat("MM/dd/yyyy");
    result = myFormat.format(myDate);
    System.out.println("Pattern #2: " + result);

    myFormat = new SimpleDateFormat("yy-MM-dd");
    result = myFormat.format(myDate);
    System.out.println("Pattern #3: " + result);

    // Different date patterns, with day of week as 3 char
    myFormat = new SimpleDateFormat("EEE, MMM dd, yyyy ");
    result = myFormat.format(myDate);
    System.out.println("Pattern #4: " + result);

    // Different date patterns, with full day of week and time
    myFormat = new SimpleDateFormat(
                       "EEEE, MMMM dd, yyyy 'at' HH:MM:ss a");
    result = myFormat.format(myDate);
    System.out.println("Pattern #5: " + result);

    // Time pattern, with hours, min, secs, ms and time zone
    myFormat = new SimpleDateFormat("HH:MM:ss.SSS zzz");
    result = myFormat.format(myDate);
    System.out.println("Pattern #6: " + result);

    // Date with Era
    myFormat = new SimpleDateFormat("MMMM.yyyy.dd GG");
    result = myFormat.format(myDate);
    System.out.println("Pattern #7: " + result);

    myFormat = new SimpleDateFormat("MMMM.yyyy.dd GG hh:mm.ss");
    result = myFormat.format(myDate);
    System.out.println("Pattern #8: " + result);

    // Adding locales, English, French and Gemany
    myFormat = new SimpleDateFormat(
                              "HH:MM:ss.SSS zzzzz", Locale.ENGLISH);
    result = myFormat.format(myDate);
    System.out.println("Pattern #9: " + result);

    myFormat = new SimpleDateFormat(
                               "HH:MM:ss.SSS zzzzz", Locale.FRANCE);
    result = myFormat.format(myDate);
    System.out.println("Pattern #10: " + result);

    myFormat = new SimpleDateFormat(
                               "HH:MM:ss.SSS zzzzz", Locale.GERMAN);
    result = myFormat.format(myDate);
    System.out.println("Pattern #11: " + result);
  }
}

Useful Formatting Codes

SymbolMeaning of Code
yYear
MMonth in year
wWeek in year
DDay in year
dDay in month
EDay in week
aam/pm designator
HHour in day, 24-hour
hHour in day, 12-hour
mMinutes in hour
sSeconds in hour
SMilliseconds
zTimezone

Output:

date_format_output

Core Java Related Tutorials

Please Share Us on Social Media

Facebooktwittergoogle_plusredditpinterestlinkedinmail

Leave a Reply

Your email address will not be published. Required fields are marked *