i am trying to insert html5 datepicker's date into Oracle table having datatype date,i've tried so many thing but i am still facing problem,plz help me to solve the issue.
static int addNotification(String date,String message)
{
int i=0;
try {
ps=con.prepareStatement("INSERT into ev values(?,to_date(?,'DD-MON-YYYY'))");
ps.setString(1, message);
DateFormat originalFormat = new SimpleDateFormat("yyyyMMdd");
DateFormat targetFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
java.util.Date dt = originalFormat.parse(date);
String formattedDate = targetFormat.format(dt);
System.out.println(formattedDate);
ps.setString(2, formattedDate);
// ps.setDate(2,formattedDate);
i=ps.executeUpdate();
}
everytime i am making changes , i am getting new exception for ex.
java.sql.SQLException: ORA-01861: literal does not match format string
java.text.ParseException: Unparseable date('YYYY-MM-DD')
plz help me to solve this problem.
You can use PreparedStatement.setDate(int parameterIndex,Date x) as follows.
try {
ps=con.prepareStatement("INSERT into ev values(?,?)");
ps.setString(1, message);
DateFormat originalFormat = new SimpleDateFormat("yyyyMMdd");
java.util.Date dt = originalFormat.parse(date);
ps.setDate(2,dt);
i=ps.executeUpdate();
}
if you want to convert the date then use,
DateFormat originalFormat = new SimpleDateFormat("yyyyMMdd");
if not would suggest you not capture html 5 datepickers data directly ,I would rather convert it using javascript and send it to response.
go through date class :-
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date
Related
In my Java Cloud Endpoints API I have some code to get the current date and then store that date in my Cloud SQL (MySQL) database:
Calendar c = Calendar.getInstance();
SimpleDateFormat df = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
String formattedDate = df.format(c.getTime());
//formattedDate is then inserted into the database
Later on in my Android Activity I query the database and get the date back as a String that looks like:
2015-06-24 17:53:01
Now I want to format this date to display it like 06/24/2015 on the UI of my Activity. To accomplish this I do the following:
//I get a string like 2015-06-24 17:53:01 passed in from another activity
//which in-turn got it from the MySQL database
datetime = getIntent().getExtras().getString("datetime");
//first convert the string datatime to a date object
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
Date convertedDate = new Date();
try {
convertedDate = dateFormat.parse(datetime);
} catch (ParseException e) {
e.printStackTrace();
}
//then format that date object the way you want
SimpleDateFormat df = new SimpleDateFormat("MM/dd/yyyy");
String formattedDate = df.format(convertedDate);
//set the TextView in my Activity to display 06/24/2015
myDateTextView.setText(formattedDate);
This works. But man is it a convoluted way to do something simple. I am wondering if there is a more efficient way to do this?
You should save long value represented date into DB.
long l = c.getTimeInMillis()
and this value you should save.
answer on your question below:
Date date=new Date(l);
SimpleDateFormat df2 = new SimpleDateFormat("MM/dd/yyyy");
String dateText = df2.format(date);
System.out.println(dateText);
The simplest way is to store date in long value at DB, after that you can format to string in a very simple way.
DateFormat.format("MMM dd, yyyy", milliseconds).toString();
If you can change the source code of insertion into database, try inserting the timestamp in Long. Then conversion of timestamp to date format should be straight forward.
in Android,
final String data = DateFormat.format("MM/dd/yyyy",timestamp);
I have jsp code that get date from the user (using html form) and I try to use the date in preparedStatament (sql).
the user enter date in format dd/mm/yyyy and i try to convert it into sql date yyyy-mm-dd...
this is the input type in the htl form:
<td><input type=date name="fdate"/></td>
<td><input type=date name="tdate"/></td>
this is the source code of dates.jsp (that run the sql):
SimpleDateFormat format = new SimpleDateFormat("yyyy-mm-dd");
String fDateStr = request.getParameter("fdate");
String tDateStr = request.getParameter("tdate");
Date fdate = format.parse(fDateStr);
Date tdate = format.parse(tDateStr);
PreparedStatement prSelect = con.prepareStatement("select show_id,date,artist, name from shows where date between ? and ?");
prSelect.setDate(1, fdate);
prSelect.setDate(2, tdate);
This is the error that i get from setDate line:
"The method setDate(int, java.sql.Date) in the type PreparedStatement is not applicable for the arguments (int, java.util.Date)"
How can I solve it?
SimpleDateFormat format = new SimpleDateFormat("yyyy-mm-dd"); should be like
SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy",Locale.ENGLISH);
Try to use Locale for safety
Again Date fdate = format.parse(fDateStr); gives you java.util.Date you need to convert it o java.sql.Date to set it to PreparedStatement like this
public static Date convertUtilDateToSqlDate(java.util.Date date){
if(date != null) {
Date sqlDate = new Date(date.getTime());
return sqlDate;
}
return null;
}
Then
prSelect.setDate(1, convertUtilDateToSqlDate(fdte));
prSelect.setDate(2, convertUtilDateToSqlDate(tdate));
You must use java.sql.Date instead of java.util.Date
Try this
prSelect.setDate(1, java.sql.Date.valueOf(fDateStr));
prSelect.setDate(2, java.sql.Date.valueOf(tDateStr));
I am having a column in my table of type TIMESTAMP.
In my servlet am writing the code to insert current date and time in this column like this :
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = new Date();
String sendDate = dateFormat.format(date);
ps.setString(4,sendDate);
But it throws an Exception that:
java.sql.SQLException: [Oracle][ODBC][Ora]ORA-01843: not a valid month
What can be the reason?
Please help.
Why do you use ps.setString ?
You should use ps.setDate.
EDIT
Date dateNow = new java.sql.Date(System.CurrentTimeMillis())
ps.setDate(dateNow);
Better to use timestamp tough:
ps.setTimestamp(1, new Timestamp(System.currentTimeMillis());
I am trying to cast a date format which is string resultset from database to a standard format, but using simpledateformat gives following error:
Exception in thread "main" java.lang.IllegalArgumentException: Cannot format given Object as a Date
at java.text.DateFormat.format(Unknown Source)
at java.text.Format.format(Unknown Source)
at CopyEJ.CopyEJ.main(CopyEJ.java:113)
RROR: JDWP Unable to get JNI 1.2 environment, jvm->GetEnv() return code = -2
JDWP exit error AGENT_ERROR_NO_JNI_ENV(183): [../../../src/share/back/util.c:838]
With De-bug I found out variable time_stmp has value "2013-04-19 17:29:06" I want to cast to this:
yyyyMMddhhmmss
Here's code:
SimpleDateFormat df = new SimpleDateFormat("yyyyMMddhhmmss");
ResultSet rs_dt = cmd1.executeQuery(dt);
String time_stmp = null;
while (rs_dt.next())
{
time_stmp = rs_dt.getString(1);
}
StringBuilder ts = new StringBuilder( df.format( time_stmp ) );
What is the best way to achieve this?
Your Simple DateFormat has the wrong datepattern. You have to parse it to date with the pattern of you DB, then parse it back to String.
Try it this way:
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
ResultSet rs_dt = cmd1.executeQuery(dt);
String time_stmp = null;
while (rs_dt.next())
{
time_stmp = rs_dt.getString(1);
}
Date d = null;
try {
Date d = df.parse(time_stmp);
} catch (ParseException ex) {
Logger.getLogger(Prime.class.getName()).log(Level.SEVERE, null, ex);
}
SimpleDateFormat df2 = new SimpleDateFormat("yyyyMMddhhmmss");
StringBuilder ts = new StringBuilder( df2.format(d) );
By the way:
If you want your output to be in 24h-Format, then you have to use the pattern yyyyMMddHHmmss
The problem is, format of the date retrieved from DB is yyyy-dd-MM HH:mm:ss and you are trying to parse it with the format yyyyMMddhhmmss
You can do something like this
String date = "2013-04-19 17:29:06";
Date d = new SimpleDateFormat("yyyy-dd-MM HH:mm:ss").parse(date);
SimpleDateFormat outputFormat = new SimpleDateFormat("yyyyMMddhhmmss");
System.out.println(outputFormat.format(d));
You need to use DateFormat.parse() here since the argument is of type String. DateFormat.format() on the other hand is used to format Date objects as String.
StringBuilder ts = new StringBuilder(df.format(df.parse(time_stmp)));
Also, it's recommended to save date/time data as a TimeStamp in database instead of a String.
Like it was mention you need to use
SimpleDateFormat.parse()
Also if your string is in the form
`2013-04-19 17:29:06`
You will want to construct your SimpleDateFormat for reading in like this
`yyyy-MM-dd HH:mm:ss` //make sure you use uppercase H for 24 hour times
Use SimpleDateFormat.parse() to convert a String to a Date.
Use SimpleDateFormat.format() to convert a Date to a String.
All together now:
SimpleDateFormat dbFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
SimpleDateFormat stdFormat = new SimpleDateFormat("yyyyMMddhhmmss");
String s = stdFormat.format(dbFormat.parse(input));
You can modify the formats to suit your needs.
Hi you can use below code to format date as of your choice (Ex: yyyy-MM-dd HH:mm:ss)
Calendar calendar = Calendar.getInstance();
Date calDate = calendar.getInstance().getTime();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println("Calendar Date: " + calDate);
System.out.println("Your Choice Date format (yyyy-MM-dd HH:mm:ss): "+dateFormat.format(calDate));
This will work
SimpleDateFormat df = new SimpleDateFormat("YYYY-MM-dd hhhh:mm:ss");
String time_stmp ="2013-04-19 17:29:06";
Date date=df.parse(time_stmp);
StringBuilder ts = new StringBuilder( df.format( date ) );
System.out.println(ts);
I am trying to in insert date into sqlite using:
SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
String data = format.format(new Date(19900101));
System.err.println("Time is.."+format.format(19900101));
But i am getting 19700101 in log. i am getting same result even if i change the dates as 20000101 or any thing.
Any one suggest me why it happens?
the code is wrong
SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
Date data = format.parse("19900101");
System.out.println("Time is.."+data );
when u use new Date(123) it expects some time in milliseconds i think
Change your code as for formatting current string to date :
SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
Date convertedDate = (Date) format.parse("19900101");
SimpleDateFormat formats = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String fmconvertedDate =formats.format(convertedDate);
System.err.println("Time is.."+fmconvertedDate);
and output is :
Time is..1990-01-01 00:00:00