以字符串格式计算两个日期之间的日期差异

时间:2022-01-27 16:26:58

I have two dates in string format.I need to get the difference between these two dates in days.How can I get it?I am very new to these date format.please give me any suggestion.

我有两个日期的字符串格式。我需要在几天内得到这两个日期之间的差异。我怎么能得到它?我对这些日期格式很新。请给我任何建议。

2017-06-13
2017-06-27

    String newDate = null;
    Date dtDob = new Date(GoalSelectionToDate);
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    newDate = sdf.format(dtDob);

    String newDate1 = null;
    SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
    newDate1 = sdf1.format(currentDate);
    System.out.println("currentdateformat"+newDate1);
    System.out.println("anotherdateformat"+newDate);

2 个解决方案

#1


0  

see below

import java.time.LocalDate;
import java.time.Period;

public class DatesComparison {

    public static void main(String[] args) {
        String date1= "2017-06-13";
        String date2= "2017-06-27";




        LocalDate localDate1 = LocalDate.parse(date1);
        LocalDate localDate2 = LocalDate.parse(date2);

        Period intervalPeriod = Period.between(localDate1, localDate2);

        System.out.println("Difference of days: " + intervalPeriod.getDays());  // Difference of days: 14
        System.out.println("Difference of months: " + intervalPeriod.getMonths());  // Difference of months: 0
        System.out.println("Difference of years: " + intervalPeriod.getYears());  // Difference of years: 0
    }
}

#2


1  

If you are using Java 8, you can parse the dates to LocalDates without the need for a formatter because they are in ISO format:

如果您使用的是Java 8,则无需格式化程序即可将日期解析为LocalDates,因为它们采用ISO格式:

LocalDate start = LocalDate.parse("2017-06-13");
LocalDate end = LocalDate.parse("2017-06-27");

Then you can calculate the number of days between them with using a ChronoUnit:

然后,您可以使用ChronoUnit计算它们之间的天数:

long days = ChronoUnit.DAYS.between(start, end);

#1


0  

see below

import java.time.LocalDate;
import java.time.Period;

public class DatesComparison {

    public static void main(String[] args) {
        String date1= "2017-06-13";
        String date2= "2017-06-27";




        LocalDate localDate1 = LocalDate.parse(date1);
        LocalDate localDate2 = LocalDate.parse(date2);

        Period intervalPeriod = Period.between(localDate1, localDate2);

        System.out.println("Difference of days: " + intervalPeriod.getDays());  // Difference of days: 14
        System.out.println("Difference of months: " + intervalPeriod.getMonths());  // Difference of months: 0
        System.out.println("Difference of years: " + intervalPeriod.getYears());  // Difference of years: 0
    }
}

#2


1  

If you are using Java 8, you can parse the dates to LocalDates without the need for a formatter because they are in ISO format:

如果您使用的是Java 8,则无需格式化程序即可将日期解析为LocalDates,因为它们采用ISO格式:

LocalDate start = LocalDate.parse("2017-06-13");
LocalDate end = LocalDate.parse("2017-06-27");

Then you can calculate the number of days between them with using a ChronoUnit:

然后,您可以使用ChronoUnit计算它们之间的天数:

long days = ChronoUnit.DAYS.between(start, end);