| |

VerySource

 Forgot password?
 Register
Search
View: 889|Reply: 7

Find the number of days between two dates (online, etc.)

[Copy link]

1

Threads

3

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-1-2 21:10:01
| Show all posts |Read mode
There are 2 times now
t1 = Mon Jan 01 00:00:00 CST 2007
t2 = Sun Dec 31 15:28:52 CST 2006
How to find the number of days between these two dates?
Or I want to judge whether t2 is within 2 years before t1. How can I judge?
Thank you! !! !!
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-1-2 21:54:02
| Show all posts
/ **
* Returns the number of days between two dates
* @param beginDate
* @param endDate
* @return days
* /
public long getBetweenDays (Date beginDate, Date endDate) {

long beginTime = beginDate.getTime ();

long endTime = endDate.getTime ();

long betweendays = (long) ((endTime-beginTime)
/ (1000 * 60 * 60 * 24) + 0.5);
Ranch
return betweendays;
}
Reply

Use magic Report

1

Threads

3

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

 Author| Post time: 2020-1-2 22:09:01
| Show all posts
So how to judge whether 2 is within 2 years before t1? Thank you
Reply

Use magic Report

0

Threads

12

Posts

11.00

Credits

Newbie

Rank: 1

Credits
11.00

 China

Post time: 2020-1-2 23:48:01
| Show all posts
boolean before (Date when)
boolean after (Date when)
Reply

Use magic Report

1

Threads

3

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

 Author| Post time: 2020-1-3 17:36:01
| Show all posts
Upstairs, can you speak more clearly?
Reply

Use magic Report

0

Threads

3

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

Post time: 2020-1-3 22:00:01
| Show all posts
Date.compareTo (date)
Reply

Use magic Report

1

Threads

4

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-1-6 14:57:01
| Show all posts
public static long getOutWrokDays (String time1, String time2)
   {
      long days = 0;
      SimpleDateFormat ft = new SimpleDateFormat ("yyyy-MM-dd");
      try {
            Date date1 = ft.parse (time1);
            Date date2 = ft.parse (time2);
            days = date1.getTime ()-date2.getTime ();
            days = days / 1000/60/60/24;
          } catch (ParseException e)
          {
            e.printStackTrace ();
          }
      return days;
   }
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-8-21 22:15:01
| Show all posts
1.public int diffDate(Calendar date1, Calendar date2) {
return diffDate(date1.getTime(), date2.getTime());
}

2.public int diffDate(java.util.Date date1, java.util.Date date2) {
return (int) ((date1.getTime()-date2.getTime()) / (24 * 60 * 60 * 1000));
}
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list