为什么 Java 的 Date.getYear() 返回 111 而不是 2011?

Why does Java#39;s Date.getYear() return 111 instead of 2011?(为什么 Java 的 Date.getYear() 返回 111 而不是 2011?)
本文介绍了为什么 Java 的 Date.getYear() 返回 111 而不是 2011?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我在将字符串日期解析为 Date 对象时遇到了一些麻烦.我使用 DateFormat 来解析字符串,当我打印日期的值时,它给出了我所期望的.

I am having a bit of trouble parsing a string date to a Date object. I use a DateFormat to parse the string, and when I print the value of the date, it gives me what I expect.

但是当我尝试获取日期、月份或年份时,它给了我错误的值.例如,年份是 2011 年,但是当我执行 .getYear() 时,它给了我 111.我不知道为什么会这样.以下是相关代码段:

But when I try get the day, the month or the year it gives me the wrong values. For instance, the year is 2011, but when I do .getYear() it gives me 111. I have no idea why this is happening. Here is the relevant code segment:

    Date dateFrom = null;

    String gDFString = g.getDateFrom();

    System.out.println(gDFString);

    DateFormat df = new SimpleDateFormat("dd/MM/yyyy");

    try {
        dateFrom = df.parse("04/12/2011");

        System.out.println(dateFrom);

        System.out.println(dateFrom.getYear());
    } catch (ParseException e) {
        e.printStackTrace();
    }

当我输出 dateFrom 时,我得到 Sun Dec 04 00:00:00 GMT 2011,这是您所期望的.但是打印 .getYear() 会返回 111.

When I out print dateFrom, I get Sun Dec 04 00:00:00 GMT 2011, which is what you would expect. But printing .getYear() returns 111.

我需要能够为时间序列图获取日期的日、月和年.

I need to be able to get the day, month and year of the date for a time series graph.

推荐答案

这些方法已弃用.相反,请使用 Calendar 类.

Those methods have been deprecated. Instead, use the Calendar class.

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;

public final class DateParseDemo {
    public static void main(String[] args){
         final DateFormat df = new SimpleDateFormat("MM/dd/yyyy");
         final Calendar c = Calendar.getInstance();
         try {
             c.setTime(df.parse("04/12/2011"));
             System.out.println("Year = " + c.get(Calendar.YEAR));
             System.out.println("Month = " + (c.get(Calendar.MONTH)));
             System.out.println("Day = " + c.get(Calendar.DAY_OF_MONTH));
         } 
         catch (ParseException e) {
             e.printStackTrace();
         }
    }
}

输出:

Year = 2011
Month = 3
Day = 12

<小时>

对于 month 字段,这是从 0 开始的.这意味着一月 = 0 和十二月 = 11.正如 javadoc 所述,


And as for the month field, this is 0-based. This means that January = 0 and December = 11. As stated by the javadoc,

get 和 set 的字段编号,表示月份.这是一个特定于日历的值.公历一年的第一个月而儒略历是JANUARY,即0;最后取决于一年中的月数.

Field number for get and set indicating the month. This is a calendar-specific value. The first month of the year in the Gregorian and Julian calendars is JANUARY which is 0; the last depends on the number of months in a year.

这篇关于为什么 Java 的 Date.getYear() 返回 111 而不是 2011?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

How to send data to COM PORT using JAVA?(如何使用 JAVA 向 COM PORT 发送数据?)
How to make a report page direction to change to quot;rtlquot;?(如何使报表页面方向更改为“rtl?)
Use cyrillic .properties file in eclipse project(在 Eclipse 项目中使用西里尔文 .properties 文件)
Is there any way to detect an RTL language in Java?(有没有办法在 Java 中检测 RTL 语言?)
How to load resource bundle messages from DB in Java?(如何在 Java 中从 DB 加载资源包消息?)
How do I change the default locale settings in Java to make them consistent?(如何更改 Java 中的默认语言环境设置以使其保持一致?)