前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >XTime使用笔记

XTime使用笔记

作者头像
项勇
发布2023-03-24 13:39:21
2680
发布2023-03-24 13:39:21
举报
文章被收录于专栏:项勇

一.代码

点击阅读原文跳转

二.demo

三.方法

获取当前毫秒时间戳
代码语言:javascript
复制
 /**
 * Return the current time in milliseconds.
 *
 * @return the current time in milliseconds
 */
public static long getNowMills() {
    return System.currentTimeMillis();
}
获取当前时间字符串
代码语言:javascript
复制
/**
 * 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
代码语言:javascript
复制
/**
 * Return the current date.
 *
 * @return the current date
 */
public static Date getNowDate() {
    return new Date();
}
今年是否闰年
代码语言:javascript
复制
/**
 * 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;
}
获取中式星期
代码语言:javascript
复制
 /**
 * 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);
}
获取美式式星期
代码语言:javascript
复制
 /**
 * 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);
}
判断现在是否上午
代码语言:javascript
复制
 /**
 * 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);
}
获取今年生肖
代码语言:javascript
复制
 /**
 * 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 =
        {"猴", "鸡", "狗", "猪", "鼠", "牛", "虎", "兔", "龙", "蛇", "马", "羊"};
获取本月星座
代码语言:javascript
复制
  /**
  * 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       = {
         "水瓶座", "双鱼座", "白羊座", "金牛座", "双子座", "巨蟹座",
         "狮子座", "处女座", "天秤座", "天蝎座", "射手座", "摩羯座"
 };
本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2021-06-05,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 项勇 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 一.代码
  • 二.demo
  • 三.方法
    • 获取当前毫秒时间戳
      • 获取当前时间字符串
        • 获取当前 Date
          • 今年是否闰年
            • 获取中式星期
              • 获取美式式星期
                • 判断现在是否上午
                  • 获取今年生肖
                    • 获取本月星座
                    领券
                    问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档