简体   繁体   中英

how to format date using SimpleDateFormat

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM