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
Symbol | Meaning of Code |
---|---|
y | Year |
M | Month in year |
w | Week in year |
D | Day in year |
d | Day in month |
E | Day in week |
a | am/pm designator |
H | Hour in day, 24-hour |
h | Hour in day, 12-hour |
m | Minutes in hour |
s | Seconds in hour |
S | Milliseconds |
z | Timezone |
Output:
Core Java Related Tutorials
- Base64 Encoding and Decoding Examples in Java 8
In this tutorial we will discuss how to Encode and Decode using Base64 using Java 8, which now finally has native Base64 support. - Base64 Encoding and Decoding Examples in Java using Google Guava
This tutorial will introduce how to Encode and Decode using Base64 using Google’s Guava Project Open Source library. - Base64 Encoding and Decoding Examples in Java using Apache Commons
This tutorial will introduce Base64 encoding and decoding examples using the Apache Commons Codec library. - Custom Number Formatting in Java
In this example we will show you how to use the NumberFormat and DecimalFormat classes to format numbers using special patterns. - 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.
Please Share Us on Social Media






Leave a Reply