/**
* @author lautturi.com
* Java example: calculate the number of weeks between two dates in java
*/
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.text.ParseException;
import java.text.SimpleDateFormat;
public class Lautturi {
public static void main(String[] args) throws ParseException {
SimpleDateFormat myFormat = new SimpleDateFormat("yyyy-MM-dd");
String startData = "2020-01-23";
String endData = "2020-05-01";
Date startDate = myFormat.parse(startData);
Date endDate = myFormat.parse(endData);
long datediff = endDate.getTime() - startDate.getTime(); // calculate the milliseconds between two dates in java
int weeks = (int)(TimeUnit.DAYS.convert(datediff, TimeUnit.MILLISECONDS)/7);
System.out.println("weeks: " +weeks);
}
}
output:
weeks: 14