How to match system date with the date from database mysql - java

I have a database named work created using mysql in which a table named register is created. It contain a column renewdate. I accessed the table from java swing like this
conn=ConnectionDB.conn();
Date d=sysdate();
String sql="select renewdate from register";
try
{
ps=conn.prepareStatement(sql);
ResultSet rs=ps.executeQuery();
Date dates;
while(rs.next())
{
dates = rs.getDate("renewdate");
System.out.println(dates);
if(dates==d)
{
SendMailTLS.sendMail();
}
}
}
My problem is to send an email when renewdate equals system date. I generate current date using function sysdate() and assigned it to d. Also assigned renewdate in table to dates using dates = rs.getDate("renewdate").
My problem is I can not match up both d and dates and thus can not sent email. Could you help me how to match d and dates.
I tried while(rs.next()) all the dates from table register is obtained. But can not match with d using if(dates==d). Also I tried if(rs.next()), but it only fetch first renewdate from table. So how could i check all the values of renewdate and match with the current date to sent message

Just change a query string to
// NOW() in mysql will get the current date and time. So here you get only renewdates matched with current date and time
String sql="select renewdate from register where renewdate = NOW()";
// If you want to compare only with date not time, then go with
String sql="select renewdate from register where renewdate = CURDATE()";
There is no need for this if(dates == d) in your above code. Send mail as soon you have next element in your while condition.

Nice code! So:
Save the value of date into strings like:
Date d=sysdate();
String strDate1 = d.toString();
String strDate2 = rs.getDate("renewdate").toString;
Then compare it like:
strDate1.equals(strDate2);
as Strings contain references to "words", not the "words" themself, the == opereator would do something else;
Couly you please send us your whole code for this app anyway, as it would be a nice peace of work to learn from?

If you want to do it in code, here is a way:-
Convert date fetched from database into java.util.Date object using below code (assuming date is stored in GMT):
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
formatter.setTimeZone(TimeZone.getTimeZone("GMT"));
Date dateInDB = formatter.parse(dates);
Then, you can simply use java.util.Date equals method to compare d with dateInDB.
If you want to ignore time portion while comparing two dates, call below method (that sets time portion to 00:00:00) for each date and use returned date in equals method:
public static Date getZeroTimeDate(Date dateWithTime) {
Date res = dateWithTime;
Calendar calendar = Calendar.getInstance();
calendar.setTime( dateWithTime );
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
res = calendar.getTime();
return res;
}
This way it does not uses any deprecated method too.
Hope this helps!

Related

how to insert data type date(yyyy-MM-dd) in sqlite database and retrive data between two dates in android

