-3

How would I get the number of days completed in Java when I input some random date(dd/mm/yyyy). For example , if my input is 15 1 2020 I need to get 15 as output.

Here is my code.



public class NumberOfCompletedDays {

    /**
     * @param args
     * @throws ParseException 
     */
    public static void main(String[] args) throws ParseException {

        Scanner scanner = new Scanner(System.in);
        String date1;
        String date2 = "00 00 0000";
        long completedDays;
        System.out.println("Enter the date");
        date1 = scanner.nextLine();
        String[] date1Array = date1.split(" ");
        String[] date2Array = date2.split(" ");
        date2Array[2] = date1Array[2];
        StringBuilder builder = new StringBuilder();
        for(int i  = 0; i < date2Array.length; i++){
            builder.append(date2Array[i]);
            builder.append(" ");
        }
        date2 = builder.toString();

        SimpleDateFormat myFormat = new SimpleDateFormat("dd MM yyyy");
        Date mydate1 = myFormat.parse(date1);
        Date mydate2 = myFormat.parse(date2);
        completedDays = mydate1.getTime() - mydate2.getTime();
        System.out.println ("Days: " + TimeUnit.DAYS.convert(completedDays, 
        TimeUnit.MILLISECONDS));

    }

}

rocky
  • 253
  • 2
  • 10
Sebastian
  • 29
  • 1
  • 7
  • 1
    Are you asking for the day of the year? If so: `LocalDate.of(2020, 1, 15).getDayOfYear()` – Andreas Apr 04 '20 at 11:00
  • I recommend you don’t use `SimpleDateFormat` and `Date`. Those classes are poorly designed and long outdated, the former in particular notoriously troublesome. Instead use `LocalDate` and `DateTimeFormatter`, both from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. Apr 04 '20 at 11:10

2 Answers2

1

java.time

Do use java.time, the modern Java date and time API, for your date work. Then it’s much simpler:

static DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("dd MM uuuu");

/** @throws DateTimeParseException if the user enters an invalid date */
public static void main(String[] args) {

    Scanner scanner = new Scanner(System.in);
    System.out.println("Enter the date");
    String date1 = scanner.nextLine();
    LocalDate mydate1 = LocalDate.parse(date1, dateFormatter);
    long completedDays = mydate1.getDayOfYear();
    System.out.println ("Days: " + completedDays);
}

Example session:

Enter the date
15 01 2020
Days: 15

What went wrong in your code?

Apart from over-complicating things and using old and troublesome date and time classes you were counting days from the 0th day of the 0th month. This is clearly nonsense, but SimpleDateFormat lets you get away with it. The 0th month is the month before the 1st month, so December of the previous year. And the 0th day of the month is the day before the 1st, so 30th November. The count of days from November 30 to January 15 is 46, for example, the result that you probably got.

I say probably because converting from milliseconds to days, like you did, does not always give the correct and expected result. This conversion assumes that a day is always 24 hours, which it isn’t always. Summer time (DST) and other time anomalies cause a day to be longer or shorter sometimes. If the user enters a date after the spring gap or spring forward on the Northern hemisphere (but before the fall back), converting the milliseconds will result in one day too few, for example.

Link

Oracle tutorial: Date Time explaining how to use java.time.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
0

If you are asking about finding difference between two days:

SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy", Locale.ENGLISH);
Date firstDate = sdf.parse("06/24/2017");
Date secondDate = sdf.parse("06/30/2017");

long diffInMillies = Math.abs(secondDate.getTime() - firstDate.getTime());
long diff = TimeUnit.DAYS.convert(diffInMillies, TimeUnit.MILLISECONDS);
Nandu Raj
  • 2,072
  • 9
  • 20