問題描述
我有一個包含變量的類
private GregorianCalendar cal;
現(xiàn)在我想用 yyyy-mm-dd
格式的日期對其進行初始化,我嘗試使用默認構(gòu)造函數(shù),但得到的輸出類似于 "2017-05-25T14:36:52+03:00"
,我只想要 "2017-05-25"
日期部分?
Now I want to initialize it with a date in yyyy-mm-dd
format, I tried using the default constructor but the output I get is something like this "2017-05-25T14:36:52+03:00"
, I only want "2017-05-25"
date part?
如何實現(xiàn)?
推薦答案
首先,您可能會討厭我的重復,您需要了解日期和該日期的字符串表示之間存在差異;基本數(shù)據(jù)和它的字符串表示之間的區(qū)別.
First, you may hate me for reiterating, you need to understand that there’s a difference between a date and a string representation of that date; a difference between the fundamental data and a string representation of it.
其次,如果您愿意,可以在 Java 7 中使用 LocalDate
(以及其他 Java 日期和時間類).它(它們都在)在 ThreeTen-Backport ,將 Java SE 8 日期時間類向后移植到 Java SE 6 和 7.
Second, you can use LocalDate
(and the other Java date and time classes) with Java 7 if you want. It is (and they are all) in the ThreeTen-Backport , a back-port of the Java SE 8 date-time classes to Java SE 6 and 7.
由于我收集到 LocalDate
更符合您的要求很多,我真的認為您應該考慮一下.所以我建議代替你的 cal
Since I gather that LocalDate
fits your requirements much better, I really think you should give it a thought or two. So instead of your cal
I suggest
private LocalDate date = LocalDate.now(ZoneId.systemDefault());
還要考慮您是想要 JVM 的當前時區(qū)設置(如上所示)還是想要控制您使用的時區(qū).它會有所作為.
Also think about whether you want the current time zone setting of your JVM (as the above will give you) or you want to control which time zone you use. It will make a difference.
最后,如果你真的堅持.正如你現(xiàn)在所理解的,我不能給你一個字符串in一個GregorianCalendar
.您可以丟棄 GregorianCalendar
的時間部分,因此您只有日期部分.你可以把它格式化成你喜歡的字符串.
Finally, if you really insist. As you have understood by now, I cannot give you a string in a GregorianCalendar
. You may discard the time part of your GregorianCalendar
so you only have the date part. And you may format it into a string of your liking.
public class GregorianCalendarDemo {
private GregorianCalendar cal;
public GregorianCalendarDemo() {
cal = new GregorianCalendar(TimeZone.getDefault(), Locale.getDefault());
// discard time of day so we only have the date
cal.set(Calendar.HOUR_OF_DAY, cal.getActualMinimum(Calendar.HOUR_OF_DAY));
cal.set(Calendar.MINUTE, cal.getActualMinimum(Calendar.MINUTE));
cal.set(Calendar.SECOND, cal.getActualMinimum(Calendar.SECOND));
cal.set(Calendar.MILLISECOND, cal.getActualMinimum(Calendar.MILLISECOND));
}
protected GregorianCalendar getCal() {
return cal;
}
public String getFormattedCal() {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
return format.format(getCal().getTime());
}
}
當我剛剛調(diào)用 getFormattedCal()
時,它返回了 2017-05-25
.
When I just called getFormattedCal()
, it returned 2017-05-25
.
再次,確定時區(qū)和語言環(huán)境的默認值是否合適,或者您想要其他值.
Again, decide whether default values for time zone and locale are fine or you want something else.
您可能認為我們可以只使用 cal.set(Calendar.HOUR_OF_DAY, 0):
來丟棄小時,同樣使用分鐘和秒.它至少適用于所有情況的 99%.但是,隨著夏令時(夏令時)的過渡,一天不能保證從 0 小時開始,所以上面的代碼更加安全.
You might have thought that we could discard the hours with just cal.set(Calendar.HOUR_OF_DAY, 0):
, and similarly with minutes and seconds. It would work in 99 % of all cases at least. However, with transistion to summer time (daylight savings time), the day is not guaranteed to begin at 0 hours, so the above code is more bulletproof.
ThreeTen Backport 主頁
這篇關于如何初始化公歷,日期為 YYYY-MM-DD 格式?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!