I have a Sqlite3 database table contains name,address,date of birth details.i want to display 1990-01-01 to 1995-01-01 details.
but Sqlite3 database stores only following data types.
TEXT
NUMERIC
INTEGER
REAL
NONE
Any one have some hint to store and retrieve date format data..?
From my own experience on doing several projects with database in Android my answer is:
Do not store the date as a string. Never! Ever! Store them as Unix timestamps and format them as needed during runtime.
the important thing here is to separate what is your data and what is the on-screen representation of your data. Storing in a database the on-screen representation of your data is wrong.
You'll always store your dates as INTEGER types.
So for example to store the date now you'll store the value System.currentTimeInMilis
To select between 1990-01-01 and 1995-01-01 you will:
long val1 = new GregorianCalendar(1990, 01, 01).getTimeInMillis();
long val2 = new GregorianCalendar(1995, 01, 01).getTimeInMillis();
and then you'll do the normal SELECT statement between those 2 values.
to show those values in the screen as yyyy-MM-dd you'll use the SimpleDateFormat class:
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
long longDate = cursor.getLong(colNumber); // from the database
String stringDate = dateFormat.format(new Date(longDate));
Use this code to convert your date into millisecond format and store it into your database as INTEGER types
String someDate = "1995-01-01";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date date = sdf.parse(someDate);
System.out.println(date.getTime());
date.getTime()-give the millisecond format
At the same way to convert your input (i.e from 1990-01-01 and to date 1995-01-01)
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date date1 = sdf.parse(1990-01-01);
value1=date.getTime();
Date date2 = sdf.parse(1995-01-01);
value2=date.getTime();
Retrieve from database using following query
db.rawQuery("SELECT * FROM table_name WHERE column_name BETWEEN "+value1+" AND "+value2+"",null);
or
db.rawQuery("SELECT * FROM table_name WHERE column_name<="+value1+" AND column_name>="+value2+"",null);
You can do something like this
DateFormat df=new SimpleDateFormat("yyyy-MM-dd");
Date date1=df.parse("1990-01-01");
Date date2=df.parse("1995-01-01");
Date myDate=df.parse("1992-01-01"); // checking date
if((date1.getTime()<myDate.getTime())&&(myDate.getTime()<date2.getTime())){
System.out.println(df.format(myDate)+" is in this range");
}else{
System.out.println(df.format(myDate)+" is not in this range");
}
Since the format you want to use (yyyy-MM-dd) is ordered in the same way as a String (i.e. for any dates x and y you would choose, if x < y as a Date, then x < y as a String), you can simply store the dates as Strings (TEXT) in your database.
When selecting the values between them, you would just have to use a WHERE clause in your SELECT statement like this:
SELECT * FROM yourTable WHERE yourDateFieldName > ? and yourDateFieldName < ?
You can then use DateFormat.format to set the values for the ? parameters of your prepared statement. The first parameter would be the "start" date, and the second would be the "end" date. You can replace < with <= and > with >= if you want the items on start and end dates included.
This gives you a String representation of a Date. To convert from that to an actual Date object you can use date formatter's parse method (i.e. SimpleDateFormat.parse).
Another, "cleaner", approach would be to use the SQLite date and time functions (see here). While SQLite doesn't have a DATE type for storing date values, it has helper functions that you can use to interpret TEXT and NUMBER values as date in your statements.
If you don't need extra processing for your date values, I'd recommend going for the first solution as it should be faster because it merely compares TEXTs rather than parsing and extracting a date from them, then comparing the extracted date (I haven't compared the speed of the two approaches, so don't take my word for it on this one). This approach also has less code to write and maintain and the code is easier to read.
Sources:
SQLite data type - for the validity of comparing two TEXT values
SimpleDateFormat - Android documentation
You can use dates in yyyy-MM-dd format directly, JDBC will understand it. Assuming we a have a table t1 with c1 of DATE type
PreparedStatement ps = conn.prepareStatement("insert into t1 (c1) values (?)");
ps.setString(1, "2001-01-01");
ps.executeUpdate();
Reading dates is simple too
ResultSet rs = st.executeQuery("select c1 from t1");
rs.next();
Date date = rs.getDate(1);
ResultSet.getDate returns result as java.sql.Date whose toString method returns date in yyyy-MM-dd format

Java Date Format

