XTime使用笔记
一.代码
二.demo
三.方法
1 | /** |
获取当前时间字符串
1
2
3
4
5
6
7
8
9
10
11
12
13/**
* 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");
}获取当前 Date
1
2
3
4
5
6
7
8/**
* Return the current date.
*
* @return the current date
*/
public static Date getNowDate() {
return new Date();
}今年是否闰年
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21/**
* 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;
}获取中式星期
1
2
3
4
5
6
7
8
9/**
* 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);
}获取美式式星期
1
2
3
4
5
6
7
8
9/**
* 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);
}判断现在是否上午
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15/**
* 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);
}获取今年生肖
1
2
3
4
5
6
7
8
9
10
11
12
13
14/**
* 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 =
{"猴", "鸡", "狗", "猪", "鼠", "牛", "虎", "兔", "龙", "蛇", "马", "羊"};获取本月星座
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32/**
* 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 = {
"水瓶座", "双鱼座", "白羊座", "金牛座", "双子座", "巨蟹座",
"狮子座", "处女座", "天秤座", "天蝎座", "射手座", "摩羯座"
};