Thanks to @Andy Brown. In addition to what Andy Brown has answered, I'm posting the complete snippet

Complete Solution:

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class SampleDate {
    public static void main(String[] args) throws ParseException {
        DateFormat parseFormat = new SimpleDateFormat(
                 "yyyy-MM-dd'T'HH:mm:ss.SSSZ");
        Date date = parseFormat.parse("2012-03-16T00:00:00.000-0500");
        String strDate = parseFormat.format(date);
        System.out.println(strDate);

        // if you get date of type 'java.sql.Date' directly from database cursor like
         //rs.getDate("created_date"), just pass it directly to format()

        SimpleDateFormat dateFormat = new SimpleDateFormat(
                "dd-MMM-yyyy HH:mm:ss");
        String stringDate = dateFormat.format(date);
        System.out.println(stringDate);

    }
}

/*
Output:

2012-03-16T01:00:00.000-0400
16-Mar-2012 01:00:00

*/

you can also convert java.util.Date to java.sql.Date like this,

String dateString = "03-11-2012";
    SimpleDateFormat dateFormat = new SimpleDateFormat("MM-dd-yyyy");
    java.util.Date date = dateFormat.parse(dateString);
    java.sql.Date sqlDate = new Date(date.getTime());
// set the input param type as OracleTypes.DATE and pass the input param date as sqlDate
Answer from spiderman on Stack Overflow
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › text › SimpleDateFormat.html
SimpleDateFormat (Java Platform SE 8 )
October 20, 2025 - It allows for formatting (date → text), parsing (text → date), and normalization. SimpleDateFormat allows you to start by choosing any user-defined patterns for date-time formatting. However, you are encouraged to create a date-time formatter with either getTimeInstance, getDateInstance, ...
🌐
Baeldung
baeldung.com › home › java › java dates › a guide to simpledateformat
A Guide to SimpleDateFormat | Baeldung
January 8, 2024 - First, let’s look at how to instantiate a new SimpleDateFormat object. There are 4 possible constructors – but in keeping with the name, let’s keep things simple. All we need to get started is a String representation of a date pattern we want. Let’s start with a dash-separated date pattern like so: ... This will correctly format a date starting with the current day of the month, current month of the year, and finally the current year.
🌐
Oracle
docs.oracle.com › en › java › javase › 11 › docs › api › java.base › java › text › SimpleDateFormat.html
SimpleDateFormat (Java SE 11 & JDK 11 )
January 20, 2026 - It allows for formatting (date → text), parsing (text → date), and normalization. SimpleDateFormat allows you to start by choosing any user-defined patterns for date-time formatting. However, you are encouraged to create a date-time formatter with either getTimeInstance, getDateInstance, or getDateTimeInstance in DateFormat.
Top answer
1 of 2
5

Thanks to @Andy Brown. In addition to what Andy Brown has answered, I'm posting the complete snippet

Complete Solution:

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class SampleDate {
    public static void main(String[] args) throws ParseException {
        DateFormat parseFormat = new SimpleDateFormat(
                 "yyyy-MM-dd'T'HH:mm:ss.SSSZ");
        Date date = parseFormat.parse("2012-03-16T00:00:00.000-0500");
        String strDate = parseFormat.format(date);
        System.out.println(strDate);

        // if you get date of type 'java.sql.Date' directly from database cursor like
         //rs.getDate("created_date"), just pass it directly to format()

        SimpleDateFormat dateFormat = new SimpleDateFormat(
                "dd-MMM-yyyy HH:mm:ss");
        String stringDate = dateFormat.format(date);
        System.out.println(stringDate);

    }
}

/*
Output:

2012-03-16T01:00:00.000-0400
16-Mar-2012 01:00:00

*/

you can also convert java.util.Date to java.sql.Date like this,

String dateString = "03-11-2012";
    SimpleDateFormat dateFormat = new SimpleDateFormat("MM-dd-yyyy");
    java.util.Date date = dateFormat.parse(dateString);
    java.sql.Date sqlDate = new Date(date.getTime());
// set the input param type as OracleTypes.DATE and pass the input param date as sqlDate
2 of 2
3

If you want to read in the date "2012-02-16T00:00:00.000-0500" you should probably use a SimpleDateFormat to parse it like so:

DateFormat parseFormat = new SimpleDateFormat(
        "yyyy-MM-dd'T'HH:mm:ss.SSSZ");
Date date = parseFormat.parse("2012-02-16T00:00:00.000-0500");

Along with the rest of your code this writes:

