반응형
JAVA JDK 1.8에서 테스트 했습니다.
A 2020년 7월 7일, B 2020년 7월 9일 두 개의 날짜 차이 계산
package com.main;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.concurrent.TimeUnit;
public class DateTest {
public static void main(String[] args) {
try {
String a = "20200707";
String b = "20200709";
String format = "yyyyMMdd";
SimpleDateFormat sdf = new SimpleDateFormat(format, Locale.ENGLISH);
Date firstDate = sdf.parse(a);
Date secondDate = sdf.parse(b);
long diffInMillies = Math.abs(secondDate.getTime() - firstDate.getTime());
long diff = TimeUnit.DAYS.convert(diffInMillies, TimeUnit.MILLISECONDS);
System.out.println(String.format("A %s , B %s Diff %s Days", a, b, diff));
} catch (Exception e) {
e.printStackTrace();
}
}
}
실행 결과
응용
A 일자와 B일자 개월 및 일수 차이 계산 하기
package com.main;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.util.Date;
import java.util.Locale;
public class DateTest {
public static void main(String[] args) {
String logFormat = "%s-%s-%s, %s-%s-%s Diff Momth %s, Diff Days %s";
// String dateFormat = "yyyy-MM-dd";
String strA = "2020-06-15";
// LocalDate a = LocalDate.of(2020, 6, 15);
LocalDate a = LocalDate.parse(strA);
LocalDate b = LocalDate.of(2020, 7, 14);
long monthDiff = ChronoUnit.MONTHS.between(a, b);
// long dayDiff = getDayDiff(strA, "2020-07-14", dateFormat);
long dayDiff = getDayDiff2(strA, "2020-07-14");
System.out.println(String.format(logFormat, a.getYear(), a.getMonthValue(), a.getDayOfMonth(), b.getYear(), b.getMonthValue(), b.getDayOfMonth() , monthDiff, dayDiff));
System.out.println("----");
LocalDate c = LocalDate.of(2020, 7, 15);
long monthDiff2 = ChronoUnit.MONTHS.between(a, c);
// long dayDiff2 = getDayDiff(strA, "2020-07-15", dateFormat);
long dayDiff2 = getDayDiff2(strA, "2020-07-15");
System.out.println(String.format(logFormat, a.getYear(), a.getMonthValue(), a.getDayOfMonth(), c.getYear(), c.getMonthValue(), c.getDayOfMonth() , monthDiff2, dayDiff2));
System.out.println("----");
LocalDate d = LocalDate.of(2020, 7, 16);
long monthDiff3 = ChronoUnit.MONTHS.between(a, d);
// long dayDiff3 = getDayDiff(strA, "2020-07-16", dateFormat);
long dayDiff3 = getDayDiff2(strA, "2020-07-16");
System.out.println(String.format(logFormat, a.getYear(), a.getMonthValue(), a.getDayOfMonth(), d.getYear(), d.getMonthValue(), d.getDayOfMonth() , monthDiff3, dayDiff3));
System.out.println("----");
LocalDate e = LocalDate.of(2020, 8, 14);
long monthDiff4 = ChronoUnit.MONTHS.between(a, e);
// long dayDiff4 = getDayDiff(strA, "2020-08-14", dateFormat);
long dayDiff4 = getDayDiff2(strA, "2020-08-14");
System.out.println(String.format(logFormat, a.getYear(), a.getMonthValue(), a.getDayOfMonth(), e.getYear(), e.getMonthValue(), e.getDayOfMonth() , monthDiff4, dayDiff4));
System.out.println("----");
LocalDate f = LocalDate.of(2020, 8, 15);
long monthDiff5 = ChronoUnit.MONTHS.between(a, f);
// long dayDiff5 = getDayDiff(strA, "2020-08-15", dateFormat);
long dayDiff5 = getDayDiff2(strA, "2020-08-15");
System.out.println(String.format(logFormat, a.getYear(), a.getMonthValue(), a.getDayOfMonth(), f.getYear(), f.getMonthValue(), f.getDayOfMonth() , monthDiff5, dayDiff5));
System.out.println("----");
LocalDate g = LocalDate.of(2020, 8, 16);
long monthDiff6 = ChronoUnit.MONTHS.between(a, g);
// long dayDiff6 = getDayDiff(strA, "2020-08-16", dateFormat);
long dayDiff6 = getDayDiff2(strA, "2020-08-16");
System.out.println(String.format(logFormat, a.getYear(), a.getMonthValue(), a.getDayOfMonth(), g.getYear(), g.getMonthValue(), g.getDayOfMonth() , monthDiff6, dayDiff6));
System.out.println("----");
}
private static long getDayDiff2(String dayA, String dayB) {
LocalDate a = LocalDate.parse(dayA);
LocalDate b = LocalDate.parse(dayB);
long dayDiff = ChronoUnit.DAYS.between(a, b);
return dayDiff;
}
private static long getDayDiff(String a, String b, String format) {
long diff = 0;
try {
SimpleDateFormat sdf = new SimpleDateFormat(format, Locale.ENGLISH);
Date firstDate = sdf.parse(a);
Date secondDate = sdf.parse(b);
long diffInMillies = Math.abs(secondDate.getTime() - firstDate.getTime());
// diff = TimeUnit.DAYS.convert(diffInMillies, TimeUnit.MILLISECONDS);
// System.out.println(String.format("A %s , B %s Diff %s Days", a, b, diff));
} catch (Exception e) {
e.printStackTrace();
}
return diff;
}
}
실행 결과
반응형
'IT > 프로그래밍' 카테고리의 다른 글
Offset Pagination과 Cursor Pagination (0) | 2024.03.27 |
---|---|
File up & download HTML (0) | 2020.08.07 |
ANGULAR build Options [ 빌드 캐시 삭제 ] (0) | 2020.05.14 |
2020-03-17 [ readable-stream ] 오류 (0) | 2020.03.17 |
eclipse git reflog (0) | 2019.11.14 |