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

问题描述

我有一点麻烦解析字符串日期到日期对象。我使用 DateFormat 来解析字符串,当我打印日期的值,它给我我想要的。



但是当我尝试得到的一天,一个月或一年它给我错误的价值观。例如,年是2011年,但是当我做 .getYear()它给我111.我不知道为什么这种情况发生。以下是相关代码段:

 日期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



我需要能够得到时间序列图的日期,月份和年份。

解决方案

已被弃用。而应使用类。






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

strong>:

  Year = 2011 
Month = 3
Day = 12






至于月份 0为基础。这意味着January = 0和December = 11.如,


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.

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

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.

解决方案

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

Output:

Year = 2011
Month = 3
Day = 12


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

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

09-13 09:52