Calculate age in Years, Months, Days, Hours, Minutes, and Seconds

后端 未结 4 1505
臣服心动
臣服心动 2020-12-21 02:04

I need to take a birthday entered by the user (preferably in dd/mm//yyyy format) and find their age, based on todays date. Could someone explain to me the proce

4条回答
  •  没有蜡笔的小新
    2020-12-21 02:46

    public class Main {
        public static void main(String[] args) {
        LocalDateTime dateTime = LocalDateTime.of(2018, 12, 27, 11, 45, 0);
                Duration showSeconds = AgeCalculator.calculateAgeDuration(dateTime, LocalDateTime.now());
                TimeConverter.calculateTime(showSeconds.getSeconds());
        }
    }
    
    public class AgeCalculator {
        public static Duration calculateAgeDuration(LocalDateTime dayBefore, LocalDateTime currentDay) {
                return Duration.between(dayBefore, currentDay);
        }
    }
    
    public class TimeConverter {
        public static void calculateTime(long timeSeconds) {
            long days = timeSeconds / 86400; // 24*60*60
            long hours = timeSeconds / 3600;
            long minutes = (timeSeconds % 3600) / 60;
            long seconds = (timeSeconds % 3600) % 60;
    
            System.out.println("Days: " + days);
            System.out.println("Hours: " + hours);
            System.out.println("Minutes: " + minutes);
            System.out.println("Seconds: " + seconds);
        }
    }
    

    Days: 0 Hours: 4 Minutes: 30 Seconds: 29

提交回复
热议问题