I am trying to compare a SQL date with the current date.
I figured out how to compare two SQL dates but I couldn't extract the current date.
java.sql.Date xxx = new java.sql.Date(jdatechooser1.getDate().getTime());
java.sql.Date yyy = new java.sql.Date(jdatechooser2.getDate().getTime());
if (yyy.after(xxx)) {
System.out.println("ok");
}
Uses the system date instead: System.currentTimeMillis()
java.sql.Date dateToBeChecked= new java.sql.Date(jdatechooser1.getDate().getTime());
java.sql.Date systemDate = new java.sql.Date(System.currentTimeMillis());
if(dateToBeChecked.after(systemDate)){
System.out.println("ok");
}
But since it's long values you don't need to transform anything to an object and can do this instead:
if(jdatechooser1.getDate().getTime() > System.currentTimeMillis()){
System.out.println("ok");
}
At first you must create a java.util.Date object with empty constructor. Then give the long value which can be get by getTime() method, to the java.sql.Date constructor.
java.sql.Date date = new java.sql.Date(new java.util.Date().getTime());
Related
I want to convert java.util.Date to java.sql.Date but I want hours, minutes, and seconds as well but java.sql.Date can be used only to store date(no time) . I tried the below code but it is giving only year, month, and day for the java.sql.Date object.
SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
Date parsed = format.parse("20110210120534");
System.out.println(format.parse("20110210120534"));
java.sql.Date sql = new java.sql.Date(parsed.getTime());
System.out.println("SQL date is= "+sql);
Current output:
2011-02-10
Desired output:
2011-02-10 12:05:34
The java.sql.Date type is used to store only date (no time) information, as it maps to the SQL DATE type, which doesn't store time. What its toString() method does is:
Formats a date in the date escape format yyyy-mm-dd.
To achieve the desired output you can use java.sql.Timestamp, which stores date and time information, mapping to the SQL TIMESTAMP type. Its toString() method outputs what you need:
Formats a timestamp in JDBC timestamp escape format: yyyy-mm-dd hh:mm:ss.fffffffff, where ffffffffff indicates nanoseconds.
Example:
java.text.DateFormat format = new java.text.SimpleDateFormat("yyyyMMddHHmmss");
java.util.Date date = format.parse("20110210120534");
java.sql.Timestamp timestamp = new java.sql.Timestamp(date.getTime());
System.out.println(timestamp); // prints "2011-02-10 12:05:34.0"
As other folks said, you need to use java.sql.TimeStamp.
public class Test {
public static void main(String[] args) {
java.util.Date date = new java.util.Date();
java.sql.Timestamp sqlTimeStamp = new java.sql.Timestamp(date.getTime());
System.out.println("util-date:" + date);
System.out.println("sql-timestamp:" + sqlTimeStamp );
}
}
http://tutorials.jenkov.com/java-date-time/java-sql-date.html
This question already has answers here:
java.util.Date vs java.sql.Date
(7 answers)
Closed 5 years ago.
I am trying to insert Date into Database but I am getting error as java.util.Date cannot be cast to java.sql.Date.
Please help.
String next_dt = req.getParameter("NextDate");
DateFormat dtFmt = null;
dtFmt = new SimpleDateFormat("yyyy-MM-dd");
dtToday = (Date) dtFmt.parse(next_dt);
You have imported java.sql.Data. But dtFmt.parse(next_dt); returns an object of type java.util.Date so you have to change
import java.sql.Date;
to
import java.util.Date;
DateFormat.parse() returns a java.util.Date, and you're trying to illegally cast it to a java.sql.Date.
Assuming you continue to import java.sql.Date, you can successfully assign the variable like so:
dtToday = new Date(dtFmt.parse(next_dt).getTime());
You should use java.sql.Timestamp or java.sql.Date instead of java.util.Date
Problem with java.sql.Date is that it will not store time. So using Timestamp is the approach i always take. As it is the child class of java.util.date it is compatible with both date and timestamp columns in DB.
Add following lines - as it needs to be a sql Date and not util date
java.sql.Date sqlDate = new java.sql.Date(dtToday.getTime());
//now insert this sqlDate
public static java.sql.Date convertFromJAVADateToSQLDate(
java.util.Date javaDate) {
java.sql.Date sqlDate = null;
if (javaDate != null) {
sqlDate = new Date(javaDate.getTime());
}
return sqlDate;
}
As the name of the class is same use should give fully qualified name(FQN) of both classes you also can use format method to convert date to proper SQL format date.
public static String toMysqlDateStr(Date date) {
String dateForMySql = "";
if (date == null) {
dateForMySql = null;
} else {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
dateForMySql = sdf.format(date);
}
return dateForMySql;
}
I'm trying to create a variable called startDate using java.sql.Date.
I've tried...
java.sql.date startDate = "02/04/2015"
But it thinks it's a string.
java.sql.date startDate = 02/04/2015
But it thinks it's an int.
java.sql.date startDate = 02-04-2015
But it displays the error "invalid character constant".
How do I properly write this variable?
Thanks.
One possible approach is to use a SimpleDateFormat and the java.sql.Date(long) constructor like
DateFormat df = new SimpleDateFormat("MM-dd-yyyy");
java.sql.Date sqlDate = new java.sql.Date(df.parse("02-04-2015").getTime());
you could also try
java.sql.Date sqlDate = new java.sql.Date(2020,12,12);
I do not know why this is deprecated, but it is very intuitive for me.
basically year, month, day integer
This question already has answers here:
Converting ISO 8601-compliant String to java.util.Date
(31 answers)
Closed 8 years ago.
For example, I have this String: 06/10/2013 18:29:09. I want to convert this string and put it in a SQL database (as Date).
How can I convert this to an sql date, so that it could be inserted into a database? I want the hours minutes and seconds to remain as well.
I tried the following:
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
Date javaDate = sdf.parse("06/10/2013 18:29:09");
java.sql.Date date = new java.sql.Date(javaDate.getTime());
The problem is here:
java.sql.Date date = new java.sql.Date(javaDate.getTime());
java.sql.Date stores the date part of your timestamp. If you want/need to handle both date and time, use java.sql.Timestamp instead:
java.sql.Timestamp date = new java.sql.Timestamp (javaDate.getTime());
More info:
Date vs TimeStamp vs calendar?
You will use a SimpleDateFormat object to parse the string to java.util.date and then use the getTime() method to instantiate a java.sql.Date.
String input = "06/10/2013 18:29:09";
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss");
java.util.Date dt = sdf.parse(input);
java.sql.Date dtSql = new java.sql.Date(dt.getTime());
If you are working with String type for date input and then you want to save that in a database like MySQL, you should use an appropriate Date Format for it. There's a class called "SimpleDateFormat" which you can use for that purpose. You can find a sample in the following link, also a brief explanation on how it works. Hope it helps.
Example: http://www.java2s.com/Tutorial/Java/0040__Data-Type/SimpleDateFormat.htm
Best Regards.
Here's a simple demo. In a Database table like this.
You can insert into it like this.
//the SQL statement for creating the database table
create table user(id, integer primary key, username varchar(100), date_created varchar(100));
//the java code to insert into the table created above.
try{
String date = new SimpleDateFormat("dd/MM/YYYY HH:mm:ss").format(new Date());
String sql = "insert into user(username, date_created) values('olupotd', '"+date+"')";
int done = statement.executeUpdate(sql);
if(done > 0)
//inserted
else
//not inserted.
}catch(java.sql.SQLException e){}
Hope that helps
I use Grails 2.0.3
and am comparing a a date string post value using creatCriteria and i have used below methods to convert the string into date but it always empty ? when i try , i can use params.date() method since i pass only one string ,for searching it can be date in one time ,or it can be number and so on ...
println Date.parse('2013-02-05')
my creatCriteria
def vDate = new Date().parse(query.toString())
{
eq('dateCreated',vDate)
}
what am missing or any alternative ? i still believe that this trivial issue has to be resolved enough for future use?
First of all convert your String to date using this method.
def date = Date.parse('yyyy-MM-dd','2013-02-05')
Now use today start and today end method.
Date getTodayStart( Date inDate){
Calendar cal = Calendar.getInstance()
cal.set(inDate[Calendar.YEAR], inDate[Calendar.MONTH], inDate[Calendar.DATE], 0, 0, 0)
Date todayStart = cal.getTime()
return todayStart
}
Date getTodayEnd(Date inDate){
Calendar cal = Calendar.getInstance()
cal.set(inDate[Calendar.YEAR], inDate[Calendar.MONTH], inDate[Calendar.DATE], 0, 0, -1)
Date todayEnd = cal.getTime() + 1
return todayEnd
}
It will give you object from day start to day end.
Date startDate = getTodayStart(date)
Date endDate = getTodayEnd(date)
Now you can create criteria for date.
{
between('dateCreated',startDate.toString(),endDate.toString())
}
It will give you all date for that day.
you can use
Date vDate = new java.text.SimpleDateFormat("yyyy-MM-dd").parse(query.toString())
If your database column is of type Date then you should not have problem using the way #Kamil has suggested. Although, there is a groovier way of parsing the date string to date.
def date = Date.parse('yyyy-MM-dd','2013-02-05')
assert date instanceof java.util.Date