0

I am trying to create a little application which holds the data of licenses, I allow the user to input the date it was the redeemed and the license length.

I want to create a day's remaining field where the user can view how many days they have left until they have redeemed the new license.

I've used jxDatepicker

and have three combo boxes where the user selects the number of years, months and days the license is available for

int days = Integer.parseInt(cboDays.getSelectedItem().toString());
Date dRedeemed = cboDate.getDate();
SimpleDateFormat format = new SimpleDateFormat("dd/MM/YYYY");
String strRedeemed = format.format(dRedeemed);
days = (years * 365) + (months * 12) + days;

EDIT

I think i have left some people quite confused. That or i don't understand how this would fix my problem. Ill try and describe in a little more detail

Scenario

User can record their license using the application, they enter the day the license was redeemed and they also input the length of the license(days).

I am trying to figure out if you are able to calculate a new date from both these variables. for example

days = INPUT FROM USER;    
dateRedeemed = new Date();   
newDate = dateRedeemed + days;
Ryan MacPhee
  • 73
  • 1
  • 1
  • 8
  • Could you post what you have tried so far (in java) apart from GUI stuff? – deHaar Jun 24 '19 at 10:18
  • Similar question regarding finding the difference between two dates: https://stackoverflow.com/questions/5351483/calculate-date-time-difference-in-java – JG7 Jun 24 '19 at 10:19
  • @JG7 - I dont think this link will help, I only have the one Date field and then a user entry for numbers of days the license is available for. I am trying to essentially trying to do this >>>>> Today (24/06/2019) + 700 days = newDate <<<<<< but i dont know how to even go about doing this – Ryan MacPhee Jun 24 '19 at 10:28
  • 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. Jun 24 '19 at 11:52
  • Just saw that Eduardo Eljaiek has written a brand new and modern answer for your [here](https://stackoverflow.com/a/56739994/5772882). Amazing. – Ole V.V. Jun 25 '19 at 09:07

3 Answers3

2

Assuming you are receiving the license-deadline somehow before (via database or similar), you can utilize the java.time API in order to calculate the remaining days until deadline with this little example:

import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.time.format.DateTimeFormatter;

public class StackoverflowMain {

    public static void main(String args[]) {
        // define a future deadline (hint: define some others for your test cases, too)
        LocalDate deadLine = LocalDate.parse("2019-06-29", DateTimeFormatter.ISO_DATE);
        System.out.println("Days til deadline: " + getRemainingDays(deadLine));
    }

    public static long getRemainingDays(LocalDate deadline) {
        return ChronoUnit.DAYS.between(LocalDate.now(), deadline);
    }
}

Please note the use of the modern API, which is recommended to use nowadays.

EDIT

For the case you don't want to refactor your entire code, here are two methods that handle the conversion from java.time.LocalDate to java.util.Date and back:

    public LocalDate toLocalDate(Date dateToConvert) {
        return dateToConvert.toInstant()
                .atZone(ZoneId.systemDefault())
                .toLocalDate();
    }

    public static Date toDate(LocalDate localDate) {
        return Date.from(
                localDate.atStartOfDay()
                    .atZone(ZoneId.systemDefault())
                    .toInstant()
                );
    }
deHaar
  • 17,687
  • 10
  • 38
  • 51
0
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy", Locale.ENGLISH);
Date firstDate = new Date();
Date secondDate = null;
secondDate = sdf.parse("06/30/2019");
long diffInMillies = Math.abs(secondDate.getTime() - firstDate.getTime());
long diff = TimeUnit.DAYS.convert(diffInMillies, TimeUnit.MILLISECONDS);

This gives you answer in days.

0

You can use this simple method two find out the difference between Two dates


public void printDifference(Date startDate, Date endDate) { 
    long different = endDate.getTime() - startDate.getTime();
    System.out.println("startDate : " + startDate);
    System.out.println("endDate : "+ endDate);
    System.out.println("different : " + different);

    long secondsInMilli = 1000;
    long minutesInMilli = secondsInMilli * 60;
    long hoursInMilli = minutesInMilli * 60;
    long daysInMilli = hoursInMilli * 24;

    long elapsedDays = different / daysInMilli;
    different = different % daysInMilli;

    long elapsedHours = different / hoursInMilli;
    different = different % hoursInMilli;

    long elapsedMinutes = different / minutesInMilli;
    different = different % minutesInMilli;

    long elapsedSeconds = different / secondsInMilli;

    System.out.printf(
        "%d days, %d hours, %d minutes, %d seconds%n", 
        elapsedDays, elapsedHours, elapsedMinutes, elapsedSeconds);  }

In the Above method, you can pass the first parameter as the current date and second parameter expiry date of license

For more refer answer of this question Android difference between Two Dates