Skip to main content

ProwessApps

Learn · Practice · Excel

Java Program to display date in given format. December, 27 2013

Java Code Example — Utility Programs

ADVERTISEMENT

Java Program to display date in given format. December, 27 2013

Objective

Write a Java program to display a date in a specific custom format (e.g., December, 27 2013).

Algorithm / Approach

  1. Create a Date object.
  2. Use String.format() with specific Date formatting flags.
  3. %tB extracts the full Month name.
  4. %td extracts the 2-digit day of the month.
  5. %tY extracts the 4-digit year.
  6. Print the formatted string.
Test.java
import java.util.*;
class Test {
 public static void main(String[] ar){
  Date d = new Date();
  String fd;
  fd=String.format("%tB, %td %tY",d,d,d);
  System.out.println(fd);
 }
}

Expected Output

December, 27 2013

Explanation of the Program

  • Java's String.format() method (and System.out.printf()) includes powerful built-in date formatting capabilities, saving you from having to use SimpleDateFormat for basic formatting.
  • Notice that we had to pass the d object three times into the format function, once for each `%t` flag.

Complexity

Time Complexity O(1)
Space Complexity O(1)
ADVERTISEMENT