Reputation: 139
I'm trying to measure time while a user plays a game and I have to store that playing time to database. (I will use it later to make a rank.)
I've heard that I'd better use timestamp if the data I want to store is about time.
But after I searched, I could only see the examples that converting 'date'&time to timestamp. I only need to store playing time without date, so I'm wondering if it's possible.
If it is, please give me some examples too. Thank you.
Upvotes: 0
Views: 116
Reputation: 7989
As a commenter said, I strongly recommend using date & time. However, if you really want / need to just use time:
Calendar cal = Calendar.getInstance();
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
long timestamp = cal.getTimeInMillis() - System.currentTimeMillis();
This creates a calendar, sets it to last midnight, and get the number of milliseconds since then.
Upvotes: 1