Asha
Asha

Reputation: 115

How to change date format from yyyy-MM-dd to dd-MMM-yyyy

I am retrieving date from database. Datatype is'date' with date format 2015-12-16.

I need to set that date to my bean class variable.Datatype is Date with format 16-Dec-2015

These are the date formats i am using

      SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM-yyyy");
              SimpleDateFormat formatter1 = new SimpleDateFormat("yyyy-MM-dd");

              Date date = null;
              String formatteddate = null;

                try {
                    formatteddate = formatter.format(rs.getDate("dol"));
                    System.out.println("formatteddate=============="+formatteddate);
                    date = formatter.parse(formatteddate);
                    System.out.println("date========="+date);
                } catch (ParseException e) {
                    e.printStackTrace();
                }


              joborderbean.setDol(date);

formatteddate==============15-Dec-2015

I want to display the above format i.e,15-Dec-2015. But it is diplaying the below format

date=========Tue Dec 15 00:00:00 IST 2015

Please help me

Upvotes: 0

Views: 2527

Answers (2)

Blip
Blip

Reputation: 3171

First of all the Resultset objects getDate method return a java.sql.Date object. So in order to store the Date in the date object use :

date = rs.getDate("dol");

instead of the code:

formatteddate = formatter.format(rs.getDate("dol"));
System.out.println("formatteddate=============="+formatteddate);
date = formatter.parse(formatteddate);
System.out.println("date========="+date);

now in order to test the date you can use:

SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM-yyyy");
System.out.printLn("date========="+formatter.format(date));

This will give you the required output.

The last output you are getting because of the code System.out.println("date========="+date); Here the toString method of the date object is getting called which has a default as in the documentation as :

Converts this Date object to a String of the form:

dow mon dd hh:mm:ss zzz yyyy

where:

  • dow is the day of the week (Sun, Mon, Tue, Wed, Thu, Fri, Sat).
  • mon is the month (Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec).
  • dd is the day of the month (01 through 31), as two decimal digits.
  • hh is the hour of the day (00 through 23), as two decimal digits.
  • mm is the minute within the hour (00 through 59), as two decimal digits.
  • ss is the second within the minute (00 through 61, as two decimal digits.
  • zzz is the time zone (and may reflect daylight saving time). Standard time zone abbreviations include those recognized by the method parse. If time zone information is not available, then zzz is empty - that is, it consists of no characters at all.
  • yyyy is the year, as four decimal digits.

Upvotes: 0

Veshraj Joshi
Veshraj Joshi

Reputation: 3589

Do like following:

SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM-yyyy");
String dateformat=fromatter.format(mysqldate);
System.out.println("first date format"+ dateformat);
SimpleDateFormat formatter1 = new SimpleDateFormat("yyyy-MM-dd");
String dateformat1=formatter1.format(mysqldate);
System.out.println("second date format"+dateforamt1);

also you can edit the sql fired to get the result like

 select DATE_FORMAT(date_field,'%Y-%b-%d')

and

select DATE_FORMAT(date_field,'%d-%b-%Y')

Upvotes: 1

Related Questions