16-Feb-2012 05:00:00

The parse format pattern letters are listed in the SimpleDateFormat documentation. The T is escaped with apostrophes.

This answer assumes Java 7, or you would be using the new date & time API from Java 8

🌐
Oracle
docs.oracle.com › javase › 7 › docs › api › java › text › SimpleDateFormat.html
SimpleDateFormat (Java Platform SE 7 )
It allows for formatting (date -> text), parsing (text -> date), and normalization. SimpleDateFormat allows you to start by choosing any user-defined patterns for date-time formatting. However, you are encouraged to create a date-time formatter with either getTimeInstance, getDateInstance, ...
🌐
Jenkov
jenkov.com › tutorials › java-internationalization › simpledateformat.html
Java SimpleDateFormat
The java.text.SimpleDateFormat class is used to both parse and format dates according to a formatting pattern you specify yourself. When parsing dates, the Java SimpleDateFormat typically parses the date from a Java String.
🌐
Oracle
docs.oracle.com › javase › 10 › docs › api › java › text › SimpleDateFormat.html
SimpleDateFormat (Java SE 10 & JDK 10 )
It allows for formatting (date → text), parsing (text → date), and normalization. SimpleDateFormat allows you to start by choosing any user-defined patterns for date-time formatting. However, you are encouraged to create a date-time formatter with either getTimeInstance, getDateInstance, or getDateTimeInstance in DateFormat.
🌐
Oracle
docs.oracle.com › javase › 6 › docs › api › java › text › SimpleDateFormat.html
SimpleDateFormat (Java Platform SE 6)
It allows for formatting (date -> text), parsing (text -> date), and normalization. SimpleDateFormat allows you to start by choosing any user-defined patterns for date-time formatting. However, you are encouraged to create a date-time formatter with either getTimeInstance, getDateInstance, ...
Find elsewhere
🌐
Android Developers
developer.android.com › api reference › simpledateformat
SimpleDateFormat | API reference | Android Developers
Skip to main content · English · Deutsch · Español – América Latina · Français · Indonesia · Polski · Português – Brasil · Tiếng Việt · 中文 – 简体
Top answer
1 of 4
191

Date and time formats are well described below

SimpleDateFormat (Java Platform SE 7) - Date and Time Patterns

There could be n Number of formats you can possibly make. ex - dd/MM/yyyy or YYYY-'W'ww-u or you can mix and match the letters to achieve your required pattern. Pattern letters are as follow.

  • G - Era designator (AD)
  • y - Year (1996; 96)
  • Y - Week Year (2009; 09)
  • M - Month in year (July; Jul; 07)
  • w - Week in year (27)
  • W - Week in month (2)
  • D - Day in year (189)
  • d - Day in month (10)
  • F - Day of week in month (2)
  • E - Day name in week (Tuesday; Tue)
  • u - Day number of week (1 = Monday, ..., 7 = Sunday)
  • a - AM/PM marker
  • H - Hour in day (0-23)
  • k - Hour in day (1-24)
  • K - Hour in am/pm (0-11)
  • h - Hour in am/pm (1-12)
  • m - Minute in hour (30)
  • s - Second in minute (55)
  • S - Millisecond (978)
  • z - General time zone (Pacific Standard Time; PST; GMT-08:00)
  • Z - RFC 822 time zone (-0800)
  • X - ISO 8601 time zone (-08; -0800; -08:00)

To parse:

2000-01-23T04:56:07.000+0000

Use: new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");

2 of 4
55

Let me throw out some example code that I got from http://www3.ntu.edu.sg/home/ehchua/programming/java/DateTimeCalendar.html Then you can play around with different options until you understand it.

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

public class DateTest {
   public static void main(String[] args) {
       Date now = new Date();

       //This is just Date's toString method and doesn't involve SimpleDateFormat
       System.out.println("toString(): " + now);  // dow mon dd hh:mm:ss zzz yyyy
       //Shows  "Mon Oct 08 08:17:06 EDT 2012"

       SimpleDateFormat dateFormatter = new SimpleDateFormat("E, y-M-d 'at' h:m:s a z");
       System.out.println("Format 1:   " + dateFormatter.format(now));
       // Shows  "Mon, 2012-10-8 at 8:17:6 AM EDT"

       dateFormatter = new SimpleDateFormat("E yyyy.MM.dd 'at' hh:mm:ss a zzz");
       System.out.println("Format 2:   " + dateFormatter.format(now));
       // Shows  "Mon 2012.10.08 at 08:17:06 AM EDT"

       dateFormatter = new SimpleDateFormat("EEEE, MMMM d, yyyy");
       System.out.println("Format 3:   " + dateFormatter.format(now));
       // Shows  "Monday, October 8, 2012"

       // SimpleDateFormat can be used to control the date/time display format:
       //   E (day of week): 3E or fewer (in text xxx), >3E (in full text)
       //   M (month): M (in number), MM (in number with leading zero)
       //              3M: (in text xxx), >3M: (in full text full)
       //   h (hour): h, hh (with leading zero)
       //   m (minute)
       //   s (second)
       //   a (AM/PM)
       //   H (hour in 0 to 23)
       //   z (time zone)
       //  (there may be more listed under the API - I didn't check)

   }

}

