@Test public void testGetAge() { int age = DateTimeUtil.getAge(System.currentTimeMillis() + 100); System.out.println("age: " + age); }
public static int getAge(long ms) { int age; Calendar born = Calendar.getInstance(); Calendar now = Calendar.getInstance(); born.setTimeInMillis(ms); if (born.after(now)) { return -1; } age = now.get(Calendar.YEAR) - born.get(Calendar.YEAR); if (now.get(Calendar.DAY_OF_YEAR) < born.get(Calendar.DAY_OF_YEAR)) { age -= 1; } return age; }
DateTimeUtil { public static int getAge(long ms) { int age; Calendar born = Calendar.getInstance(); Calendar now = Calendar.getInstance(); born.setTimeInMillis(ms); if (born.after(now)) { return -1; } age = now.get(Calendar.YEAR) - born.get(Calendar.YEAR); if (now.get(Calendar.DAY_OF_YEAR) < born.get(Calendar.DAY_OF_YEAR)) { age -= 1; } return age; } }
DateTimeUtil { public static int getAge(long ms) { int age; Calendar born = Calendar.getInstance(); Calendar now = Calendar.getInstance(); born.setTimeInMillis(ms); if (born.after(now)) { return -1; } age = now.get(Calendar.YEAR) - born.get(Calendar.YEAR); if (now.get(Calendar.DAY_OF_YEAR) < born.get(Calendar.DAY_OF_YEAR)) { age -= 1; } return age; }  }
DateTimeUtil { public static int getAge(long ms) { int age; Calendar born = Calendar.getInstance(); Calendar now = Calendar.getInstance(); born.setTimeInMillis(ms); if (born.after(now)) { return -1; } age = now.get(Calendar.YEAR) - born.get(Calendar.YEAR); if (now.get(Calendar.DAY_OF_YEAR) < born.get(Calendar.DAY_OF_YEAR)) { age -= 1; } return age; }  static String dateToStr(Date date, String format); static String nowStrTime(); static String nowStrTime(String pattern); static String dateToStr(Date date, String pattern, Locale locale); static String sqlDateToStr(java.sql.Date date, String format); static Date strToDate(String sDate, String format); static Timestamp nowTimeStamp(); static Timestamp strToStamp(String date); static Timestamp strToStamp(String date, String format); static double calcRunTime(long beginTime, long endTime); static void printRunTime(long beganTime, long endTime); static Long getNowTime(); static long getDistanceOfTwoDate(Date before, Date after); static boolean isLeapYear(int year); static String timestampToString(Timestamp time, String fmt); static long setTimeToNextDay0H0M0S(Timestamp time); static long setTimeToNextDay0H0M0S(long millis); static long setToNextDay0H0M0SExceptToday(long millis); static long setToNextDay0H0M0SExceptToday(Timestamp stamp); static long setTimeTo0H0M0S(Timestamp time); static long setTimeTo0H0M0S(long millis); static void setTimeToLastDay0H0M0S(Timestamp time); static boolean isDifferentDay(Calendar calendar1, Calendar calendar2); static boolean isDifferentDay(Timestamp timestamp0, Timestamp timestamp1); static boolean isDifferentDay(long millis0, long millis1); static String long2Str(Long millSec); static String long2Str(long millSec, String format); static String long2Str(long millSec, String format, Locale locale); static long strToLong(String strTime, String pattern); static long todayPastMillisecond(long millions); static int getCurrentMonthDays(); static int getCurrentMonthDays(Timestamp stamp); static int getCurrentMonthDays(long ms); static int getCurrentMonthDays(Calendar cal); static long getFirstDayOfCurrentWeek(Timestamp stamp); static long getFirstDayOfCurrentWeek(long ms); static long setToFirstDayOfCurrentYear(long millis); static long setToFirstDayOfNextYear(long millis); static long setToFirstDayOfLastMonth(long ms); static long setToLastMonthCommonDay(long ms); static long setToFirstDayOfCurrentMonth(long millis); static long setToFirstDayOfNextMonth(long millis); static long setToNextYearCommonDay(long millis); static long setToLastYearCommonDay(long millis); static long getLastDayOfCurrentWeek(Timestamp stamp); static long getFirstDayOfCurrentQuarter(long ms); static long getFirstDayOfNextQuarter(long ms); static int getDayOfWeek(long ms); static long create0H0M0STime(); static boolean isToday(long ms); static String friendlyTime(long ms); static int getWeeks(long startTime, long endTime); static String getDateWithWeek(long ms); static String getDateWithWeekAndTime(long ms); static int getAge(long ms); static String getLastYearCommonDay(String strDate, String format); static String getLastMonthCommonDay(String strDate, String format); static Timestamp getBirthdayFormIdCard(String idCard); static long getLongBirthFormIdCard(String idCard); static boolean isCurrentYear(long ms); static String format(LocalDateTime localDateTime, String pattern); static long localDateToLong(LocalDate localDate); static LocalDate parseLocalDate(String time, String pattern); static LocalDateTime parseLocalDateTime(String time, String pattern); static long LocalDateTimeToLong(LocalDateTime dateTime); }
DateTimeUtil { public static int getAge(long ms) { int age; Calendar born = Calendar.getInstance(); Calendar now = Calendar.getInstance(); born.setTimeInMillis(ms); if (born.after(now)) { return -1; } age = now.get(Calendar.YEAR) - born.get(Calendar.YEAR); if (now.get(Calendar.DAY_OF_YEAR) < born.get(Calendar.DAY_OF_YEAR)) { age -= 1; } return age; }  static String dateToStr(Date date, String format); static String nowStrTime(); static String nowStrTime(String pattern); static String dateToStr(Date date, String pattern, Locale locale); static String sqlDateToStr(java.sql.Date date, String format); static Date strToDate(String sDate, String format); static Timestamp nowTimeStamp(); static Timestamp strToStamp(String date); static Timestamp strToStamp(String date, String format); static double calcRunTime(long beginTime, long endTime); static void printRunTime(long beganTime, long endTime); static Long getNowTime(); static long getDistanceOfTwoDate(Date before, Date after); static boolean isLeapYear(int year); static String timestampToString(Timestamp time, String fmt); static long setTimeToNextDay0H0M0S(Timestamp time); static long setTimeToNextDay0H0M0S(long millis); static long setToNextDay0H0M0SExceptToday(long millis); static long setToNextDay0H0M0SExceptToday(Timestamp stamp); static long setTimeTo0H0M0S(Timestamp time); static long setTimeTo0H0M0S(long millis); static void setTimeToLastDay0H0M0S(Timestamp time); static boolean isDifferentDay(Calendar calendar1, Calendar calendar2); static boolean isDifferentDay(Timestamp timestamp0, Timestamp timestamp1); static boolean isDifferentDay(long millis0, long millis1); static String long2Str(Long millSec); static String long2Str(long millSec, String format); static String long2Str(long millSec, String format, Locale locale); static long strToLong(String strTime, String pattern); static long todayPastMillisecond(long millions); static int getCurrentMonthDays(); static int getCurrentMonthDays(Timestamp stamp); static int getCurrentMonthDays(long ms); static int getCurrentMonthDays(Calendar cal); static long getFirstDayOfCurrentWeek(Timestamp stamp); static long getFirstDayOfCurrentWeek(long ms); static long setToFirstDayOfCurrentYear(long millis); static long setToFirstDayOfNextYear(long millis); static long setToFirstDayOfLastMonth(long ms); static long setToLastMonthCommonDay(long ms); static long setToFirstDayOfCurrentMonth(long millis); static long setToFirstDayOfNextMonth(long millis); static long setToNextYearCommonDay(long millis); static long setToLastYearCommonDay(long millis); static long getLastDayOfCurrentWeek(Timestamp stamp); static long getFirstDayOfCurrentQuarter(long ms); static long getFirstDayOfNextQuarter(long ms); static int getDayOfWeek(long ms); static long create0H0M0STime(); static boolean isToday(long ms); static String friendlyTime(long ms); static int getWeeks(long startTime, long endTime); static String getDateWithWeek(long ms); static String getDateWithWeekAndTime(long ms); static int getAge(long ms); static String getLastYearCommonDay(String strDate, String format); static String getLastMonthCommonDay(String strDate, String format); static Timestamp getBirthdayFormIdCard(String idCard); static long getLongBirthFormIdCard(String idCard); static boolean isCurrentYear(long ms); static String format(LocalDateTime localDateTime, String pattern); static long localDateToLong(LocalDate localDate); static LocalDate parseLocalDate(String time, String pattern); static LocalDateTime parseLocalDateTime(String time, String pattern); static long LocalDateTimeToLong(LocalDateTime dateTime); static final String DATE_FORMAT_MINITE; static final String DATE_FORMAT_DAY; static final String DATE_FORMAT_SECOND; static final String DATE_FORMAT_SECOND_12; static final String DATE_FORMAT_MILLISECOND; static final String DATE_FORMAT_CHINESE; static final String DATE_FORMAT_CHINESE_SECONDE; static final String DATE_FORMAT_CHINESE_WEEK_SECONDE; static final String YYYYMMDD; static final String YYYYMMDDHHMMSS; static final String YYYYMMDDHHMMSSSSS; static final long DAY_MS; }