Storing Current date in database - java

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());

Related

How can i enter current date and time in textbox

I want to enter the current date and time along with second in a text box ; How can I do that ?
The code what I have written is as below , but it is only entering the string value what I am passing in send keys , which is clearly indicating I am not in correct track .
Any suggestion ?
// Create object of SimpleDateFormat class and decide the format
DateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy ");
//get current date time with Date()
Date date = new Date();
// Now format the date
String date1= dateFormat.format(date);
// Print the Date
System.out.println(date1);
collection_title.sendKeys("date1");
In collection_title.sendKeys("date1"); you are not using your variable date1, you are using the String "date1" - there should be no quotes around date1.
It should work like this:
DateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
Date date = new Date();
collection_title.sendKeys(dateFormat.format(date));
Maybe you should try to use LocalDateTime.now() or Calender.getInstance() instead of new Date(), because the Date API methods are flawed and deprecated.

Convert string to a formatted date in Android

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);

Incorrect date returned when converting from java.util.date to java.sql.date

I am using Eclipse Juno, GWT and java.
When I convert
I get the date via:
dateBoxDOB = new DateBox();
dateBoxDOB.setFormat(new DefaultFormat(DateTimeFormat.getFormat("dd-MM-yyyy")));
flexTable.setWidget(0, 1, dateBoxDOB);
dateBoxDOB.getDatePicker();
Where I enter 20/04/1961. I then need to convert it from java.util.date to java.sql.date before saving it to MySQL:
java.sql.Date sqlDOB = new java.sql.Date(dateBoxDOB.getValue().getTime());
Window.alert("Util date = " + dateBoxDOB.getValue().getTime());
Window.alert("DOB = " + sqlDOB);
java.sql.Date sqlDateArchived = new java.sql.Date(dateBoxArchived.getValue().getTime());
java.sql.Date sqlPackIn = new java.sql.Date(dateBoxPackIn.getValue().getTime());
java.sql.Date sqlPackOut = new java.sql.Date(dateBoxPackOut.getValue().getTime());
The date displayed by the window alert is -233920800000 for util and 1962-08-04 for sql.
How do I get the correct date please (i.e., 1961-04-20 from sql date)?
Also, if a date is null and exception is thrown. How do I get around this please?
This code works fine for me.
You are getting this issue because you defined the format as dd-MM-yyyy and entered date as 20/04/1961. Notice the change in format '-' vs '/'.
This may help you
Calendar cal = Calendar.getInstance();
DateFormat df=new SimpleDateFormat("dd/mm/yyyy");
String dateStr="20/04/1961";
Date date = df.parse(dateStr);
cal.setTime(date);
Date sqlDate = new java.sql.Date(cal.getTime().getTime());
System.out.println("utilDate:" + df.format(date));
System.out.println("sqlDate:" + df.format(sqlDate));

Date error in android to store in sqlite

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

Time not entered in mysql ? Java

I have a datetime field in mysql table and i am using JPA for persisting data but only date goes in database. Time always shows 00:00:00. What should i do?
I am not doing any manipulation with Date. All i do is to assign new Date() to a variable and store it in database.
What am i doing wrong?
Use the annotation #Temporal(TemporalType.TIMESTAMP)
Example below:
private String getDateTime() {
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date date = new Date();
return dateFormat.format(date);
}
Tips like these can be found on http://www.java-tips.org

Categories