how to convert String type of date to date type of date and that date should be in specified format

Hi Experts,

I have string date now i want to convert this string date to date type date
that should be in below format.
String schedueDate ="2014-9-26 23:10:15";

now i want same date into date type how do i convert

I have tried like
Date date = formatter.parse("string date");
but it is giving diff format

Can some one suggest me,
Thanks
LVL 2
srikoteshAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

srikoteshAuthor Commented:
i want this format 'YYYY-MM-DD HH:MM:SS'
dpearsonCommented:
Check out Java's SimpleDateFormat class:
http://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html

You give it a String for the format (a bit like you wrote it but with slightly different cases for the different elements) and it can either parse a date as a String or print out a Date object in that format.

Should give you what you need.

Doug
dpearsonCommented:
BTW in case it's not obvious how to use the class, there are examples here:
http://www.xyzws.com/javafaq/how-to-use-simpledateformat-class-formating-parsing-date-and-time/142
Exploring ASP.NET Core: Fundamentals

Learn to build web apps and services, IoT apps, and mobile backends by covering the fundamentals of ASP.NET Core and  exploring the core foundations for app libraries.

srikoteshAuthor Commented:
Hi dpearson,

i haven't found any of the example related to my question in the above  links
dpearsonCommented:
OK, if the docs aren't clear - try this?

            String scheduleDate ="2014-9-26 23:10:15";
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss") ;
            Date parsed = format.parse(scheduleDate) ;
            System.out.println(parsed) ;
srikoteshAuthor Commented:
"I have tried like
Date date = formatter.parse("string date");
but it is giving diff format"

i have already tried in this way  if u see the o/p:
Fri Sep 26 23:10:15 IST 2014

but i need in this format  yyyy-MM-dd HH:mm:ss

I think it is not possible .
dpearsonCommented:
I don't really understand what you're trying to do.

Are you saying now you're trying to parse strings in this format:
Fri Sep 26 23:10:15 IST 2014
and convert them to
2014-9-26 23:10:15

If so how about this?

            String scheduleDate1 = "Fri Sep 26 23:10:15 IST 2014" ;
            scheduleDate1 = scheduleDate1.replaceAll("IST", "") ;   // Take out the IST
            SimpleDateFormat format1 = new SimpleDateFormat("EEE MMM dd HH:mm:ss yyyy") ;
            Date parsed1 = format1.parse(scheduleDate1) ;
            System.out.println(parsed1) ;

Any good?

Doug
dpearsonCommented:
Oops - sorry that previous one didn't convert the output format:

            String scheduleDate1 = "Fri Sep 26 23:10:15 IST 2014" ;
            scheduleDate1 = scheduleDate1.replaceAll("IST", "") ;
            SimpleDateFormat format1 = new SimpleDateFormat("EEE MMM dd HH:mm:ss yyyy") ;
            SimpleDateFormat format2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss") ;

            Date parsed1 = format1.parse(scheduleDate1) ;
            System.out.println(format2.format(parsed1)) ;
srikoteshAuthor Commented:
what is the return type of below line
format2.format(parsed1)

again it is string.

LET ME CLARIFY MY QUESTION AGAIN
I have a date in string format  
now i want to insert in database with the type date time
the date time of mysql format is :'YYYY-MM-DD HH:MM:SS'
how to do?
srikoteshAuthor Commented:
I HAVE SEEN ONE OF THE QUESTION FROM STACK OVER FLOW

http://stackoverflow.com/questions/2400955/how-to-store-java-date-to-mysql-datetime

I THINK THIS MAY SOLVE MY PROBLEM I WILL TRY
dpearsonCommented:
OK I hope you've found a solution - but next time you're asking a question about inserting into a MySQL database it would help if your question mentioned:
a) MySQL
b) Inserting into a database

In any case, you may have realized by now that when inserting into MySQL as "datetime" you should send a java.sql.Timestamp object which you can trivially create from a java.util.Date object like this:

Date date = new Date() ; // You can get this by parsing whatever format you wish (see above code)
Timestamp timestamp = new Timestamp(date.getTime()) ;

Send the timestamp object to mysql.  It will handle the formatting etc.  You don't need to worry about YYYY etc.

Doug
CEHJCommented:
now i want to insert in database with the type date time

Type java.util.Date/java.sql.Date have nothing to do with formats. They are essentially numbers. Only date strings have formats
srikoteshAuthor Commented:
hi CEHJ and dpearson,

How to get default date value  in this format  "0000-00-00 00:00:00"
it should be of type date
srikoteshAuthor Commented:
i want to show date value in zeros
CEHJCommented:
What do you mean by
default date value
?
CEHJCommented:
i want to show date value in zeros
And that would mean ... what?
srikoteshAuthor Commented:
i have date field
whenver i dont pass any date value from ui
i want to insert in database  that value in zeros  because this column is mandatory field to me
CEHJCommented:
Then you have a mismatch between your schema and your data. If you have null dates and yet the table doesn't allow them, then either the table definition or the data is wrong.
srikoteshAuthor Commented:
i cant make a change in table to made it as nullable.
only i can do is i have to pass some default values
CEHJCommented:
afaik the not null column default is all zeros. Therefore, you can have two different statements: one which includes the date column(s) and one which doesn't. Use the latter when you have a null date and you'll leave the default in the table
dpearsonCommented:
You should read the section in the MySQL docs about the special "zero date" field and how it's handled:
https://dev.mysql.com/doc/refman/5.1/en/using-date.html
Read the comments at the bottom of the page too - since they talk more about it.

The upshot is it's a bit ugly - you can send '00-00-00' and MySQL will honor that, but it may also convert it to the value "null" when it's passed back and forth in code, because '00-00-00' isn't a real date.  So watch out for that.

Doug

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
CEHJCommented:
The upshot is it's a bit ugly - you can send '00-00-00' and MySQL will honor that
Yes, but you'd have to be sending a String, which you should try to avoid really, plus it's more efficient to be sending nothing at all for that column.
srikoteshAuthor Commented:
"The upshot is it's a bit ugly - you can send '00-00-00' and MySQL will honor that"
how  to send  in this format of type date 00-00-00
CEHJCommented:
Call setString in the PreparedStatement
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.