I have 50000 XML records like this in the database:
<?xml version="1.0" encoding="UTF-8"?>
<root>
<urlMD5>11ca7070ad6eb3180c53281e7b597976</urlMD5>
<date>
<year>2011</year>
<month>8</month>
<day>6</day>
<h>19</h>
<m>26</m>
<s>40</s>
</date>
<enc>utf-8</enc>
</root>
as you see, I saved the date in separated format.
Now I need to extract the currentTimeMillis() of these days and save to database in new field.
So I can easily compare them with current currentTimeMillis() and find out which document have been stored in last two days.
for example:
int ct = currentTimeMillis();
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/crawler?" + "user=root&password=&useUnicode=true&characterEncoding=UTF-8");
Statement stat = conn.createStatement();
ResultSet rs = st.executeQuery("SELECT * FROM DBName WHERE ct - 60 * 60 * 24 * 2 > timeField");
And because I want to use this field for so many times, I want to use a simple query and I don’t want to extract the XML format each time.
The problem is currentTimeMillis() function calculate currentTimeMillis that passed from January 1, 1970 and I need to calculate the same thing with my data.
I have written following code with the same code that I used before to store XML data to make sure if I can do it or not:
int s = 0;
s += (Calendar.getInstance().get(Calendar.YEAR) - 1970) * 60 * 60 * 24 * 365;
s += (Calendar.getInstance().get(Calendar.MONTH)) * 60 * 60 * 24 * 31;
s += (Calendar.getInstance().get(Calendar.DAY_OF_MONTH) * 60 * 60 * 24);
s += (Calendar.getInstance().get(Calendar.HOUR_OF_DAY) * 60 * 60);
s += (Calendar.getInstance().get(Calendar.MINUTE) * 60);
s += (Calendar.getInstance().get(Calendar.SECOND));
System.out.println(s);
System.out.println(System.currentTimeMillis() / 1000);
but the results are not the same:
1312313680
1312643080
One of the reasons is because not every month have 31 days.
Any way, I need help to find out how can I calculate the currentTimeMillis with special date, not the current date?
I should mention that I am using mysql, so maybe I can save the dates in special format that I can compare the with SQL query?
Use the set method of Calendar:
etc.
And then extract the number of milliseconds with
c.getTime().getTime().