srikotesh
asked on
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
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
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
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
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
http://www.xyzws.com/javafaq/how-to-use-simpledateformat-class-formating-parsing-date-and-time/142
ASKER
Hi dpearson,
i haven't found any of the example related to my question in the above links
i haven't found any of the example related to my question in the above links
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) ;
String scheduleDate ="2014-9-26 23:10:15";
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-
Date parsed = format.parse(scheduleDate)
System.out.println(parsed)
ASKER
"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 .
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 .
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(scheduleDate 1) ;
System.out.println(parsed1 ) ;
Any good?
Doug
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("
SimpleDateFormat format1 = new SimpleDateFormat("EEE MMM dd HH:mm:ss yyyy") ;
Date parsed1 = format1.parse(scheduleDate
System.out.println(parsed1
Any good?
Doug
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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?
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?
ASKER
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
http://stackoverflow.com/questions/2400955/how-to-store-java-date-to-mysql-datetime
I THINK THIS MAY SOLVE MY PROBLEM I WILL TRY
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
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
now i want to insert in database with the type date time
Type java.util.Date/java.sql.Da
ASKER
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
How to get default date value in this format "0000-00-00 00:00:00"
it should be of type date
ASKER
i want to show date value in zeros
What do you mean by
default date value?
i want to show date value in zerosAnd that would mean ... what?
ASKER
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
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
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
i cant make a change in table to made it as nullable.
only i can do is i have to pass some default values
only i can do is i have to pass some default values
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
The upshot is it's a bit ugly - you can send '00-00-00' and MySQL will honor thatYes, 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.
ASKER
"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
how to send in this format of type date 00-00-00
Call setString in the PreparedStatement
ASKER