How can I return int date,month, year in a single method

10,750

Solution 1

I think I may see what you are trying to say. 'Braj' had the right idea in the code he offered. However, I am not sure if that fully answers your needs or not. I am unsure as to whether you want to return each value (day, month, year) separately, or all together using one method? I will try my best regardless.

Below I will show you the code for DateTime:

import java.util.GregorianCalendar;

class DateTime {
    private int day, month, year;
    GregorianCalendar date = new GregorianCalendar();

    public DateTime() {
        day = date.get(GregorianCalendar.DAY_OF_MONTH);
        month = date.get(GregorianCalendar.MONTH) + 1;
        year = date.get(GregorianCalendar.YEAR);
    }
    public DateTime(int day, int month, int year){
        this.day = day;
        this.month = month;
        this.year = year;
    }

    // Accessors ///
    public int getDay(){
        return day;
    }
    public int getMonth(){
        return month;
    }
    public int getYear(){
        return year;
    }

    // Mutators ///
    public int setDay(){
        return day;
    }
    public int setMonth(){
        return month;
    }
    public int setYear(){
        return year;
    }

    // String ///
    public String toString(){
        return getMonth() + "/" + getDay() + "/" + getYear();
    }
}

Now to explain.

I made the day, month, and year private to begin with and kept your default constructor practically the same except for one thing.

I added 1 to the month because months January - December are indexed at 0 - 11 rather than 1 - 12. This means your month wouldn't follow conventional means. I never knew something such as the GregorianCalendar existed in java's import libraries, so I was initially confused for a couple of seconds when I tested my code.

Moving on I made an additional constructor in case you ever wanted to input your own dates for whatever reason. If you do not plan on making separate DateTime objects to hold random dates, then this is completely unnecessary.

Afterwards I made several accessors - getDay(), getMonth(), getYear() - in case you ever needed to grab the specific aspect of the date.

To finish it off I made a toString() method to print out your DateTime's day month and year all in one String in a conventional format.

If you don't want to change the individual aspects of the date (such as the day, the month, or the year) I'd recommend getting rid of the 'Mutators'. If you don't want to access the individual aspects of the date, then I'd recommend getting rid of the 'Accessors'.

NOW, if for some reason, you want to be able to edit and access the date's components inside your CLIENT, then you should not make the instances of day, month, and year private, but instead public.

Here is the code for the DateTime class that will allow you to change the values inside the CLIENT. (I don't recommend this. I suggest you get used to making accessors and mutators instead.) I decided to call it DateTime2:

import java.util.GregorianCalendar;

class DateTime2 {
    public int day, month, year;
    GregorianCalendar date = new GregorianCalendar();

    public DateTime2() {
        day = date.get(GregorianCalendar.DAY_OF_MONTH);
        month = date.get(GregorianCalendar.MONTH) + 1;
        year = date.get(GregorianCalendar.YEAR);
    }
    public DateTime2(int day, int month, int year){
        this.day = day;
        this.month = month;
        this.year = year;
    }
    public String toString(){
        return month + "/" + day + "/" + year;
    }
}

I left the second constructor in there in case you still want to make separate DateTime2 objects with different dates...

And here if the client code that tests the two classes out, followed by the output:

CLIENT:

public class DateTime_CLIENT{
    public static void main(){
        DateTime dt = new DateTime();

        System.out.println("- DateTime -");
        System.out.println("Date: " + dt);
        System.out.println("Month: " + dt.getMonth());
        System.out.println("Day: " + dt.getDay());
        System.out.println("Year: " + dt.getYear() + "\n");

        DateTime2 dt2 = new DateTime2();

        System.out.println("- DateTime2 -");
        System.out.println("Date: " + dt2);
        System.out.println("Month: " + dt2.month);
        System.out.println("Day: " + dt2.day);
        System.out.println("Year: " + dt2.year + "\n");

        dt2.day = 400000;

        System.out.println("- DateTime2 - CHANGE FROM CLIENT");
        System.out.println("Date: " + dt2);
        System.out.println("Month: " + dt2.month);
        System.out.println("Day: " + dt2.day);
        System.out.print("Year: " + dt2.year);
    }
}

OUTPUT:

- DateTime -
Date: 7/16/2014
Month: 7
Day: 16
Year: 2014

- DateTime2 -
Date: 7/16/2014
Month: 7
Day: 16
Year: 2014

- DateTime2 - CHANGE FROM CLIENT
Date: 7/400000/2014
Month: 7
Day: 400000
Year: 2014

Hope this helped! ;D

Solution 2

Define a type to encapsulate the three attributes, and change method return type to that class, like this:

public class YearMonthDay {
    private final year;
    private final month;
    private final day;
    public int getYear() {return year;}
    public int getMonth() {return month;}
    public int getDay() {return day;}
    public YearMonthDay(int y, int m, int d) {
        year = y;
        month = m;
        day = d;
    }
    // Consider overriding toString, and optionally hashCode and equals
}

Now you can change your class to return YearMonthDay:

public YearMonthDay TimeT(){
    return new YearMonthDay(
        date.get(Calendar.YEAR)
    ,   date.get(Calendar.MONTH)
    ,   date.get(Calendar.DAY_OF_MONTH)
    );
}

A caller can now obtain the YearMonthDay once, and then access its properties from multiple places.

Solution 3

Create different method for each one

public int getDay(Date date){
     return date.get(Calendar.DAY_OF_MONTH);
}

public int getMonth(Date date){
     return date.get(Calendar.MONTH);
}


public int getYear(Date date){
     return date.get(Calendar.YEAR);
}

Solution 4

Create a class out of the three fields and return an object of this type.

Solution 5

Java can't actually return multiple values with a method. However, you can have your method return a string with the necessary information, then parse the string. Alternatively, create a daymonthyear object with all of the necessary attributes and return the object.

Share:
10,750
Indranil Banerjee
Author by

Indranil Banerjee

Updated on June 04, 2022

Comments

  • Indranil Banerjee
    Indranil Banerjee almost 2 years

    I have written a java program which will return date, month and year (datatype int)

        import java.util.*;
        class DateTime {
    
    
        int day,month,year;
    
    
        GregorianCalendar date = new GregorianCalendar();
    
        public DateTime () {
    
          public  int TimeT() {
    
    
                day = date.get(Calendar.DAY_OF_MONTH);
                month = date.get(Calendar.MONTH);
                year = date.get(Calendar.YEAR);
                       // return day;
          }
    
      }
    

    I am really confuse how to return day, month and year. I have to return it, so I can use it many times.