1 Star 0 Fork 0

schwarizard / Chapter 13

Create your Gitee Account
Explore and code with more than 14 million developers,Free private repositories !:)
Sign up
文件
This repository doesn't specify license. Please pay attention to the specific project description and its upstream code dependency when using it.
Clone or Download
CalendarUtil 1022 Bytes
Copy Edit Raw Blame History
schwarizard authored 2016-04-17 23:54 +08:00 . new file
import static java.lang.System.out;
import java.util.Calendar;
public class CalendarUtil {
public static void main(String[] args) {
Calendar birth = Calendar.getInstance();
birth.set(1996, Calendar.JANUARY, 7);
Calendar now = Calendar.getInstance();
out.printf("岁数:%d%n", yearsBetween(birth, now));
out.printf("天数:%d%n", daysBetween(birth, now));
}
public static long yearsBetween(Calendar begin, Calendar end) {
Calendar calendar = (Calendar) begin.clone();
long years = 0;
while(calendar.before(end)) {
calendar.add(Calendar.YEAR, 1);
years++;
}
return years - 1;
}
public static long daysBetween(Calendar begin, Calendar end) {
Calendar calendar = (Calendar) begin.clone();
long days = 0;
while(calendar.before(end)) {
calendar.add(Calendar.DATE, 1);
days++;
}
return days - 1;
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Java
1
https://gitee.com/schwarizard/Chapter-13.git
git@gitee.com:schwarizard/Chapter-13.git
schwarizard
Chapter-13
Chapter 13
master

Search