久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

為什么 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?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

限時送ChatGPT賬號..

我在將字符串日期解析為 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?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Parsing an ISO 8601 string local date-time as if in UTC(解析 ISO 8601 字符串本地日期時間,就像在 UTC 中一樣)
How to convert Gregorian string to Gregorian Calendar?(如何將公歷字符串轉換為公歷?)
Java: What/where are the maximum and minimum values of a GregorianCalendar?(Java:GregorianCalendar 的最大值和最小值是什么/在哪里?)
Calendar to Date conversion for dates before 15 Oct 1582. Gregorian to Julian calendar switch(1582 年 10 月 15 日之前日期的日歷到日期轉換.公歷到儒略歷切換)
java Calendar setFirstDayOfWeek not working(java日歷setFirstDayOfWeek不起作用)
Java: getting current Day of the Week value(Java:獲取當前星期幾的值)
主站蜘蛛池模板: 麻豆精品国产免费 | 午夜男人的天堂 | 三级视频久久 | 亚洲精品日韩一区二区电影 | 午夜寂寞影院列表 | 欧美在线高清 | 最新国产精品 | 国产精品免费一区二区 | 国产精品久久久久久久久久久久冷 | 美女天天操 | 亚洲色图网址 | 欧美精品一二三 | 欧美国产一区二区 | 成人免费观看男女羞羞视频 | 天天射色综合 | 特黄色一级毛片 | 992人人草| 国产精品国产精品国产专区不卡 | 成人在线小视频 | 午夜精品视频在线观看 | 精品国产91久久久久久 | 日日操夜夜操天天操 | 久久99精品国产自在现线小黄鸭 | 一区福利视频 | 国产精品免费一区二区三区四区 | 四虎影院免费在线播放 | 日韩一区二区三区av | 五月婷婷激情网 | 一级黄色影片在线观看 | 成人在线播放 | 一区二区亚洲 | 久久久久亚洲 | 国产精品成人一区二区三区夜夜夜 | 伊人色综合久久天天五月婷 | 午夜精品视频一区 | 欧美多人在线 | 一本一道久久a久久精品综合蜜臀 | 免费在线a视频 | 免费一级片 | 欧美激情在线播放 | 男女搞网站 |