-2

am getting incorrect results whatever I do. Could anyone give me some advise please :).

I posted the question before but I didn't get any answers and I would very happily want an answer.

Here is the code of my program the is responsible for getting the time between two dates.

I am getting a result but the problem is that it is incorrect and I have having trouble finding the problem.

I have to Joda library in my project.

if (timerightnow.isSelected()) {
            DateFormat getdate = new SimpleDateFormat("dd/MM/yy HH:mm:ss");
            Date getdate1 = new Date(); 

            String datenow = getdate1.toString();

            String monthnow = datenow.substring(4, 7);
            String daynow = datenow.substring(8,10);
            String hournow = datenow.substring(11,19);
            String yearnow = datenow.substring(25, 29);

            if (monthnow.equalsIgnoreCase("jan"))
                monthnow = "01";
            if (monthnow.equalsIgnoreCase("feb"))
                monthnow = "02";
            if (monthnow.equalsIgnoreCase("mar"))
                monthnow = "03";
            if (monthnow.equalsIgnoreCase("apr"))
                monthnow = "04";
            if (monthnow.equalsIgnoreCase("may"))
                monthnow = "05";
            if (monthnow.equalsIgnoreCase("jun"))
                monthnow = "06";
            if (monthnow.equalsIgnoreCase("jul"))
                monthnow = "07";
            if (monthnow.equalsIgnoreCase("agu"))
                monthnow = "08";
            if (monthnow.equalsIgnoreCase("sep"))
                monthnow = "09";
            if (monthnow.equalsIgnoreCase("oct"))
                monthnow = "10";
            if (monthnow.equalsIgnoreCase("nov"))
                monthnow = "11";
            if (monthnow.equalsIgnoreCase("dec"))
                monthnow = "12";

            String timenow = monthnow + "/" + daynow + "/" + yearnow + " " + hournow;
            String timetotext = timeto.getText();
            SimpleDateFormat date = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");


            Date datestart = null;
            Date dateend = null;



             try {

                 datestart = date.parse(timenow);
                 dateend = date.parse(timetotext);

                 DateTime datestartdaytime = new DateTime(datestart);
                 DateTime dateenddaytime = new DateTime(dateend);

                 JOptionPane.showMessageDialog(null, datestartdaytime + "\n" + dateenddaytime);

                int monthoutput = Months.monthsBetween(datestartdaytime, dateenddaytime).getMonths();
                int daysoutput = Days.daysBetween(datestartdaytime, datestartdaytime).getDays() % 30;
                int hoursoutput = Hours.hoursBetween(datestartdaytime, datestartdaytime).getHours() % 24;
                int minutsoutput = Minutes.minutesBetween(datestartdaytime, dateenddaytime).getMinutes() % 60;
                int secondsoutput = Seconds.secondsBetween(datestartdaytime, dateenddaytime).getSeconds() % 60;

                answer.setText(monthoutput + "Months" + daysoutput + "Days" + hoursoutput + "Hours" + minutsoutput + "Minutes" + secondsoutput + "Seconds");

Thanks a lot :)

AyhamSYR
  • 13
  • 4
  • **Search Stack Overflow before posting**. You would have seen countless examples of the parsing facility built into Joda-Time with no need for you to disassemble strings. – Basil Bourque Oct 16 '18 at 17:39
  • 1
    This code is too off track to give a good answer. For starters,```Date``` is NOT a Joda object. Mixing java time a joda time I would say is not a good approach. As the answer below states Joda has the parsing login for Instant/DateTime built it. Also, it looks like you are trying to represent a period. I strongly suggest looking at joda's Duration and Period objects as well as it's PeriodFormat/ter objects for formatting/printing. Best of luck. – Rob C Oct 16 '18 at 17:39
  • Please give it time. Good answers are sometimes posted *days* after the question was asked. – Ole V.V. Oct 17 '18 at 05:47

1 Answers1

1

I didn't really understand what is it that you're trying to achieve. But you should definetly not do the parsing logic yourself, as it is already present in JodaTime.

This should solve the parsing problem:

DateTimeFormatter formatter = DateTimeFormat.forPattern("dd/MM/yy HH:mm:ss");
DateTime dt = formatter.parseDateTime(string);

Give more details on what you want if this doesn't solve it for you.

Hope it helps!

Tomaz Fernandes
  • 2,429
  • 2
  • 14
  • 18