简体   繁体   English

如何从年,月和日获取日期对象?

[英]How to get a Date object from year,month and day?

When I used the following code, the Date Object was wrong. 当我使用以下代码时, Date对象是错误的。

Date date = new Date(day.getYear(), day.getMonth(), day.getDay());

Can anyone tell me how to get the Date Object From the value of year, month and day? 谁能告诉我如何从年,月和日的值中获取日期对象?

You can use the Calendar class to achieve this. 您可以使用Calendar类来实现此目的。

public static void main(String[] args) throws Exception {
    Date date = new Date (115, 7, 5);
    System.out.println("date     = " + date);

    Calendar calendar = GregorianCalendar.getInstance();
    calendar.set(Calendar.DAY_OF_MONTH, 5);
    calendar.set(Calendar.MONTH, 7);
    calendar.set(Calendar.YEAR, 2015);
    calendar.set(Calendar.MILLISECOND, 0);
    calendar.set(Calendar.SECOND, 0);
    calendar.set(Calendar.MINUTE, 0);
    calendar.set(Calendar.HOUR_OF_DAY, 0);
    date = calendar.getTime();
    System.out.println("calendar = " + date);

    // or create directly a new clanedar instance
    // thanks Tom to mention this
    calendar = new GregorianCalendar(2015, 7, 5);
    date = calendar.getTime();
    System.out.println("calendar = " + date);
}

output 输出

date     = Wed Aug 05 00:00:00 CEST 2015
calendar = Wed Aug 05 00:00:00 CEST 2015
calendar = Wed Aug 05 00:00:00 CEST 2015

Without knowing more I'd have to guess but probably you didn't read the JavaDoc on that deprecated constructor: 在不知道更多信息的情况下,我不得不猜测,但也许您没有在该过时的构造函数上阅读过JavaDoc:

year the year minus 1900. 今年年份减去1900。
month the month between 0-11. 0-11 个月之间的月份。
date the day of the month between 1-31. 日期在1-31之间的日期

As you can see, if you want to create a date for today (Aug 5th 2015) you'd need to use new Date (115, 7, 5); 如您所见,如果要为今天(2015年8月5日)创建日期,则需要使用new Date (115, 7, 5);

If you see that documentation you are free to guess why this is deprecated and should not be used in any new code. 如果您看到该文档,则可以随意猜测为什么不推荐使用此文档,并且不应在任何新代码中使用它。 :) :)

You can do it with a workaround if you're stuck to Java < 8, but it's very ugly: 如果您坚持使用Java <8,则可以使用一种解决方法来做到这一点,但这非常丑陋:

java.util.Date date = new java.text.SimpleDateFormat("dd.MM.yyyy").parse("05.08.2015");

as @Thomas already stated, the default Constructor for date/month/year is deprecated. 如@Thomas已经指出的那样,不建议使用日期/月份/年份的默认构造函数。 Probably take a look at this link if you have access to Java8. 如果可以访问Java8,请看一下此链接

您应该使用getDate()而不是getDay()方法来获取日期,因为getDay()返回星期几而不是月份中的某天

  • int getYear() Deprecated. int getYear()不推荐使用。 As of JDK version 1.1, replaced by Calendar.get(Calendar.YEAR) - 1900. 从JDK 1.1版开始,由Calendar.get(Calendar.YEAR) - 1900.取代Calendar.get(Calendar.YEAR) - 1900.

  • int getMonth() Deprecated. int getMonth()不推荐使用。 As of JDK version 1.1, replaced by Calendar.get(Calendar.MONTH). 从JDK 1.1版开始,由Calendar.get(Calendar.MONTH).取代Calendar.get(Calendar.MONTH).

  • int getDay() Deprecated. int getDay()不推荐使用。 As of JDK version 1.1, replaced by Calendar.get(Calendar.DAY_OF_WEEK). 从JDK版本1.1开始,由Calendar.get(Calendar.DAY_OF_WEEK).取代Calendar.get(Calendar.DAY_OF_WEEK).

Hence use : 因此使用:

Date date = new Date(Calendar.get(Calendar.YEAR) - 1900, Calendar.get(Calendar.MONTH), Calendar.get(Calendar.DAY_OF_WEEK));

Recomendation : Use Joda-Time 推荐 :使用Joda-Time

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

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