I have a table in MySQL server called Caller_List. In this table I have a single date column called call_date whose data type is Date. I have created a web page in which I have a SELECT Box for from_day (where all days 1 to 31 are stored), a SELECT box for month (where all month names from January to December are stored), a SELECT box for year (where all years from 2000 to 2012 are stored. Like I also have a SELECT box for to_day, to_month and to_year.
The problem is when I fetch these day,month and year from java servlet using request.getParameter() method, it is fetched as string data type and stored in variable called from_date and to_date. I concatenate from_day,from_month and from_year and store in the variable called from_date. Also I concatenate to_day,to_month and to_year and store in to_date. I concatenate them in the format year-month-day since MySQL understands this format.
I then pass the following query to retrieve data between these two from_date and two date:
select caller_name,call_date
from Caller_List
where call_date>='"+from_date+"' and call_date<='"+to_date+"'
I also tried the following query but in vain:
select caller_name,call_date
from Caller_List
where call_date between '"+from_date+"' and '"+to_date+"'
I also came to know that I need to convert from_date and to_date to date format before executing the query. But I am a novice in java, I do not know how to do it. Also I want the date in the format year-month-date. I do want to display time with date, please please help me!
If you only want to know how to parse a String to a Date in java, you could simplely use the SimpleDateFormat class.
final DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
try {
final Date fromDate = dateFormat.parse(from_date);
} catch (ParseException e) {
// ...
}
Assume that your from_date string is looks like 2012-09-16.
And if you want to display a date with time. You can also use the SimpleDateFormat.
final DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
try {
final String displayDate = dateFormat.format(new Date());
} catch (ParseException e) {
// ...
}
The displayDate should looks like 2012-09-16 20:13:25.
One more thing, you could compare a string type and a date type in MySQL. Just make sure your date string has the pattern "yyyy-mm-dd hh:mm:ss". Of course, you should use the preparedStatement.

insert current time into table

java.util.Date date = new java.util.Date();
java.sql.Date today = new java.sql.Date(date.getTime()); //2012-03-23
java.sql.Time time = new java.sql.Time(date.getTime()); //02:32:46
PreparedStatement pst = null;
String queryString = "INSERT INTO PR_VISITOR(PRISONER_ID,VISITOR_NAME,FATHER_NAME,DOV,IN_TIME) VALUES (?,?,?,?,?)";
pst = connect.prepareStatement(queryString);
pst.setString(1, pr_id);
pst.setString(2, visit);
pst.setString(3, father);
pst.setDate(4, today);
pst.setTime(5, time);
int officerQuery = pst.executeUpdate();
if (officerQuery == 1) {
response.sendRedirect("/FYP3.4/prisonAdmin/visitor_out.jsp");
JOptionPane.showMessageDialog(null, "Visitor information registered !!", "Visitor Information", JOptionPane.INFORMATION_MESSAGE);
} else {
JOptionPane.showMessageDialog(null, "Unable to Add information !!", "Visitor Information", JOptionPane.ERROR_MESSAGE);
}
By using the above code i'm trying to insert the current date and time into table,which have the separate columns. When i'm executing the above query then it insert the todays date in the time IN_TIME field too.
EDIT
DATATYPE OF IN_TIME and DOV are DATE .
Need Help.. !!
Since DOV and IN_TIME is date you don't need to separate date and hour. The type date in Oracle holds date and time. I suggest you change your table to have just one date column.
To insert the current time you can use the Oracle's sysdate function:
INSERT INTO PR_VISITOR(PRISONER_ID,VISITOR_NAME,FATHER_NAME,DATETIME_COLUMN) VALUES (?,?,?,?,SYSDATE)
To format your output of the date value you can use the SimpleDateFormat class in Java or to_char in Oracle.
A DATE column in an Oracle database will always store both a day (i.e. March 22, 2012) and a time to the second (i.e. 3:30:00 PM). A java.sql.Date and a java.sql.Time store the day and time as well but to the millisecond.
It doesn't really make sense to have separate columns in Oracle for the day and for the time but particularly not where both columns are declared as DATE data types. It would be much more conventional to use a single column declared as a DATE. If you really wanted to, you could truncate the day so that it represents midnight on the current day and then store the time component as, say, an INTERVAL DAY TO SECOND. But that would generally add a fair amount of complexity to the system for very little gain.
You're much better off using oracle's 'systimestamp'. The reason being, if you're java code is running in one timezone, and oracle lives in another. Forcing your own Time object, could cause problems.
Do you really need separate fields for this? I would think just having a timestamp would be enough.
Use SimpleDateFormat. This is one way I have used it:
Date now = Calendar.getInstance().getTime());
DateFormat df = new SimpleDateFormat("yyyyMMdd");
String date = df.format(now);
DateFormat tf = new SimpleDateFormat("HHmmss");
String time = tf.format(now);
Follow this,it will help both in java and oracle
create table datetime(date_time timestamp);
insert into datetime values (sysdate);
To get date:
select to_char(date_time,'DD-MON-YY') from datetime;
eg:12-JUL-12
To get month:
select to_char(date_time,'mm') from datetime;
eg:7
To get time:
select to_char(date_time,'HH24:MI:SS') from datetime;
eg:23:56:15
cheers!!

different date format in java after fetching from oracle

