-2

I need to calculate the difference between 2 dates in days..which first date should be the current date and the next one is already has stroed in Mysql database ..

Mysql date format of the stored date is yyyy-MM-dd

And I just took the stored date through a textfield So please help me solve this !

jarlh
  • 42,561
  • 8
  • 45
  • 63
Mna_ofcl
  • 3
  • 2
  • I'm voting to close this question as off-topic because "Questions asking for homework help must include a summary of the work you've done so far to solve the problem, and a description of the difficulty you are having solving it". – skomisa Oct 14 '19 at 11:53
  • Actually mate this is not for a home work.. one of my cousin developing a system to a football club so that he want to code something for trnasafering players... he asked me to help so I came to stack overflow and accidentally met you!...thank you for the comments – Mna_ofcl Oct 15 '19 at 05:11
  • You are expected to demonstrate some research effort before posting here regardless of whether it is homework. Stack Overflow is not a Help forum. If you just want to specify work that you want someone else to do for you then this is probably not the best place to ask. See [How do I ask a good question?](https://stackoverflow.com/help/how-to-ask) for more information. – skomisa Oct 15 '19 at 06:33
  • Yeps... appreciate your comment.. I found a way to my home work.. thanks – Mna_ofcl Oct 15 '19 at 07:22

2 Answers2

1

java.time

If you stored the date as text, parse as LocalDate.

LocalDate localDate = LocalDate.parse( input ) ;

If you more properly stored the date in a column of a type akin to the SQL-standard DATE type, retrieve as a LocalDate.

LocalDate localDate = myResultSet.getObject( … , LocalDate.class ) ;

Capture the current date as seen in the wall-clock time used by the people of some particular region (a time zone).

ZoneId z = ZoneId.of( "America/Montreal" ) ;

LocalDate today = LocalDate.now( z ) ;

Calculate leaped time in days.

long days = ChronoUnit.DAYS.between( localDate , today ) ;

Table of date-time types in Java (both legacy and modern) and in standard SQL

Tip: To represent a date range, a pair of LocalDateobjects, add the ThreeTen-Extra library to your project. Use the LocalDateRange class.

LocalDateRange range = LocalDateRange.of( localDate , today ) ;

About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.* classes.

Where to obtain the java.time classes?

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
0
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.ZoneId;
import java.util.Date;

 private void CountDaysActionPerformed(java.awt.event.ActionEvent evt) {                                          

        //check your zone Id and place it on the double quote 
        ZoneId z=ZoneId.of("Asia/Colombo");
        LocalDate today=LocalDate.now(z);

        String currentDate=today.toString();

        SimpleDateFormat myformat=new SimpleDateFormat("yyyy-MM-dd");

        String firstDate1 =jTextField1.getText();
        String secondDate1 =currentDate;

        try{

            Date firstDate = myformat.parse(firstDate1);
            Date secondDate = myformat.parse(secondDate1);

            long dif = secondDate.getTime()- firstDate.getTime();

            int daysBetween =(int) (dif/(1000*60*60*24));


            //answer wil be the difference between your current date and the date you provided in the jTextField1
            System.out.println(daysBetween);


        }catch(Exception e){
            e.printStackTrace();
        }


    }
Mna_ofcl
  • 3
  • 2