这是重复使用日期和时间方法的正确代码吗?因为我在三个活动中重复使用
我已经创建了DateTime.java类:
public class DateTime {
public String date() {
Calendar calendar = Calendar.getInstance();
return calendar.get(Calendar.DATE)+ "/" + calendar.get(Calendar.MONTH) + "/" + calendar.get(Calendar.YEAR);
}
public String time() {
Calendar calendar = Calendar.getInstance();
return calendar.get(Calendar.HOUR) + ":" + calendar.get(Calendar.MINUTE);
}
}
在所有三个活动中,我都会得到这样的日期和时间:
DateTime dateTime = new DateTime();
dbHelper.insertNote(note, dateTime.date(), dateTime.time(), System.currentTimeMillis());
finish();
DateTime类实现有任何问题吗?
I have created DateTime.java class like this:
public class DateTime {
public String date() {
Calendar calendar = Calendar.getInstance();
return calendar.get(Calendar.DATE)+ "/" + calendar.get(Calendar.MONTH) + "/" + calendar.get(Calendar.YEAR);
}
public String time() {
Calendar calendar = Calendar.getInstance();
return calendar.get(Calendar.HOUR) + ":" + calendar.get(Calendar.MINUTE);
}
}
in all three activities I get the date and time like this:
DateTime dateTime = new DateTime();
dbHelper.insertNote(note, dateTime.date(), dateTime.time(), System.currentTimeMillis());
finish();
Is there any problem with the DateTime class implementation?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
而不是每次都会创建DateTime的新实例,使您的这两种方法都是静态的。这样,您将能够直接访问它们,而无需创建DateTime类的新对象。这会对您的内存产生可忽略的影响,因为该类非常简单,但是这样做是一件好事,因此您可以在不填充内存的
情况下使用这些方法:
Instead of creating a new instance of DateTime everytime make your both methods as static. In this way, you will be able to access them directly without creating a new object of DateTime class. This will have negligible effect on your memory because the class is very simple, but it is a good thing to do so that you can use these methods without filling your memory
to use: