时间与日期VS日历的java [英] time vs date vs calendar java

查看:123
本文介绍了时间与日期VS日历的java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

那么在我的应用我想获得当前的日期和月份abnd本地小时(currentTimeZone小时),并根据其值显示一些东西。

我已看过一些sollutions和我已经使用了以下内容:

 日历C = Calendar.getInstance();
            INT天= c.get(Calendar.DATE);
            INT月= c.get(的Calendar.MONTH);

或本

 时间今天=新时代(Time.getCurrentTimezone());
            today.setToNow();
            INT DAY2 = today.monthDay;
            INT MONTH2 = today.month;

我的问题是:

1),其中,这更是稳定考虑他们的结果?我的意思是,它不会有什么问题。

2)此外,没有他们都考虑到时区?我认为,第二个呢,但什么日历?

3)我想这code,我发现:

 的SimpleDateFormat S =新的SimpleDateFormat(ddMMyyyyhhmmss);
            字符串格式= s.format(新的Date());

和我得到的日期()必须有一个参数错误。奇怪,因为在每一个答案,他们使用这样的。

什么是错的?

4)你有什么建议我用?我唯一​​希望的是一些存储在内存中当前的月,日,小时之间进行简单的比较。 (例如,如果它的1月29日显示的事件。如果它是19.00上午发送通知等等)。


解决方案

  

1)的这是其中比较稳定考虑他们的结果?我是说
  它不会有任何问题


他们都是稳定的,很好的测试,尤其是日历这是自JDK1.1了Java的API中。同样来自文档 时间


  

要Calendar和GregorianCalendar的类的替代品。一个
  Time类重$ P $实例psents一时的,具有指定
  第二precision。它结构TM仿照,而事实上,使用
  结构以旧换新实施的大部分功能。


时间,恕我直言,更容易比日历不过,工作


  

2)的此外,没有他们都考虑到时区?一世
  认为第二个呢,但什么日历?


是的,他们做的。您可以指定时区使用<一个一个href=\"http://docs.oracle.com/javase/7/docs/api/java/util/Calendar.html#getInstance%28java.util.TimeZone%29\"><$c$c>Calendar.getInstance(java.util.TimeZone).否则,一个默认的将被使用。


  

3)的这个我试过code,我发现:


在code的作品就好。请确保您使用的是新java.util.Date()而不是 java.sql.Date ,例如。


  

4)的你有什么建议我用?我唯一​​希望的是一个简单的
  当前月,日,小时之间的比较与一些存储在存储器中。
  (例如,如果它的1月29日显示的事件。如果它是19.00上午
  发送通知等。)


这是真正的个人选择的问题。使用日历,例如,比较如果今天是1月29日,你可以简单地使用下列内容:

 今天日历= Calendar.getInstance();
INT请将dayOfMonth = today.get(Calendar.DAY_OF_MONTH);
INT月= today.get(的Calendar.MONTH);
如果(月== Calendar.JANUARY&放大器;&安培;请将dayOfMonth == 29){
  // 1月29日
}

Well in my app I want to get current date and month abnd local hour (currentTimeZone hour) and depending on its value display some things.

i have read several sollutions and I have used the following:

Calendar c = Calendar.getInstance(); 
            int day=c.get(Calendar.DATE);
            int month=c.get(Calendar.MONTH);

or this

Time today = new Time(Time.getCurrentTimezone());
            today.setToNow();
            int day2=today.monthDay;
            int month2=today.month;

my questions are:

1)which of this is more "Stable" considering their results? I mean that it will not have any problem.

2)Moreover, did they both take into consideration the TimeZone? I think that the second one does, but what about the calendar?

3)I tried this code that I have found:

SimpleDateFormat s = new SimpleDateFormat("ddMMyyyyhhmmss");
            String format = s.format(new Date());

and i get the error that Date() must have an argument. weird because in every answer they use it like that.

What is wrong?

4) What do you suggest me to use? The only thing I want is a simple comparison between current month,date,hour with some stored in memory. (for example if its January 29, display that event. if it is 19.00 am send notification and so on.)

解决方案

1) Which of this is more "Stable" considering their results? I mean that it will not have any problem.

They are both stable and very well tested, especially Calendar which is in the Java API since JDK1.1. Also from the docs of Time:

An alternative to the Calendar and GregorianCalendar classes. An instance of the Time class represents a moment in time, specified with second precision. It is modelled after struct tm, and in fact, uses struct tm to implement most of the functionality.

Time, imho, is much easier to work than Calendar though.

2) Moreover, did they both take into consideration the TimeZone? I think that the second one does, but what about the calendar?

Yes, they do. You can specify a TimeZone using Calendar.getInstance(java.util.TimeZone). Otherwise, a default one will be used.

3) I tried this code that I have found:

The code works just fine. Make sure you are using new java.util.Date() instead of java.sql.Date, for example.

4) What do you suggest me to use? The only thing I want is a simple comparison between current month,date,hour with some stored in memory. (for example if its January 29, display that event. if it is 19.00 am send notification and so on.)

It's really a matter of personal choice. Using Calendar, for example, to compare if today is January 29, you could simply use the following:

Calendar today = Calendar.getInstance();
int dayOfMonth = today.get(Calendar.DAY_OF_MONTH);
int month = today.get(Calendar.MONTH);
if (month == Calendar.JANUARY && dayOfMonth == 29) {
  // January 29
}

这篇关于时间与日期VS日历的java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