简体   繁体   中英

How can i separate instances of a class on behalf of their time of creation using java?

Suppose i have 100 instances of a class. I want to make arrays of those instances on behalf of their time of creation. For example there are 40 object which are created in september-2011 and 60 objects which are created in october-2011. Every instance has its time of creation which has type long. How can i tell my java program to make array of all instances which are created in september and another array which contains all instances which are created in october. I created time of creation using this line of code:

 Date currentDate = new Date(); 
 long timeOfCreation = currentDate.getTime();

Thanks in advance.

Well, you iterate over the instances, check their creation time and put them into the according list (I'd not use an array here, you could later convert the lists to arrays if you need to).

Basically, it's just a matter of getting the month from the date (you can create a Date object from the timestamp) which should be doable using Calendar (or yet better: use Joda Time ).

If the intervals are non-standard (eg from 15th to 15th) you might need a start and end value to compare against.

Edit :

If you store those timestamps in your database, you could just create a query to get all the instances between start and end date of each interval ( ... WHERE timeOfCreation BETWEEN <start> and <end> , note that <start> and <end> are just placeholders for your parameters). Then call that query for every interval you are interested in, eg start = September 1st 00:00:00,000 and end = September 30th 23:59:59,999 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM