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);
Related
I have a column field called StartDate in MySQL of Date datatype. In my application, I defined a way to show the current date onn my page like this.
String today = CoreUtil.parseDate(new Date());
This basically returns the date in YYYY-MM-DD format and stored it in a string which is fine.
Now, I would like to pass this value into a function that inserts the value to the column StartDate.
The function I declared is as follow:
public void insert_update(String nodeid,String ts,Date startdt,Date enddt,int enable)
I am calling this function and passing the value today to it like below:
fileFacade.insert_update(...,....,today,....,...);
Now I am not an expert in this date thingy and seeing it wouldn't allow me to pass the value as I defined it as Date parameter, how should I handle this issue?
Should I convert it back to Date format and pass the value or does MySQL allow insertion of string value into a Date datatype column?
I just want to store the value in my table in YYYY-MM-DD format.
edit:
My function:
public void insert_update(String nodeid,String ts,Date startdt,Date enddt,int enable){
try {
// UrlLink attr = em.find(UrlLink.class,n);
String sql="UPDATE urllink SET STARTDT="+startdt+",ENDDT="+enddt+",ENABLE="+enable+"WHERE URL='f0="+nodeid+"&ts="+ts + "'";
em.createNativeQuery(sql).executeUpdate();
}catch (Exception e) {
e.printStackTrace();
}
}
You should not try to convert String into a Date in order to insert it into a table. You can use date type as is using preparedStatement, e.g.:
Date date = new Date();
PreparedStatement pStmt = connection.prepareStatement("<query>");
pStmt.setDate(1, date);
Here's the javadoc.
Update
If the purpose here is to display the date in specified format then I would strongly recommend storing the date as DATE in MySQL in format it while querying. The decision of how we store the date should not be driven by how it needs to be displayed on Front End as we might need different date formats to be displayed across multiple User Interfaces.
Pass String date in method parameter instead of Date type and then format that date type like this:
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd",Locale.ENGLISH);
formatter.parse(date)
I have created a complaint registration form in JSP and in that there is an input field of type "time", also the time should not always be the current time hence I cannot use the CURRENTTIME() method of SQL.
Now while assigning it to the database I am getting HTTP Status 500 – Internal Server Error as "check the manual that corresponds to your MySQL server version for the right syntax to use near '.'20:20:00')' at line 1".
Here I have used the "setString()" method.
I have tried setTime() but that too is not working here, while setString() is working for the date value and date is properly being inserted in the DB.*/
Here is my jsp code for inserting the time in "test" table of "PCS" database .
<%# page import="java.sql.*" %>
String time=request.getParameter("time");
String url="jdbc:mysql://localhost:3306/pcs";
String uname="root";
String passw="publiccomplaint";
String query="insert into test values(?)";
Class.forName("com.mysql.jdbc.Driver");
Connection
con=DriverManager.getConnection(url,uname,passw);
PreparedStatement st=con.prepareStatement(query);
st.setString(1,time);
int count=st.executeUpdate();
st.close();
con.close();
What is the correct way in inserting the time into MYSQL DB from the input type="time" field of the form.?
You can try like this
String time = "15:30:18";
DateFormat sdf = new SimpleDateFormat("hh:mm:ss");
Date date = sdf.parse(time);
System.out.println("Time: " + sdf.format(date));
You should convert your date stored in string variable to java.util.date and then java.sql.date
String startDate="01-Feb-2013"; // Input String
SimpleDateFormat sdf1 = new SimpleDateFormat("dd-MM-yyyy"); // New Pattern
java.util.Date date = sdf1.parse(startDate); // Returns a Date format object with the pattern
java.sql.Date sqlStartDate = new java.sql.Date(date.getTime());
System.out.println(sqlStartDate); // Outputs : 2013-02-01
Then
preparedStatement.setDate(1,sqlStartDate);
You should convert the time string to a java.time.LocalTime and use setObject to pass that as the parameter:
String time = "20:20:00"; // sample data
st.setObject(1, java.time.LocalTime.parse(time));
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 am using Eclipse to do a college project on Java. I realized that java does not have a built in date selector like C#, so I downloaded and added JDateChooser. I tried to retrieve the chosen date but it failed:
String Date = dateChooser.getDate(); //I want to the date to be retrieved as string
Any ideas? Is there some kind of initialization that I must do?
Retrieve the date that the user selected by calling getDate(), which returns a Date object. Then convert that object into a String by calling SimpleDateFormat.format():
Date d;
SimpleDateFormat sdf;
String s;
d = dateChooser.getDate(); // Date selected by user
sdf = SimpleDateFormat("MM/dd/yyyy HH:mm:ss"); // Or whatever format you need
s = sdf.format(d); // Viola
See also: Question 5683728
If I have the right component, the Java Docs show that the getDate method returns Date
getDate
public java.util.Date getDate() Returns the date. If the
JDateChooser is started with a null date and no date was set by the
user, null is returned. Returns: the current date
String dob =new SimpleDateFormat("dd-MMM-yyyy").format(jDateChooser1.getDate());
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.