这是重复使用日期和时间方法的正确代码吗?因为我在三个活动中重复使用

发布于 2025-02-10 14:16:12 字数 672 浏览 1 评论 0原文

我已经创建了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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

别低头,皇冠会掉 2025-02-17 14:16:12

而不是每次都会创建DateTime的新实例,使您的这两种方法都是静态的。这样,您将能够直接访问它们,而无需创建DateTime类的新对象。这会对您的内存产生可忽略的影响,因为该类非常简单,但是这样做是一件好事,因此您可以在不填充内存的

public class DateTime {

   public static String date() {
       Calendar calendar = Calendar.getInstance();
       return calendar.get(Calendar.DATE)+ "/" + calendar.get(Calendar.MONTH) + "/" + calendar.get(Calendar.YEAR);
   }

   public static String time() {
       Calendar calendar = Calendar.getInstance();
       return calendar.get(Calendar.HOUR) + ":" + calendar.get(Calendar.MINUTE);
   }

}

情况下使用这些方法:

dbHelper.insertNote(note, DateTime.date(), DateTime.time(), System.currentTimeMillis());

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

public class DateTime {

   public static String date() {
       Calendar calendar = Calendar.getInstance();
       return calendar.get(Calendar.DATE)+ "/" + calendar.get(Calendar.MONTH) + "/" + calendar.get(Calendar.YEAR);
   }

   public static String time() {
       Calendar calendar = Calendar.getInstance();
       return calendar.get(Calendar.HOUR) + ":" + calendar.get(Calendar.MINUTE);
   }

}

to use:

dbHelper.insertNote(note, DateTime.date(), DateTime.time(), System.currentTimeMillis());
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文