I want to insert a date to a mysql database table. The datatype of the related column in mysql
database table is datetime. I used the following code.
String date="2013.05.15";
Timestamp timestamp = new Timestamp(Long.parseLong(date));
when I am inserting timestamp value, it throws numberformat exception. Anybody please tell me
how to insert the value to the database.
Thank You .
You're not parsing the date correctly. Actually you're trying to
parse it as long which is a number, not a date. You can use this code.
String date="2013.05.15";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd");
Date dt = sdf.parse(date);
Timestamp timestamp = new Timestamp(dt.getTime());
Either use NOW() function of mysql or use timestamp to insert into mysql.
and make sure datatype of mysql field datetime
Timestamp timestamp = new Timestamp(new Date().getTime());
Timestamp dateForDb= timestamp;
insert dateForDb into your database.
Related
I'm using JPA and I want to store the date in this format dd/MM/yyyy HH:mm:ss
So I create a function
public static String getNowDate() {
Date date = new Date();
final DateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
return sdf.format(date);
}
The problem is that this returns a String and I need a date.
#Temporal(TemporalType.TIMESTAMP)
private Date creationDate;
How do I make this work so I can save date and time exactly like that?
I know a easy solution is to declare creationDate as String. Is this too bad?
There is a problem with the premise to your question. Ideally, your current timestamp will be stored in a SQL database, in some sort of date column, and not as text. Since you are using JPA, backed by JDBC, you should just be inserting a date type. So, something like the following should work:
Timestamp timestamp = new Timestamp(System.currentTimeMillis());
// or maybe just
Date now = new Date();
Then, just let JPA worry about how to martial the current timestamp into your database table. If you really need to format your timestamp as shown, then worry about this in your Java code somewhere.
I wish to produce a current timestamp in the format of yyyy-MM-dd HH:mm:ss. I have written up the following code, but it always gives me this format yyyy-MM-dd HH:mm:ss.x
How do you get rid of the .x part ?
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String currentTime = df.format(new Date());
Timestamp timestamp = Timestamp.valueOf(currentTime);
I need to have a timestamp type for writing into mysql.
Probably you're looking at the String representation the Timestamp object gives in your database engine or its Java representation by printing it in the console using System.out.println or by another method. Note that which is really stored (in both Java side or in your database engine) is a number that represents the time since epoch (usually January 1st 1970) and the date you want/need to store.
You should not pay attention to the String format it is represented when you consume your Timestamp. This can be easily demostrated if you apply the same SimpleDateFormat to get a String representation of your timestamp object:
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String currentTime = df.format(new Date());
Timestamp timestamp = Timestamp.valueOf(currentTime)
//will print without showing the .x part
String currentTimeFromTimestamp = df.format(currentTime);
Anyway, if you want the current time, just create the Timestamp directly from the result of new Date:
Timestamp timestamp = new Timestamp(new Date().getTime());
You can insert the timestamp as a String to the MySQL table. Your String representation in currentTime is sufficient.
The best way to write Timestamp or any data type in Java is to use PreparedStatement and an appropriate method
PreparedStatement ps = conn.prepareStatement("update t1 set c1=?");
ps.setTimestamp(1, new java.sql.Timestamp(new Date().getTime()));
ps.executeUpdate();
I insert date into database by the following code
String sql = "Insert Into Purchase (Purchase_ID, Purchase_Date) values (?,Date())";
and yes, my database store the value with the current date only
But when I retrieve the Purchase Date from my database into JTable, the result display the date when I insert and with the time 00:00:00:00.
How can I want to display the result with the date only?
Use a SimpleDateFormat. For instance:
SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy");
String s = format.format(yourDate);
I'm having an issue while trying to insert a date into an SQL server using Java:
//final String input = "20120823151034";
final String input = "06282013143533";
final DateFormat df = new SimpleDateFormat("MMddyyyyHHmmss");
final Calendar c = Calendar.getInstance();
c.setTime(df.parse(input));
//c.add(Calendar.MILLISECOND, 1);
String s=df.format(c.getTime());
java.util.Date parsedUtilDate = df.parse(s);
java.sql.Date sqltDate= new java.sql.Date(parsedUtilDate.getTime());
System.out.println(sqltDate);
Here, I'm expecting the complete string being output as the year, month, day, hour, minutes, and seconds to be inserted into SQL server, but I'm only getting the year, month, and the date. What should I do to insert the date and the time into the database?
check your DATA type in database either it should be Timestamp or Datetimefor this purpose you can use database function NOW() for current date and time.and convert your date into timestamp
Timestamp timestamp = new Timestamp(new Date().getTime());
and insert this timestamp into database
java.sql.Date can be used to store only the year, month and day of the month. It can't be used to store the time of the day.
In order to store time, you would have to use Timestamp. In your above code, replace
java.sql.Date sqltDate= new java.sql.Date(parsedUtilDate.getTime());
by
java.sql.Timestamp timestamp = new java.sql.Timestamp(parsedUtilDate.getTime());
The above changes will make things work fine.
I am trying to set a timestamp in my database using java, however in my table all I get is the date, and no time (i.e., looks like "2010-09-09 00:00:00").
I am using a datetime field on my mysql database (because it appears that datetime is more common than timestamp). My code to set the date looks like this:
PreparedStatement ps = conn.prepareStatement("INSERT INTO mytable (datetime_field) VALUES (?)")
java.util.Date today = new java.util.Date();
java.sql.Date timestamp = new java.sql.Date(today.getTime());
ps.setDate(1, timestamp);
ps.executeUpdate();
How do I set the date to include the time?
Edit: I changed the code as per below, and it sets both the date and the time.
PreparedStatement ps = conn.prepareStatement("INSERT INTO mytable (datetime_field) VALUES (?)")
java.util.Date today = new java.util.Date();
java.sql.Timestamp timestamp = new java.sql.Timestamp(today.getTime());
ps.setTimestamp(1, timestamp);
ps.executeUpdate();
Use java.sql.Timestamp and setTimestamp(int, Timestamp). java.sql.Date is date-only, regardless of the type of the column it's being stored in.
Not exactly sure what you need to use, but
ps.setDate();
expects a column type of Date. So it's normalizing it, removing the time.
Try
ps.setTimetamp();
You could use :
private static String getTimeStamp() {
SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return f.format(new Date());
}