Java 使用公历与当前日期进行比较,无需外部类
我应该询问用户的年龄,作为回报,我应该检查他们已经活了多少天。我试过在这里搜索,但不知道怎么做 条件:方法,该方法将GregoriaCalendar日期作为参数,并返回该日期与今天之间的天数差我不能为此使用任何外部类。 以下是我未完成的代码:Java 使用公历与当前日期进行比较,无需外部类,java,calendar,Java,Calendar,我应该询问用户的年龄,作为回报,我应该检查他们已经活了多少天。我试过在这里搜索,但不知道怎么做 条件:方法,该方法将GregoriaCalendar日期作为参数,并返回该日期与今天之间的天数差我不能为此使用任何外部类。 以下是我未完成的代码: public static void main(String[] args) { int day; int month; int year; int difference; Scanner scanIn = new
public static void main(String[] args) {
int day;
int month;
int year;
int difference;
Scanner scanIn = new Scanner(System.in);
System.out.println("Please enter your birth date.");
System.out.println("Please enter the month you were born in (mm)");
month = scanIn.nextInt();
System.out.println("Please enter the day you were born in (dd)");
day = scanIn.nextInt();
System.out.println("Please enter the year you were born in (yyyy)");
year = scanIn.nextInt();
GregorianCalendar greg = new GregorianCalendar(year, month, day);
difference = getDifference(greg);
month = greg.get(Calendar.MONTH) + 2;
year = greg.get(Calendar.YEAR);
//System.out.println("Year is " + year);
//System.out.println("Month " + month);
}
public static int getDifference(GregorianCalendar greg) {
Calendar now = new GregorianCalendar();
int difference;
System.currentTimeMillis.greg();
difference = greg - now;
return difference;
}
我不明白怎样才能得到区别
我的导师建议我们用毫秒来解决这个问题,但我已经花了4个小时试图解决这个问题,我不明白这一点
如何获取当前日期、月份和年份?建议使用Java 8中的而不是
日历API。特别是,您可以使用所述的方法ChronoUnits.DAYS.between()
。我建议:
LocalDate dob = LocalDate.of(year, month, day);
LocalDate now = LocalDate.now();
difference=(int)ChronoUnit.DAYS.between(dob, now);
System.out.println("Days since date of birth: " + difference);
您可以轻松检查is是否提供与旧界面相同的结果:
GregorianCalendar greg = new GregorianCalendar(year, month-1, day);
Calendar nowCal = new GregorianCalendar();
long deltaMillis = nowCal.getTime().getTime() - greg.getTime().getTime();
System.out.println("Days since date of birth: " + deltaMillis/(1000*60*60*24));
请注意,Calendar
接口从0开始计算月份,其中新API从1开始
我希望它能有所帮助。建议使用Java 8中的,而不是日历
API。特别是,您可以使用所述的方法ChronoUnits.DAYS.between()
。我建议:
LocalDate dob = LocalDate.of(year, month, day);
LocalDate now = LocalDate.now();
difference=(int)ChronoUnit.DAYS.between(dob, now);
System.out.println("Days since date of birth: " + difference);
您可以轻松检查is是否提供与旧界面相同的结果:
GregorianCalendar greg = new GregorianCalendar(year, month-1, day);
Calendar nowCal = new GregorianCalendar();
long deltaMillis = nowCal.getTime().getTime() - greg.getTime().getTime();
System.out.println("Days since date of birth: " + deltaMillis/(1000*60*60*24));
请注意,Calendar
接口从0开始计算月份,其中新API从1开始
我希望它能有所帮助。如果您只能使用GregoriaCalendar,一种方法是通过getTimeInMillis
将日历的两个实例转换为它们的毫秒表示形式,减去它们,然后通过将其除以(24*60*60*1000)来计算表示的天数
例如:
public static int countDaysSince(GregorianCalendar pastDate) {
GregorianCalendar now = GregorianCalendar.getInstance();
long difference = pastDate.getTimeInMillis() - now.getTimeInMillis();
return difference / (24 * 60 * 60 * 1000); // 24 hours, 60 minutes, 60 seconds, 1000 milliseconds
}
这应该可以做到。如果您只能使用GregoriaCalendar,一种方法是通过getTimeInMillis
将日历的两个实例转换为它们的毫秒表示形式,减去它们,然后通过将其除以(24*60*60*1000)来计算表示的天数
例如:
public static int countDaysSince(GregorianCalendar pastDate) {
GregorianCalendar now = GregorianCalendar.getInstance();
long difference = pastDate.getTimeInMillis() - now.getTimeInMillis();
return difference / (24 * 60 * 60 * 1000); // 24 hours, 60 minutes, 60 seconds, 1000 milliseconds
}
应该可以了。日历不推荐了吗?查看一下Java8的日历API。不,你说得对,谢谢。虽然java.util.Date/.Calendar类没有被正式弃用,但是java.util.Date/.Calendar类的设计很差,现在是遗留类,被java.time类取代。甲骨文解释说,我很惊讶它没有被正式否决。知道为什么吗?在Java 9中它会被弃用吗?日历会被弃用吗?查看一下Java8的日历API。不,你说得对,谢谢。虽然java.util.Date/.Calendar类没有被正式弃用,但是java.util.Date/.Calendar类的设计很差,现在是遗留类,被java.time类取代。甲骨文解释说,我很惊讶它没有被正式否决。知道为什么吗?在Java 9中它会被弃用吗?请在发布前搜索堆栈溢出。请在发布前搜索堆栈溢出。