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, 0);
        calendar.clear(Calendar.MINUTE);
        calendar.clear(Calendar.SECOND);
        calendar.clear(Calendar.MILLISECOND);

        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));

}

你可能感兴趣的:(Calendar类测试)