如何初始化日期为YYYY-MM-DD格式的公历? [英] How to initialize Gregorian calendar with date as YYYY-MM-DD format?

查看:99
本文介绍了如何初始化日期为YYYY-MM-DD格式的公历?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含变量的类

 私有GregorianCalendar cal; 

现在我想用 yyyy-mm-dd中的日期初始化它格式,我尝试使用默认构造函数,但得到的输出是这样的 2017-05-25T14:36:52 + 03:00 ,我只想要 2017-05-25 日期部分?



如何实现?

解决方案

首先,您可以讨厌我重申一下,您需要了解日期和该日期的字符串表示形式之间存在差异;基本数据和它的字符串表示形式之间的差异。



第二,您可以使用 LocalDate (以及其他Java日期和时间类)(如果需要)与Java 7一起使用。它是(而且都是) ThreeTen-Backport ,即Java SE 8日期时间类的后向端口,移植到Java SE 6和7。



由于我收集了 LocalDate 更好地满足了您的要求 ,我真的认为您应该考虑一下。因此,建议您不要使用 cal

  private LocalDate日期= LocalDate。现在(ZoneId.systemDefault()); 

还要考虑是否要设置JVM的当前时区(如上所述) ),或者您想控制使用哪个时区。



最后,如果您真的坚持的话。如您目前所了解的,我无法在 GregorianCalendar 中给您一个字符串。您可以丢弃 GregorianCalendar 的时间部分,因此只有日期部分。

 公共类GregorianCalendarDemo {

private GregorianCalendar校准

public GregorianCalendarDemo(){
cal = new GregorianCalendar(TimeZone.getDefault(),Locale.getDefault());
//舍弃一天中的时间,因此只有日期
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));
}

受保护的GregorianCalendar getCal(){
return cal;
}

public String getFormattedCal(){
SimpleDateFormat format = new SimpleDateFormat( yyyy-MM-dd);
返回format.format(getCal()。getTime());
}
}

当我刚刚打电话给 getFormattedCal时(),则返回 2017-05-25



再次,确定是否



您可能以为我们可以只用 cal来丢弃小时数。



set(Calendar.HOUR_OF_DAY,0):,类似的是分钟和秒。至少在所有情况下,它都可以使用99%。但是,如果将时间转换为夏季时间(夏令时),则不能保证一天从0小时开始,因此上面的代码更加安全。



链接



ThreeTen Backport主页


I have a class which contains a variable

private GregorianCalendar cal;

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?

How do I achieve it?

解决方案

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.

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.

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());

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.

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());
    }
}

When I just called getFormattedCal(), it returned 2017-05-25.

Again, decide whether default values for time zone and locale are fine or you want something else.

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.

Link

ThreeTen Backport home

这篇关于如何初始化日期为YYYY-MM-DD格式的公历?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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