[英]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. 假设我有一个类的100个实例。 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.
例如,在2011年9月创建的对象有40个,在2011年10月创建的对象有60个。 Every instance has its time of creation which has type long.
每个实例的创建时间均为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.
我怎么能告诉我的Java程序对所有在9月创建的实例进行数组,而另一个对包含在10月创建的所有实例进行数组。 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 ). 基本上,这只是从日期中获取月份的问题(您可以从时间戳中创建
Date
对象),该Date
应该可以使用Calendar
(或者更好的是,使用Joda Time )来实现。
If the intervals are non-standard (eg from 15th to 15th) you might need a start and end value to compare against. 如果间隔是非标准的(例如,从15日到15日),则可能需要一个开始值和结束值进行比较。
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). 如果将这些时间戳存储在数据库中,则可以创建一个查询来获取每个时间间隔的开始和结束日期之间的所有实例(
... WHERE timeOfCreation BETWEEN <start> and <end>
,请注意, <start>
和<end>
只是参数的占位符)。 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
. 然后针对您感兴趣的每个时间间隔调用该查询,例如
start = September 1st 00:00:00,000
and end = September 30th 23:59:59,999
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.