In my db max date is as : 27-FEB-12
when i am fetching data by java from db that is:
select to_char(max(CREATE_DT),'dd-mm-yyyy') from PROFILE_DETAILS;
gives me 2012-02-27 00:00:00.0
How can i convert it to: 27-FEB-12( i am trying to use indian date format)
Any idea please
I don't know why you need to_char function in your query. If you are fetching data by jdbc, oracle could give you Date object. It is in your case much easier to convert into different format (String) in future.
anyway based on your current requirement, with to_char, you get a String 2012-02-27 00:00:00.0. now you want to get another string 27-FEB-12. you could do something like below(exception handling was omitted):
final String s = "2012-02-27 00:00:00.0";
String newDateString = new SimpleDateFormat("dd-MMM-yy").format(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S").parse(s));
this will give you 27-Feb-12
String strDate = "2012-02-27 00:00:00.0";
String TimeZoneIds = TimeZone.getDefault().getID();
SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss.S");
SimpleDateFormat sdf2 = new SimpleDateFormat("dd-MMM-yyyy");
sdf2.setTimeZone(TimeZone.getTimeZone(TimeZoneIds));
try {
Date date = sdf1.parse(strDate);
String strFinalDate = sdf2.format(date);
System.out.println(strFinalDate);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
In an Oracle DATE column there is no format; the string representation you see when you select max(create_dt)) from profile_details in SQL*Plus, say, is using an implicit format mask from your NLS settings, which appears to be DD-MON-RR in that client.
You JDBC call is applying an explicit format mask, which is the right thing to do if you want Java to treat it as a String, not least because it may have different NLS settings. But your mask doesn't match what you say you want; you're specifying DD-MM-YYYY when you want DD-MON-RR.
But it also looks like you're probably retrieving the value from the JDBC call with a getDate() call, and it's being implicitly cast back to a Java Date object type. If you want to treat it as a Date in Java, then you don't need the to_char in your select, and you need to use Java tools (e.g. SimpleDateFormat as #Andrew Logvinov suggests) to turn it into a String as needed. If you're only ever treating it as a String - for immediate display, say - then use getString() instead, and fix your date format mask in the query.
Edit
If you retrieve the value from JDBC with getDate() and want to see the value as a String in the format you specified, you need to do something like:
Date raw_date;
String string_date;
SimpleDateFormat sdf = new SimpleDateFormat("dd-MMM-yy");
raw_date = <resultSet>.getDate(1);
string_date = sdf.format(raw_date);
select to_char(max(CREATE_DT),'dd-MON-yy') from PROFILE_DETAILS;

Hibernate Criteria for Dates

In oracle I have dates in format
17-April-2011 19:20:23.707000000
I would like to retrieve all orders for 17-04-2011.
SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-YYYY");
String myDate = "17-04-2011";
Date date = formatter.parse(myDate);
Criteria criteria =
session.createCriteria(Order.class);
Criterion restrictDate = Restrictions.like("orderDate",date);
but it brings me empty result:
Why do you use Restrictions.like(...)?
You should use Restrictions.eq(...).
Note you can also use .le, .lt, .ge, .gt on date objects as comparison operators. LIKE operator is not appropriate for this case since LIKE is useful when you want to match results according to partial content of a column.
Please see http://www.sql-tutorial.net/SQL-LIKE.asp for the reference.
For example if you have a name column with some people's full name, you can do where name like 'robert %' so that you will return all entries with name starting with 'robert ' (% can replace any character).
In your case you know the full content of the date you're trying to match so you shouldn't use LIKE but equality. I guess Hibernate doesn't give you any exception in this case, but anyway you will probably have the same problem with the Restrictions.eq(...).
Your date object you got with the code:
SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-YYYY");
String myDate = "17-04-2011";
Date date = formatter.parse(myDate);
This date object is equals to the 17-04-2011 at 0h, 0 minutes, 0 seconds and 0 nanoseconds.
This means that your entries in database must have exactly that date. What i mean is that if your database entry has a date "17-April-2011 19:20:23.707000000", then it won't be retrieved because you just ask for that date: "17-April-2011 00:00:00.0000000000".
If you want to retrieve all entries of your database from a given day, you will have to use the following code:
SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-YYYY");
String myDate = "17-04-2011";
// Create date 17-04-2011 - 00h00
Date minDate = formatter.parse(myDate);
// Create date 18-04-2011 - 00h00
// -> We take the 1st date and add it 1 day in millisecond thanks to a useful and not so known class
Date maxDate = new Date(minDate.getTime() + TimeUnit.DAYS.toMillis(1));
Conjunction and = Restrictions.conjunction();
// The order date must be >= 17-04-2011 - 00h00
and.add( Restrictions.ge("orderDate", minDate) );
// And the order date must be < 18-04-2011 - 00h00
and.add( Restrictions.lt("orderDate", maxDate) );
By using this way you can get the list of selected records.
GregorianCalendar gregorianCalendar = new GregorianCalendar();
Criteria cri = session.createCriteria(ProjectActivities.class);
cri.add(Restrictions.ge("EffectiveFrom", gregorianCalendar.getTime()));
List list = cri.list();
All the Records will be generated into list which are greater than or equal to '08-Oct-2012' or else pass the date of user acceptance date at 2nd parameter of Restrictions (gregorianCalendar.getTime()) of criteria to get the records.
If the column is a timestamp you can do the following:
if(fromDate!=null){
criteria.add(Restrictions.sqlRestriction("TRUNC(COLUMN) >= TO_DATE('" + dataFrom + "','dd/mm/yyyy')"));
}
if(toDate!=null){
criteria.add(Restrictions.sqlRestriction("TRUNC(COLUMN) <= TO_DATE('" + dataTo + "','dd/mm/yyyy')"));
}
resultDB = criteria.list();
try this,
String dateStr = "17-April-2011 19:20:23.707000000 ";
Date dateForm = new SimpleDateFormat("dd-MMMM-yyyy HH:mm:ss").parse(dateStr);
SimpleDateFormat format = new SimpleDateFormat("dd-MM-yyyy");
String newDate = format.format(dateForm);
Calendar today = Calendar.getInstance();
Date fromDate = format.parse(newDate);
today.setTime(fromDate);
today.add(Calendar.DAY_OF_YEAR, 1);
Date toDate= new SimpleDateFormat("dd-MM-yyyy").parse(format.format(today.getTime()));
Criteria crit = sessionFactory.getCurrentSession().createCriteria(Model.class);
crit.add(Restrictions.ge("dateFieldName", fromDate));
crit.add(Restrictions.le("dateFieldName", toDate));
return crit.list();

Categories