简体   繁体   English

我该如何设定日期?

[英]How should I set the date?

I want to modify the date in Java. 我想用Java修改日期。 That's no problem, but I'm a bit confused about how I should do this. 这没问题,但我对如何做到这一点感到有点困惑。

1. 1。

Calendar c = Calendar.getInstance(); 
c.set(2010,9,10,6,0);

2. 2。

Calendar c = Calendar.getInstance();
c.set(c.YEAR, 2010);
c.set(c.MONTH, 9);
c.set(c.DATE, 10);
c.set(c.HOUR, 6);
c.set(c.MINUTE, 0);

Which one is the better way? 哪一种更好? If I use the second way I have to type a bit more and I learned that the code should be as short as possible. 如果我使用第二种方式,我必须输入更多,我知道代码应该尽可能短。 On the other side the second way is a bit more clear. 另一方面,第二种方式更清晰一点。

Try this, but ctrl+space could help you next time. 试试这个,但ctrl + space可以帮助你下次。

Calendar cal = Calendar.getInstance();
    cal.set(year, month, date, hourOfDay, minute);

Thanks 谢谢

The ONLY way to modify a date is in one atomic operation. 修改日期的唯一方法是在一个原子操作中。 Doing it a field at a time will result in surprisingly incorrect results sometimes. 一次做一个字段有时会导致令人惊讶的错误结果。 When you create a new Calendar it gets initialized with todays date. 当您创建新Calendar它会使用今天的日期进行初始化。 Suppose today is May 31st and you set the month to June, intending to set the day next. 假设今天是5月31日,你将月份设置为6月,打算设置下一天。 When you set the month, to June, 31 is no longer valid, so the date gets "normalized" to July 1st. 当您将月份设置为6月31日不再有效时,日期将“标准化”至7月1日。 You then set the day, but now the month is wrong. 然后你设置了一天,但现在月份是错误的。

You can construct quite a few of these "interesting" cases. 你可以构建这些“有趣”案例中的一些。 Some will fail if you set the month first, and some will fail if you set the day first, so there's no order that is guaranteed to work for all cases. 如果您先设置月份,有些会失败,如果您先设置第一天,有些会失败,所以没有任何订单可以保证适用于所有情况。 Set the date as a single operation. 将日期设置为单个操作。

Here are some examples: 这里有些例子:

Today         Changes             Result
5/31/2013     MM->6, DD->12       7/12/2013 (not 6/12)
6/01/2013     DD->31, MM->07      7/01/2013 (not 7/31)

A short code is not all time the good way in Java (or other language). 短代码并不是Java(或其他语言)的好方法。 You have to be clear for you and for other developers who want to use your code. 您必须清楚地了解您和其他想要使用您的代码的开发人员。

So, in my opinion, you can use the first method but with more java doc and comments than the second method which is the most explicit (attention : the second method is the most explicit but it need comments too...). 因此,在我看来,你可以使用第一种方法,但使用更多的java文档和注释,而不是第二种方法,这是最明确的(注意:第二种方法是最明确的,但它也需要注释......)。

Well Stefan, I think you should go for the cleaner approach. 斯蒂芬,我认为你应该采取更清洁的方法。 The first method has only two lines of code to solve your problem . 第一种方法只有两行代码来解决您的问题。 You should adopt it. 你应该采用它。 From my development experience, I have learned that the LOC (Line Of Code) really matters in the maintenance of code and we should always think about solving a problem with minimum lines of code. 根据我的开发经验,我了解到LOC(代码行)在维护代码时非常重要,我们应该始终考虑用最少的代码行来解决问题。 So I think you should take advantage of multi-parameters setter API. 所以我认为你应该利用多参数setter API。
Thanks! 谢谢!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM