简体   繁体   English

如何使用Java代表类的实例创建类的实例?

[英]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.

相关问题 如何在单独的文件中使用类编译java文件 - How I can compile a java file using a class in a separate file 如何在JAVA中一次传递一个类的两个单独实例来对该类中的某些数据进行排序 - How to pass two separate instances of a class at a time in JAVA to sort some data in that classes 如何在Java中使用单独的枚举类引用静态类? - How can I reference a static class with a separate enum class in Java? 如何在一条语句中打印多个类实例? 爪哇 - How can I print multiple class instances in one statement? Java 如何将我的 ArrayList 与不同的 java class 分开? - How can I separate my ArrayList to different java class? 如何使用 json 文件将 class 的实例添加到列表中 - How can I make instances of a class into a list using a json file 单独类实例的碰撞检测-Java处理 - Collision detection for separate class instances - Processing Java Java:如何在创建 object 时使用 class 的对象初始化数组? - Java: How do I initialize an array with objects of a class at the time of object creation? 如何使用单独的jar覆盖类? - How can I override a class using a separate jar? 如何在内部 class 上创建 2 个单独的实例作为外部 class 的单独属性,这是一个 JPA 实体? - How do I create 2 separate instances on an inner class as separate properties of the outer class which is a JPA entity?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM