0
public class Date {

    private int day;
    private int month;
    private int year;


    public Date(){
        this.day = 01;
        this.month = 01;
        this.year = 2000;
    }

    public Date(int month, int day, int year){
        this.day = day;
        this.month = month;
        this.year = year;
    }

    public int getday(){
        return day;
    }

    public int getmonth(){
        return month;
    }

    public int getyear(){
        return year;
    }

    public void setday(int day){
        this.day = day;
    }

    public void setmonth(int month){
        this.month = month;
    }

    public void setyear(int year){
        this.year = year;
    }

    public boolean leapYear(){
        if((year%4 == 0) && (year%100 != 0) || (year%400 == 0))
            return true;
        return false;
    }

    public int differenceInDays(){


    }

    public String dateToString(){
        String d = Integer.toString(this.day);
        String m = Integer.toString(this.month);
        String y = Integer.toString(this.year);
        return "d/m/y";
    }

I'm having few problems, one, how can I show the difference between two dates in terms of days without using an external lib. Second, is when I call the methods in the main they don't really work.

Here's my main class so far.

public static void main(String[] args){

        Date d1 = new Date();

        d1.setday(31);
        d1.setmonth(12);
        d1.setyear(2004);

        d1.dateToString();


    }

The date, month, a year I set won't turn into a String when I use the dateToString() method. What am I doing wrong? And I really want to do this without using external libraries.

display name
  • 4,165
  • 2
  • 27
  • 52
Rocky Singh
  • 629
  • 5
  • 12
  • 1
    _"they don't really work"_ is not sufficient. Please visit the [help] and read [ask] for details. Your question is too broad and unfocused, and is really more of a general "please help me" which is off-topic on StackOverflow. Please also read [Why is “Can someone help me?” not an actual question?](http://meta.stackoverflow.com/q/284236/18157) – Jim Garrison Mar 18 '17 at 23:32
  • Possible duplicate of [Java: date difference](http://stackoverflow.com/questions/11949830/java-date-difference) – JCasso Mar 18 '17 at 23:33
  • 1
    Please use a relevant title. – ROMANIA_engineer Mar 18 '17 at 23:34
  • If you want to learn about writing a simple Date class, I suggest you read the source code for `java.util.Date` to understand some of the intricacies of coding with time and intervals. But remember that `java.util.Date` is old and now considered obsolete and underpowered to cope with real-world date calculation. – Jim Garrison Mar 18 '17 at 23:35
  • Thank you guys!! @JimGarrison – Rocky Singh Mar 18 '17 at 23:43
  • Date-time work is surprisingly complicated. Unwise to roll-your-own date-time classes. Check out the java.time classes instead. – Basil Bourque Mar 19 '17 at 06:54

4 Answers4

1

First of all, there is a compilation error in your differenceInDays(), so fix it.

Secondly, in your dateToString() method, you are always returning "d/m/y" string (hardcoded), but what you need to do is to compose the date string using the actual variables as shown below:

public String dateToString() {
        String dateString= new StringBuilder().append(this.day).append("/").
           append(this.month).append("/").append(this.year).toString();
        return dateString;
    }

Also, I suggest you always use StringBuilder as shown above to concatenate strings.

Vasu
  • 21,832
  • 11
  • 51
  • 67
  • Thank you!! would I be able to use dateToString method in my differenceInDays method. When subtracting in terms of days should I use the dates as Strings or Integer. – Rocky Singh Mar 18 '17 at 23:37
  • obviously, you need to use integers to subtract – Vasu Mar 18 '17 at 23:39
1

One quick observation....

you are returning a string "d/m/y"

It appears what you are trying to do is return d + "/" + m + "/" + y;

Or another way.... String dateString = d + "/" + m + "/" + y; return dateString;

  • Thank you, How would I subtract two dates when they are inputs from the user? You don't have to give me the answer just help with it. – Rocky Singh Mar 18 '17 at 23:45
  • Calculating the diff between two dates is a little more involved considering there is a calculation to be taken into consideration for daylight savings changes each year. This is a decent resource I just found for your problem. http://stackoverflow.com/questions/1555262/calculating-the-difference-between-two-java-date-instances – Dustin Dwyer Mar 20 '17 at 03:13
1

If you want to print out use

System.out.println(d1.dateToString());

In your main method And correct your datToString method with the given answers

Incognito
  • 133
  • 1
  • 10
-1
    public String dateToString(){
        String d = Integer.toString(this.day);
        String m = Integer.toString(this.month);
        String y = Integer.toString(this.year);
        return d + "/" + m + "/" + y;
    }
display name
  • 4,165
  • 2
  • 27
  • 52
  • 1
    Why down vote the answer without leaving a comment that provides a legit reason? – display name Mar 18 '17 at 23:35
  • @RockySingh please select as answer if it solves the problem. Thanks. – display name Mar 18 '17 at 23:39
  • The expectation of answers here is that they also describe the solution, not just dump a code-only 'solution'. And regarding you asking to leave a comment: there is no such requirement. A downvote means that someone thought "This answer is not useful" (the tooltip of the downvote button). – Mark Rotteveel Mar 19 '17 at 10:30
  • @MarkRotteveel Looking at the history of this Q&A, it is obvious that someone down voted all answers that were still in edit as well as the beginner's question. It is not how SO was meant to be. Expectation? It's way easier to come back another day and say, "wait, why doesn't the answers reach expectation afterwards?" Oh perhaps it's not worth editing since it's downvoted right after posting. But thanks for commenting long after the incident. Looking at people up vote my request, I think I'm not the only one who dislike a quick down vote for fun type of peer behavior. – display name Mar 19 '17 at 16:09