1. 程式人生 > >idea 時間工具類

idea 時間工具類

private static final String DEFAULT_FORMAT = "yyyy-MM-dd"; //定義預設的日期格式

/**
 * 判斷時間是否有效
 *
 * @param date 用正則表示式判斷 yyyy-MM-dd
 * @return
 */
public static boolean isValidateDate(String date) {
    Matcher matcher = null;
    Boolean res = false;
    String regexp = "^[0-9]{4}-[0-9]{1,2}-[0-9]{1,2}";
    if (StringUtils.isNotEmpty(date)) {
        Pattern pattern = Pattern.compile(regexp);
        matcher = pattern.matcher(date);
    }
    if (matcher != null) {
        res = matcher.matches();
    }
    return res;
}

/**
 * 預設獲取昨天的日期  yyyy-MM-dd
 *
 * @return
 */
public static String getYesterday() {
    return getYesterday(DEFAULT_FORMAT);
}

/**
 * @param pattern 獲取指定格式的昨天的日期
 * @return
 */
public static String getYesterday(String pattern) {
    SimpleDateFormat sdf = new SimpleDateFormat(pattern);
    Calendar calendar = Calendar.getInstance();
    calendar.add(Calendar.DAY_OF_YEAR, -1);
    return sdf.format(calendar.getTime());
}


/**
 * 將時間戳轉換成預設格式的日期
 *
 * @param time
 * @return
 */
public static String parseLong2String(long time) {
    return parseLong2String(time, DEFAULT_FORMAT);
}

public static String parseLong2String(long time, String pattern) {
    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(time);
    return new SimpleDateFormat(pattern).format(calendar.getTime());
}


/**
 * 將預設的日期格式轉換成時間戳
 *
 * @param date
 * @return
 */
public static long parseString2Long(String date) {
    return parseString2Long(date, DEFAULT_FORMAT);
}

public static long parseString2Long(String date, String pattern) {
    Date dt = null;

    try {
        dt = new SimpleDateFormat(pattern).parse(date);
    } catch (ParseException e) {
        e.printStackTrace();
    }
    return dt.getTime();
}

/**
 * 獲取日期資訊
 * @param time
 * @param type
 * @return
 */
public static int getDateInfo(long time, DateEnum type) {
    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(time);
    if(type.equals(DateEnum.YEAR)){
        return calendar.get(Calendar.YEAR);
    }
    if(type.equals(DateEnum.SEASON)){
        int month = calendar.get(Calendar.MONTH) + 1;
        return month % 3 == 0 ? month / 3 : (month / 3 + 1);
    }
    if(type.equals(DateEnum.MONTH)){
        return calendar.get(Calendar.MONTH) + 1;
    }
    if(type.equals(DateEnum.WEEK)){
        return calendar.get(Calendar.WEEK_OF_YEAR);
    }
    if(type.equals(DateEnum.DAY)){
        return calendar.get(Calendar.DAY_OF_MONTH);
    }
    if(type.equals(DateEnum.HOUR)){
        return calendar.get(Calendar.HOUR_OF_DAY);
    }
    throw  new RuntimeException("不支援該型別的日期資訊獲取.type:"+type.dateType);
}

/**
 * 獲取某周第一天時間戳
 * @param time
 * @return
 */
public static long getFirstDayOfWeek(long time) {
    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(time);
    //
    calendar.set(Calendar.DAY_OF_WEEK,1);//該周的第一天
    calendar.set(Calendar.HOUR_OF_DAY,0);
    calendar.set(Calendar.MINUTE,0);
    calendar.set(Calendar.SECOND,0);
    calendar.set(Calendar.MILLISECOND,0);
    return calendar.getTimeInMillis();
}

public static void main(String[] args) {
    System.out.println(TimeUtil.isValidateDate("2018-09-20"));
    System.out.println(TimeUtil.isValidateDate("2018-9-20"));
    System.out.println(TimeUtil.getYesterday());
    System.out.println(TimeUtil.getYesterday("yyyy/MM/dd"));
    System.out.println(TimeUtil.parseString2Long("2018-09-20"));
    System.out.println(TimeUtil.parseLong2String(1537372800000L,"yyyy-MM-dd"));
    System.out.println(TimeUtil.getDateInfo(1537372800000L,DateEnum.DAY));
    System.out.println(TimeUtil.getDateInfo(1537372800000L,DateEnum.WEEK));
    System.out.println(TimeUtil.getDateInfo(1537372800000L,DateEnum.SEASON));
    System.out.println(TimeUtil.getDateInfo(1537372800000L,DateEnum.MONTH));
    System.out.println(TimeUtil.getFirstDayOfWeek(1537372800000L));
    System.out.println(TimeUtil.parseLong2String(1537027200000L,"yyyy-MM-dd"));
}