0

I'm trying to do a simple console display in Eclipse and my time and date are coming directly from the computer. For some reason, the year display is only coming out in 3 characters instead of 4, and I have no idea why. Any help would be amazing. Here's the code:

public class InClass3 implements ActionListener
{
static int month, day, year, hour, minute;

public static void Display()
{
    int i;
    String []Date = new String[3];
    String []Hello = new String[3];
    String []Time = new String[3];
    Date();
    Time();
    for (i = 0; i < 3; i ++)
    {
        switch (i)
        {
        case 0:
            Hello[0] = "HELLO WORLD";
            Date[0] = month + "/" + day + "/" + year;               
            Time[0] = hour + ":" + minute;
            break;

        case 1:
            Hello[1] = "hello world";
            Date[1] = year + "." + month + "." + day;
            if (hour > 12)
            {
                hour = hour - 12;
                Time[1] = hour + ":" + minute + "pm";
            }
            else 
            {
                Time[1] = hour + ":" + minute + "am";
            }
            break;

        case 2:
            Hello[2] = "Hello World";
            Date[2] =  day + " " + month + " " + year;
            Time[2] =  hour + ":" + minute;
            break;

        default: System.out.println("Fatal error occured.  Please relaunch this application.");
        }
        System.out.println(Hello[i] + "\n" + Date[i] + "\n" + Time[i]);
    }

}

@SuppressWarnings("deprecation")
public static void Time()
{
    Date t = new Date();
    hour = t.getHours();
    minute = t.getMinutes();        
}

@SuppressWarnings("deprecation")
public static void Date()
{
    Date d = new Date();
    year = d.getYear();
    month = d.getMonth();
    day = d.getDay();
}

public static void main(String[] args) 
{
    Display();
}

@Override
public void actionPerformed(ActionEvent arg0) 
{
    // TODO Auto-generated method stub

}

}

And this is what it's outputting:

HELLO WORLD

3/5/115

14:47

hello world

115.3.5

2:47pm

Hello World

5 3 115

2:47

I would obviously like it to display the 2015 - not the 115. Thanks for any insight.

Uchiha Itachi
  • 1,251
  • 1
  • 16
  • 42
  • 2
    I would start by having a look at the [JavaDocs for `java.util.Date`](https://docs.oracle.com/javase/7/docs/api/java/util/Date.html#getYear()) - *"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"* - Also, `Date#getYear` is deprecated and it's use is highly discouraged, in fact, if you're using Java 8, you should be using the newer Timer API – MadProgrammer Apr 03 '15 at 22:00
  • 1
    You might like to have a read through [Code Conventions for the Java TM Programming Language](http://www.oracle.com/technetwork/java/codeconvtoc-136057.html), it will make it easier for people to read your code and for you to read others – MadProgrammer Apr 03 '15 at 22:00
  • FYI, the troublesome old date-time classes such as [`java.util.Date`](https://docs.oracle.com/javase/8/docs/api/java/util/Date.html), [`java.util.Calendar`](https://docs.oracle.com/javase/8/docs/api/java/util/Calendar.html), and `java.text.SimpleTextFormat` are now [legacy](https://en.wikipedia.org/wiki/Legacy_system), supplanted by the [java.time](https://docs.oracle.com/javase/8/docs/api/java/time/package-summary.html) classes. See [Tutorial by Oracle](https://docs.oracle.com/javase/tutorial/datetime/TOC.html). – Basil Bourque Jun 21 '17 at 06:21

4 Answers4

3
year = d.getYear() + 1900;

That's because the year is stored as the number of years since 1900.

However, there are some good reasons why Date is deprecated. You should use Calendar instead.

Gabriel Negut
  • 13,860
  • 4
  • 38
  • 45
2

Do not rely on Date's get-like methods. They are deprecated.

The getYear() method returns the date "minus 1900", so that 1999 is 99, 2000 is 100, etc. You have a Y2K bug!

Returns:

the year represented by this date, minus 1900.

Instead, use a SimpleDateFormat object to control your formatting.

SimpleDateFormat sdf = new SimpleDateFormat("M/d/yyyy");
System.out.println(sdf.format(new Date()));
Community
  • 1
  • 1
rgettman
  • 176,041
  • 30
  • 275
  • 357
2

https://docs.oracle.com/javase/8/docs/api/java/util/Date.html#getYear--

getYear @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.

You could consider newer options from Java 8 https://docs.oracle.com/javase/8/docs/api/java/time/Year.html#now--

Victor
  • 3,520
  • 3
  • 38
  • 58
0

tl;dr

Year.now( ZoneID.of( "America/Montreal" ) )
    .getValue()

Details

You are using troublesome old date-time classes now supplanted by the java.time classes.

LocalDate

The LocalDate class represents a date-only value without time-of-day and without time zone.

A time zone is crucial in determining a date. For any given moment, the date varies around the globe by zone. For example, a few minutes after midnight in Paris France is a new day while still “yesterday” in Montréal Québec.

ZoneId z = ZoneId.of( “America/Montreal” );
LocalDate today = LocalDate.now( z );

Interrogate the LocalDate for its year number.

int year = today.getYear() ;

Alternatively, use Year class.

int yearNumber = Year.now( ZoneId.of( "Pacific/Auckland" ) ).getValue() ;

About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old date-time classes such as java.util.Date, .Calendar, & java.text.SimpleDateFormat.

The Joda-Time project, now in maintenance mode, advises migration to java.time.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations.

Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport and further adapted to Android in ThreeTenABP (see How to use…).

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154