Reputation: 2199
In my simple application, I am storing the timestamp into the database table programmatically by using new Timestamp(System.currentTimeMillis())
.
Now, I have one condition, where I have to store the onClick count for a particular day, and all onClick counts into two different columns.
So for that, I need to find out onClicks for today. So how can I find out if the time onclicks occur are today? I mean for say 1st August, I want all the onclick counts in one column and all previous onClick counts into another column.
*In simple words - storing clicks for today in one column and storing clicks till now in another column, so if today expires, I want to add clicks for today with total clicks and then make the today_clicks column count to 0 and then when there is new click on next day, store it in the today_clicks column by starting it with count 1*
How can I decide that? Which Java class should I use for that?
Upvotes: 2
Views: 1823
Reputation: 347214
First, I'd suggest taking a look at Joda Time.
Second, I'd take a look at Calendar
:
Calendar lower = Calendar.getInstance();
lower.setTime(new Date());
lower.set(Calendar.HOUR_OF_DAY, 0);
lower.set(Calendar.MINUTE, 0);
lower.set(Calendar.SECOND, 0);
lower.set(Calendar.MILLISECOND, 0);
Calendar upper = Calendar.getInstance();
upper.setTime(new Date());
upper.set(Calendar.HOUR_OF_DAY, 23);
upper.set(Calendar.MINUTE, 59);
upper.set(Calendar.SECOND, 59);
upper.set(Calendar.MILLISECOND, 99);
Date clickTime = new Date(timeInMillis);
if (clickTime.after(lower.getTime()) && clickTime.before(upper.getTime())) {
// Is today
}
Upvotes: 0
Reputation: 39485
If I understand correctly, you are looking for a way to best store your data in your database. You have decided to maintain a clicks-for-today column and a clicks-until-now column. (I may be off base on this, but it wasnt completely clear from the question)
I would suggest that you dont try to manipulate the data from column to column as you have described, but instead do so by maintaining click counts per day:
date | count ------|------- Jul 31| 10876 Aug 1| 15721
(where date is a in the db timestamp/date datatype)
How would this work?
At startup, you could then bring into memory all rows, and sum their counts to get a clicks-until-now value, if needed.
You then have special handling for today's row, where you update the appropriate context with the clicks-for-today value.
To determine whether you've passed a day threshold, you could simply:
todayCal
)clickCal
)todayCal
and clickCal
Upvotes: 0
Reputation: 21223
Not sure if that is what are you after. To get start of next day you can use Calendar:
public static Date nextDayStart(Date date) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
cal.add(Calendar.DATE, 1);
return cal.getTime();
}
To get tomorrow:
Date tomorrow = nextDayStart(new Date());
To get Timestamp
from Date
:
Timestamp s = new Timestamp(date.getTime());
Upvotes: 4