Calendar類測試

public static void main(String[] args) throws ParseException
    {
        // 字符串轉換日期格式
        // DateFormat fmtDateTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        // 獲得日期格式對象
        // Date date = fmtDateTime.parse(strDateMake);

        // 完整顯示日期時間
        String str = (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS")).format(new Date());
        System.out.println(str);

        // 建立 Calendar 對象
        Calendar calendar = Calendar.getInstance();
        // 初始化 Calendar 對象,但並沒必要要,除非須要重置時間
        calendar.setTime(new Date());

        // setTime 相似上面一行
        // Date date = new Date();
        // calendar.setTime(date);

        // 顯示年份
        int year = calendar.get(Calendar.YEAR);
        System.out.println("YEAR is = " + String.valueOf(year));

        // 顯示月份 (從0開始, 實際顯示要加一)
        int MONTH = calendar.get(Calendar.MONTH);
        System.out.println("MONTH is = " + (MONTH + 1));

        // 今年的第 N 天
        int DAY_OF_YEAR = calendar.get(Calendar.DAY_OF_YEAR);
        System.out.println("DAY_OF_YEAR is = " + DAY_OF_YEAR);

        // 本月第 N 天
        int DAY_OF_MONTH = calendar.get(Calendar.DAY_OF_MONTH);
        System.out.println("DAY_OF_MONTH = " + String.valueOf(DAY_OF_MONTH));

        // 3小時之後
        calendar.add(Calendar.HOUR_OF_DAY, 3);
        int HOUR_OF_DAY = calendar.get(Calendar.HOUR_OF_DAY);
        System.out.println("HOUR_OF_DAY + 3 = " + HOUR_OF_DAY);

        // 當前分鐘數
        int MINUTE = calendar.get(Calendar.MINUTE);
        System.out.println("MINUTE = " + MINUTE);

        // 15 分鐘之後
        calendar.add(Calendar.MINUTE, 15);
        MINUTE = calendar.get(Calendar.MINUTE);
        System.out.println("MINUTE + 15 = " + MINUTE);

        // 30分鐘前
        calendar.add(Calendar.MINUTE, -30);
        MINUTE = calendar.get(Calendar.MINUTE);
        System.out.println("MINUTE - 30 = " + MINUTE);

        // 格式化顯示
        str = (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SS")).format(calendar.getTime());
        System.out.println(str);

        // 重置 Calendar 顯示當前時間
        calendar.setTime(new Date());
        str = (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SS")).format(calendar.getTime());
        System.out.println(str);

        // 建立一個 Calendar 用於比較時間
        Calendar calendarNew = Calendar.getInstance();

        // 設定爲 5 小時之前,後者大,顯示 -1
        calendarNew.add(Calendar.HOUR, -5);
        System.out.println("時間比較:" + calendarNew.compareTo(calendar));

        // 設定7小時之後,前者大,顯示 1
        calendarNew.add(Calendar.HOUR, +7);
        System.out.println("時間比較:" + calendarNew.compareTo(calendar));

        // 退回 2 小時,時間相同,顯示 0
        calendarNew.add(Calendar.HOUR, -2);
        System.out.println("時間比較:" + calendarNew.compareTo(calendar));

    }

/**
     * 根據time得到該time的月初
     *
     * @param time
     * @return
     */
    public static long getMonthStartByTime(long time)
    {
        Calendar calendar = new GregorianCalendar();
        calendar.setTimeInMillis(time);
        calendar.add(Calendar.MONTH, 0);
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.clear(Calendar.MINUTE);
        calendar.clear(Calendar.SECOND);
        calendar.clear(Calendar.MILLISECOND);

        return calendar.getTimeInMillis();
    }

    /**
     * 根據time得到該time的月末
     *
     * @param time
     * @return
     */
    public static long getMonthEndByTime(long time)
    {
        Calendar calendar = new GregorianCalendar();
        calendar.setTimeInMillis(time);
        calendar.add(Calendar.MONTH, 0);
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
     calendar.set(Calendar.HOUR_OF_DAY, 23);
     calendar.set(Calendar.MINUTE, 59);
     calendar.set(Calendar.SECOND, 59);
     calendar.set(Calendar.MILLISECOND, 999);
    return calendar.getTimeInMillis(); 
}
 
 

 

 

 

public static void main(String[] args) throws ParseException
{
// 字符串轉換日期格式
// DateFormat fmtDateTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
// 獲得日期格式對象
// Date date = fmtDateTime.parse(strDateMake);

// 完整顯示日期時間
String str = (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS")).format(new Date());
System.out.println(str);

// 建立 Calendar 對象
Calendar calendar = Calendar.getInstance();
// 初始化 Calendar 對象,但並沒必要要,除非須要重置時間
calendar.setTime(new Date());

// setTime 相似上面一行
// Date date = new Date();
// calendar.setTime(date);

// 顯示年份
int year = calendar.get(Calendar.YEAR);
System.out.println("YEAR is = " + String.valueOf(year));

// 顯示月份 (0開始, 實際顯示要加一)
int MONTH = calendar.get(Calendar.MONTH);
System.out.println("MONTH is = " + (MONTH + 1));

// 今年的第 N
int DAY_OF_YEAR = calendar.get(Calendar.DAY_OF_YEAR);
System.out.println("DAY_OF_YEAR is = " + DAY_OF_YEAR);

// 本月第 N
int DAY_OF_MONTH = calendar.get(Calendar.DAY_OF_MONTH);
System.out.println("DAY_OF_MONTH = " + String.valueOf(DAY_OF_MONTH));

// 3小時之後
calendar.add(Calendar.HOUR_OF_DAY, 3);
int HOUR_OF_DAY = calendar.get(Calendar.HOUR_OF_DAY);
System.out.println("HOUR_OF_DAY + 3 = " + HOUR_OF_DAY);

// 當前分鐘數
int MINUTE = calendar.get(Calendar.MINUTE);
System.out.println("MINUTE = " + MINUTE);

// 15 分鐘之後
calendar.add(Calendar.MINUTE, 15);
MINUTE = calendar.get(Calendar.MINUTE);
System.out.println("MINUTE + 15 = " + MINUTE);

// 30分鐘前
calendar.add(Calendar.MINUTE, -30);
MINUTE = calendar.get(Calendar.MINUTE);
System.out.println("MINUTE - 30 = " + MINUTE);

// 格式化顯示
str = (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SS")).format(calendar.getTime());
System.out.println(str);

// 重置 Calendar 顯示當前時間
calendar.setTime(new Date());
str = (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SS")).format(calendar.getTime());
System.out.println(str);

// 建立一個 Calendar 用於比較時間
Calendar calendarNew = Calendar.getInstance();

// 設定爲 5 小時之前,後者大,顯示 -1
calendarNew.add(Calendar.HOUR, -5);
System.out.println("時間比較:" + calendarNew.compareTo(calendar));

// 設定7小時之後,前者大,顯示 1
calendarNew.add(Calendar.HOUR, +7);
System.out.println("時間比較:" + calendarNew.compareTo(calendar));

// 退回 2 小時,時間相同,顯示 0
calendarNew.add(Calendar.HOUR, -2);
System.out.println("時間比較:" + calendarNew.compareTo(calendar));}
相關文章
相關標籤/搜索