Good luck!

🌐
UCLA Computer Science
web.cs.ucla.edu › classes › winter15 › cs144 › projects › java › simpledateformat.html
Using SimpleDateFormat for custom date formatting and parsing
SimpleDateFormat format = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy"); Note that the constructor of SimpleDateFormat takes a date/time format string as an input parameter. The date format string "EEE MMM dd HH:mm:ss zzz yyyy" specifies that this SimpleDateFormat will be used for a ...
🌐
DigitalOcean
digitalocean.com › community › tutorials › java-simpledateformat-java-date-format
Master Java Date Formatting: SimpleDateFormat & DateFormat Guide | DigitalOcean
December 20, 2024 - The only major difference between them is that SimpleDateFormat can be used for formatting (Date to String conversion) and for parsing (String to Date conversion) with locale support, whereas DateFormat don’t have locale support. DateFormat is an abstract class that provides base support for date formatting and parsing.
🌐
Javatpoint
javatpoint.com › java-simpledateformat
SimpleDateFormat
Java Date Format There are two classes for formatting dates in Java: and Simple. The java.text. class provides various methods to format and parse date and time in java in language-independent manner. The class is an abstract class.
🌐
Google
developers.google.com › j2objc › simpledateformat
SimpleDateFormat | J2ObjC | Google for Developers
It allows for formatting (date -> text), parsing (text -> date), and normalization. SimpleDateFormat allows you to start by choosing any user-defined patterns for date-time formatting.
🌐
GeeksforGeeks
geeksforgeeks.org › java › simpledateformat-format-method-in-java-with-examples
SimpleDateFormat format() Method in Java with Examples - GeeksforGeeks
December 1, 2021 - // Java code to illustrate format() method import java.text.*; import java.util.Calendar; public class SimpleDateFormat_Demo { public static void main(String[] args) throws InterruptedException { SimpleDateFormat SDFormat = new SimpleDateFormat("MM/dd/yyyy"); // Initializing the calendar Object Calendar cal = Calendar.getInstance(); // Displaying the actual date System.out.println("The original Date: " + cal.getTime()); // Using format() method for conversion String curr_date = SDFormat.format(cal.getTime()); System.out.println("Formatted Date: " + curr_date); } }
🌐
Microsoft Learn
learn.microsoft.com › en-us › dotnet › api › java.text.simpledateformat
SimpleDateFormat Class (Java.Text) | Microsoft Learn
It allows for formatting (date → text), parsing (text → date), and normalization. SimpleDateFormat allows you to start by choosing any user-defined patterns for date-time formatting.
🌐
Oracle
docs.oracle.com › javase › tutorial › i18n › format › simpleDateFormat.html
Customizing Formats (The Java™ Tutorials > Internationalization > Formatting)
When you create a SimpleDateFormat object, you specify a pattern String. The contents of the pattern String determine the format of the date and time.
🌐
Oracle
docs.oracle.com › en › java › javase › 21 › docs › api › java.base › java › text › SimpleDateFormat.html
SimpleDateFormat (Java SE 21 & JDK 21)
January 20, 2026 - It allows for formatting (date → text), parsing (text → date), and normalization. SimpleDateFormat allows you to start by choosing any user-defined patterns for date-time formatting. However, you are encouraged to create a date-time formatter with either getTimeInstance, getDateInstance, or getDateTimeInstance in DateFormat.
🌐
BeginnersBook
beginnersbook.com › 2013 › 05 › simple-date-format-java
Java SimpleDateFormat Class explained with examples
import java.text.SimpleDateFormat; ... sdf.format(date); System.out.println("Date in the format of MM-dd-yyyy : "+dateString); sdf = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss"); dateString = sdf.format(date); System.out.println("Date in the format of dd/MM/yyyy hh:mm:ss : "+dat...