Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/183.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何正确计算两个日期之间的实际月数?_Java_Android_Date - Fatal编程技术网

Java 如何正确计算两个日期之间的实际月数?

Java 如何正确计算两个日期之间的实际月数?,java,android,date,Java,Android,Date,我采用了方法getDiffDateMap,该方法计算两个日期之间的差异,并返回分别表示毫秒、秒、分钟、小时、天、月和年的整数的Map public static Map<Integer, String> getDiffDateMap(String dateA, String dateB) { Calendar cal = Calendar.getInstance(); Map<Integer,String> out = new LinkedH

我采用了方法
getDiffDateMap
,该方法计算两个日期之间的差异,并返回分别表示毫秒、秒、分钟、小时、天、月和年的整数的
Map

public static Map<Integer, String> getDiffDateMap(String dateA, String dateB) {

    Calendar cal = Calendar.getInstance();      
    Map<Integer,String> out = new LinkedHashMap<Integer, String>();
    long timeInMillA = 0;
    long timeInMillB = 0;

    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 

    Date convertedDateA;
    Date convertedDateB;



    try {
        convertedDateA = dateFormat.parse(dateA);           
        cal.setTime(convertedDateA);
        timeInMillA = cal.getTimeInMillis();


        convertedDateB = dateFormat.parse(dateB);           
        cal.setTime(convertedDateB);
        timeInMillB = cal.getTimeInMillis();

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

    long mili = timeInMillB - timeInMillA;
    long sec = mili/1000;
    long min = sec/60;
    long hour = min/60;
    long day = hour/24;
    long week = day/7;
    long month = day/31; // ????
    long year = month/12;

    out.put(7, mili + "");
    out.put(6, sec + "");
    out.put(5, min + "");
    out.put(4, hour + "");
    out.put(3, day + "");
    out.put(2, week + "");
    out.put(1, month + "");
    out.put(0, year + "");

    return out;
}
例如:

Map<Integer,String> out = getDiffInMillsec("2012-9-01 20:9:01", "2012-10-01 20:10:01");

    System.out.println(Arrays.asList(out)); 
LocalDateTime start = ...;
LocalDateTime end = ...;
Period difference = new Period(start, end, PeriodType.yearMonthDayTime());
int months = difference.getMonths(); // etc
Map out=getDiffInMillsec(“2012-9-01 20:9:01”、“2012-10-01 20:10:01”);
System.out.println(Arrays.asList(out));
我得到输出:
[{7=259206000,6=2592060,5=43201,4=720,3=30,2=4,1=0,0=0}]
其中
1
是月份计数及其0。因为相差只有30天。我需要添加什么流来解决此问题?有什么建议吗

我遵循了getDiffDateMap方法,该方法计算两个日期之间的差异,并返回分别表示毫秒、秒、分钟、小时、天、月和年的整数映射

不要重新发明轮子:)

有代码来完成所有这些和更多。例如:

Map<Integer,String> out = getDiffInMillsec("2012-9-01 20:9:01", "2012-10-01 20:10:01");

    System.out.println(Arrays.asList(out)); 
LocalDateTime start = ...;
LocalDateTime end = ...;
Period difference = new Period(start, end, PeriodType.yearMonthDayTime());
int months = difference.getMonths(); // etc
请注意,当您刚刚将“不同”转换为毫秒数时,无法获得月数,因为月数将取决于开始/结束日期。(例如,30天可能是一个月,也可能不是一个月……)

我强烈建议您在整个Java代码中使用Joda时间,而不是
Java.util.*
。这是一个更好的API,希望这意味着您很少需要编写自己的日期/时间处理代码。

我建议您使用它

这具有以下功能:

    static Months   monthsBetween(ReadableInstant start, ReadableInstant end) 
创建一个月,表示两个指定日期时间之间的整月数

    static Months   monthsBetween(ReadablePartial start, ReadablePartial end) 

创建一个月,表示两个指定的部分日期时间之间的整月数。

我只想总结一下我们之前讨论过的所有内容,在Jon Skeet的帮助下,下面是一个答案,我使用了
JodaTime
新期间
每个日期值:

    static Months   monthsBetween(ReadablePartial start, ReadablePartial end) 
import org.joda.time.LocalDateTime;
import org.joda.time.Period;
import org.joda.time.PeriodType;

....
    public static Map<Integer, String> getDateTimeDiffMap(String dateA, String dateB) {

    Calendar cal = Calendar.getInstance();
    Map<Integer,String> out = new LinkedHashMap<Integer, String>();

    long timeInMillA = 0;
    long timeInMillB = 0;

    SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT); 

    Date convertedDateA;
    Date convertedDateB;

    try {
    convertedDateA = dateFormat.parse(dateA);           
    cal.setTime(convertedDateA);
    timeInMillA = cal.getTimeInMillis();


    convertedDateB = dateFormat.parse(dateB);           
    cal.setTime(convertedDateB);
    timeInMillB = cal.getTimeInMillis();

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


    LocalDateTime startA = new LocalDateTime(timeInMillA);
    LocalDateTime startB = new LocalDateTime(timeInMillB);

    Period difference = new Period(startA, startB, PeriodType.days());
    int day = difference.getDays();

    difference = new Period(startA, startB, PeriodType.months());
    int month = difference.getMonths();

    difference = new Period(startA, startB, PeriodType.years());
    int year = difference.getYears();

    difference = new Period(startA, startB, PeriodType.weeks());
    int week = difference.getWeeks();

    difference = new Period(startA, startB, PeriodType.hours());
    int hour = difference.getHours();

    difference = new Period(startA, startB, PeriodType.minutes());
    long min = difference.getMinutes();

    difference = new Period(startA, startB, PeriodType.seconds());
    long sec = difference.getSeconds();

    //difference = new Period(startA, startB, PeriodType.millis());
    long mili = timeInMillB - timeInMillA;  


    out.put(7, mili + "");
    out.put(6, sec + "");
    out.put(5, min + "");
    out.put(4, hour + "");
    out.put(3, day + "");
    out.put(2, week + "");
    out.put(1, month + "");
    out.put(0, year + "");      

    return out;
}

谢谢,
请注意,当您刚刚将不同转换为毫秒数时,您无法计算月数
是的,我知道。我正在寻找不使用额外的
jar
(android)的方法。在我的代码中,我仍然有
日期转换日期a
日期转换日期b
@Fess:如果您使用的是
java.util.Date
,那么首先需要确定您所在的时区。但真的,请不要仅仅为了避免依赖而重新发明这个轮子。日期和时间的计算很难准确可靠——使用Joda time,您的代码也会变得更加清晰。好的,如果我还没有找到其他方法,我会使用Joda time。谢谢,我看不到任何带有
周期(开始、结束,
)的解决方案。例如
“01-09-2012 20:9:01”、“01-10-2012 20:9:01”
作为开始和结束,我预计1个月,30天,720小时,但实际上我有1个月,0天,0小时,0分钟…@Fess:为什么你会预计1个月和30天?这两个日期正好相隔一个月!如果你试图获取总数,那么你应该多次调用新周期,每次调用一个单位PeriodType。你的字符串示例s不一致。第一个的分钟数是一个不带零的单个数字,而其他1-9个值有前导零。