java.util.Date和getYear() [英] java.util.Date and getYear()

查看:207
本文介绍了java.util.Date和getYear()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Java中遇到以下问题(我看到有些人在JavaScript中有
a类似的问题,但是我使用Java)

  System.out.println(new Date()。getYear()); 
System.out.println(new GregorianCalendar()。getTime()。getYear());
System.out.println(this.sale.getSaleDate()。getYear());
System.out.println(this.sale.getSaleDate()。getMonth());
System.out.println(this.sale.getSaleDate()。getDate());

返回

  I / System.out(4274):112 
I / System.out(4274):112
I / System.out(4274):112
I / (4274):1
I / System.out(4274):11

I不明白我认为2012年的112位。
发生了什么事?
java.util.Date 类不可用?我将这个作为一个字段存储在我的几个
的类中以存储日期和时间。我应该怎么做?

解决方案

根据javadocs:

  @Deprecated 
public int getYear()

已弃用。从JDK 1.1版开始,由 Calendar.get(Calendar.YEAR) - 1900 替换。



返回值这是从包含或以该Date对象表示的时间开始的年份减去1900的结果,如在当地时区所解释的。



返回:
此日期所代表的年份,减去1900年。



另请参见:
日历



所以112是正确的输出。我会遵循Javadoc中的建议,或者使用JodaTime。


I am having the following problem in Java (I see some people are having a similar problem in JavaScript but I'm using Java)

System.out.println(new Date().getYear());
System.out.println(new GregorianCalendar().getTime().getYear());
System.out.println(this.sale.getSaleDate().getYear());
System.out.println(this.sale.getSaleDate().getMonth());
System.out.println(this.sale.getSaleDate().getDate());

returns

I/System.out( 4274): 112
I/System.out( 4274): 112
I/System.out( 4274): 112
I/System.out( 4274): 1
I/System.out( 4274): 11

I don't understand the 112 bit which I thought would have been 2012. What's going on? Is the java.util.Date class unusable? I am storing this as a field in several of my classes to store a date and time. What should I do?

解决方案

According to javadocs:

@Deprecated
public int getYear()

Deprecated. As of JDK version 1.1, replaced by Calendar.get(Calendar.YEAR) - 1900.

Returns a value that is the result of subtracting 1900 from the year that contains or begins with the instant in time represented by this Date object, as interpreted in the local time zone.

Returns: the year represented by this date, minus 1900.

See Also: Calendar

So 112 is the correct output. I would follow the advice in the Javadoc or use JodaTime instead.

这篇关于java.util.Date和getYear()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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