点击阅读原文跳转
/**
* Return the current time in milliseconds.
*
* @return the current time in milliseconds
*/
public static long getNowMills() {
return System.currentTimeMillis();
}
/**
* Return the current formatted time string.
* <p>The pattern is {@code yyyy-MM-dd HH:mm:ss}.</p>
*
* @return the current formatted time string
*/
public static String getNowString() {
return millis2String(System.currentTimeMillis(), getDefaultFormat());
}
private static SimpleDateFormat getDefaultFormat() {
return getSafeDateFormat("yyyy-MM-dd HH:mm:ss");
}
/**
* Return the current date.
*
* @return the current date
*/
public static Date getNowDate() {
return new Date();
}
/**
* Return whether it is leap year.
*
* @param date The date.
* @return {@code true}: yes<br>{@code false}: no
*/
public static boolean isLeapYear(final Date date) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
int year = cal.get(Calendar.YEAR);
return isLeapYear(year);
}
/**
* Return whether it is leap year.
*
* @param year The year.
* @return {@code true}: yes<br>{@code false}: no
*/
public static boolean isLeapYear(final int year) {
return year % 4 == 0 && year % 100 != 0 || year % 400 == 0;
}
/**
* Return the day of week in Chinese.
*
* @param date The date.
* @return the day of week in Chinese
*/
public static String getChineseWeek(final Date date) {
return new SimpleDateFormat("E", Locale.CHINA).format(date);
}
/**
* Return the day of week in US.
*
* @param date The date.
* @return the day of week in US
*/
public static String getUSWeek(final Date date) {
return new SimpleDateFormat("EEEE", Locale.US).format(date);
}
/**
* Return whether it is am.
*
* @param millis The milliseconds.
* @return {@code true}: yes<br>{@code false}: no
*/
public static boolean isAm(final long millis) {
return getValueByCalendarField(millis, GregorianCalendar.AM_PM) == 0;
}
public static int getValueByCalendarField(final long millis, final int field) {
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(millis);
return cal.get(field);
}
/**
* Return the Chinese zodiac.
*
* @param date The date.
* @return the Chinese zodiac
*/
public static String getChineseZodiac(final Date date) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
return CHINESE_ZODIAC[cal.get(Calendar.YEAR) % 12];
}
private static final String[] CHINESE_ZODIAC =
{"猴", "鸡", "狗", "猪", "鼠", "牛", "虎", "兔", "龙", "蛇", "马", "羊"};
/**
* Return the zodiac.
*
* @param date The date.
* @return the zodiac
*/
public static String getZodiac(final Date date) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
int month = cal.get(Calendar.MONTH) + 1;
int day = cal.get(Calendar.DAY_OF_MONTH);
return getZodiac(month, day);
}
/**
* Return the zodiac.
*
* @param month The month.
* @param day The day.
* @return the zodiac
*/
public static String getZodiac(final int month, final int day) {
return ZODIAC[day >= ZODIAC_FLAGS[month - 1]
? month - 1
: (month + 10) % 12];
}
private static final int[] ZODIAC_FLAGS = {20, 19, 21, 21, 21, 22, 23, 23, 23, 24, 23, 22};
private static final String[] ZODIAC = {
"水瓶座", "双鱼座", "白羊座", "金牛座", "双子座", "巨蟹座",
"狮子座", "处女座", "天秤座", "天蝎座", "射手座", "摩羯座"
};