how to format date using SimpleDateFormat

16,723

Solution 1

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

Solution 2

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
Share:
16,723
spiderman
Author by

spiderman

#SOreadytohelp

Updated on June 04, 2022

Comments

  • spiderman
    spiderman almost 2 years

    I cannot format a date. dateFormat.format() accepts a Date as argument. So I created a new Date()

    It says the below Date() method is deprecated, and I get the below exception while running.

    exception:

    Exception in thread "main" java.lang.IllegalArgumentException at
    java.util.Date.parse(Date.java:598)
    
    
    public class MyDate {
        public static void main(String[] args) {
    
    
            Date date = new Date("2012-02-16T00:00:00.000-0500");
    
            SimpleDateFormat dateFormat = new SimpleDateFormat(
                    "dd-MMM-yyyy HH:mm:ss");
            String stringDate = dateFormat.format(date);
    
            System.out.println(stringDate); // how do I test this conversion??
    
        }
    }
    

    My database has date of the format - 2012-02-16T00:00:00.000-0500 I need to convert it to string of the format : dd-MMM-yyyy HH:mm:ss

    I'm using Java6

  • Andy Brown
    Andy Brown about 9 years
    @spiderman. You don't need to update your question with the solution; that's why you mark an answer as accepted.
  • spiderman
    spiderman about 9 years
    Even after reading your solution, it took some time for me to put things correctly, so I always update the post to show the complete solution, which might help someone later
  • spiderman
    spiderman about 9 years
    you're right, I 'll post as seperate answer just as a placeholder