每天记录学习的新知识:Calendar.getInstance() 获取时间

作用:

使用Calendar.getInstance()不仅能获取当前的时间,还能指定需要获取的时间点,在项目应用中达到定时的作用。

Calendar常用的方法的返回结果:

public class CalendarText {

    Calendar calendar = null;
    
    @Before
    public void test() {
        calendar = Calendar.getInstance();
    }

    // 基本用法,获取年月日时分秒星期
    @Test
    public void test1() {
    
        // 获取年
        int year = calendar.get(Calendar.YEAR);
        // 获取月,这里需要需要月份的范围为0~11,因此获取月份的时候需要+1才是当前月份值
        int month = calendar.get(Calendar.MONTH) + 1;
        // 获取日
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        // 获取时
        int hour = calendar.get(Calendar.HOUR);
        // int hour = calendar.get(Calendar.HOUR_OF_DAY); // 24小时表示
        // 获取分
        int minute = calendar.get(Calendar.MINUTE);
        // 获取秒
        int second = calendar.get(Calendar.SECOND);
        // 星期,英语国家星期从星期日开始计算
        int weekday = calendar.get(Calendar.DAY_OF_WEEK);
        
        //现在是2019年1月14日3时22分35秒星期2,
        //发现获得星期有问题,中国星期一是开始,国外是星期日
        weekday = weekday - 1 > 0 ? weekday - 1 : 7;

        System.out.println("现在是" + year + "年" + month + "月" + day + "日"
         + hour + "时" + minute + "分" + second + "秒" + "星期" + weekday);
    }

    // 一年后的今天
    @Test
    public void test2() {
    
        // 同理换成下个月的今天calendar.add(Calendar.MONTH, 1);
        calendar.add(Calendar.YEAR, 1);
        // 获取年
        int year = calendar.get(Calendar.YEAR)
        // 获取月
        int month = calendar.get(Calendar.MONTH) + 1;
        // 获取日
        int day = calendar.get(Calendar.DAY_OF_MONTH);

        System.out.println("一年后的今天:" + year + "年" + month + "月" 
        + day + "日");
    }

    // 获取任意一个月的最后一天
    @Test
    public void test3() {
    
        // 假设求6月的最后一天
        int currentMonth = 6;
        // 先求出7月份的第一天,实际中这里6为外部传递进来的currentMonth变量
        // 1
        calendar.set(calendar.get(Calendar.YEAR), currentMonth, 1);
        calendar.add(Calendar.DATE, -1)
        // 获取日
        int day = calendar.get(Calendar.DAY_OF_MONTH);

        System.out.println("6月份的最后一天为" + day + "号");
    }

    // 设置日期
    @Test
    public void test4() {
    
        calendar.set(Calendar.YEAR, 2000);
        System.out.println("现在是" + calendar.get(Calendar.YEAR) + "年");

        calendar.set(2008, 8, 8);
        // 获取年
        int year = calendar.get(Calendar.YEAR);
        // 获取月
        int month = calendar.get(Calendar.MONTH);
        // 获取日
        int day = calendar.get(Calendar.DAY_OF_MONTH);

        System.out.println("现在是" + year + "年" + month + "月" + day + "日");
    }
}

获取指定时间点:


//获得当天0点时间
public static int getTimesmorning(){
Calendar cal = Calendar.getInstance();
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.MILLISECOND, 0);
return (int) (cal.getTimeInMillis()/1000);
}

//获得当天24点时间
public static int getTimesnight(){
Calendar cal = Calendar.getInstance();
cal.set(Calendar.HOUR_OF_DAY, 24);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.MILLISECOND, 0);
return (int) (cal.getTimeInMillis()/1000);
}

//获得本周一0点时间
public static int getTimesWeekmorning(){
Calendar cal = Calendar.getInstance();
cal.set(cal.get(Calendar.YEAR),cal.get(Calendar.MONDAY), cal.get(Calendar.DAY_OF_MONTH), 0, 0,0);
cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
return (int) (cal.getTimeInMillis()/1000);
}

//获得本周日24点时间
public static int getTimesWeeknight(){
Calendar cal = Calendar.getInstance();
cal.set(cal.get(Calendar.YEAR),cal.get(Calendar.MONDAY), cal.get(Calendar.DAY_OF_MONTH), 0, 0,0);
cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
return (int) ((cal.getTime().getTime()+ (7 * 24 * 60 * 60 * 1000))/1000);
}

//获得本月第一天0点时间
public static int getTimesMonthmorning(){
Calendar cal = Calendar.getInstance();
cal.set(cal.get(Calendar.YEAR),cal.get(Calendar.MONDAY), cal.get(Calendar.DAY_OF_MONTH), 0, 0,0);
cal.set(Calendar.DAY_OF_MONTH,cal.getActualMinimum(Calendar.DAY_OF_MONTH));
return (int) (cal.getTimeInMillis()/1000);
}

//获得本月最后一天24点时间
public static int getTimesMonthnight(){
Calendar cal = Calendar.getInstance();
cal.set(cal.get(Calendar.YEAR),cal.get(Calendar.MONDAY),cal.get(Calendar.DAY_OF_MONTH), 0, 0,0);
cal.set(Calendar.DAY_OF_MONTH,cal.getActualMaximum(Calendar.DAY_OF_MONTH));
cal.set(Calendar.HOUR_OF_DAY, 24);
return (int) (cal.getTimeInMillis()/1000);
}

不能差不多
http://www.cnblogs.com/huangminwen/p/6041168.html

阿里云 Calendar.getInstance()获取指定点时间
https://www.aliyun.com/jiaocheng/1325151.html

你可能感兴趣的:(Android,系统功能)