安卓日历Calendar和DatePicker日期选择器的使用

一、Calendar的使用

Calendar 该控件可以获得日期和时间,可以获取当前的,也可以根据当前日期加减年月日时分秒。
比如想做一个类似这样的东西,只使用Calendar 就够了。
在这里插入图片描述

1:获取年、月、日、周、时、分、秒

 private Calendar calendar;     //定义一个全局的
    private  int year;      //年
    private  int month;   //月
    private  int day;       //日
   
         calendar= Calendar.getInstance();   // 然后获得该类的实列
          year=calendar.get(Calendar.YEAR);   //获取当前年份赋值给year
          Log.e(TAG,"当前年份:"+calendar.get(Calendar.YEAR));
          month=calendar.get(Calendar.MONTH);  //获取当前月份赋值给month,月份坐标是从0开始的0~11,真实月份要在该基础上+1;
          Log.e(TAG,"当前月份"+(calendar.get(Calendar.MONDAY)+1) );
          day=calendar.get(Calendar.DAY_OF_MONTH);   //获取当前日期赋值给day
          Log.e(TAG,"本月的第几天:"+calendar.get(Calendar.DAY_OF_MONTH));
           //也可以获取第几周
           Log.e(TAG,"在本月的第几周"+calendar.get(Calendar.DAY_OF_WEEK_IN_MONTH));
          Log.e(TAG,"周几:"+calendar.get(Calendar.DAY_OF_WEEK));    
          Log.e(TAG,"在本年的第几周:"+calendar.get(Calendar.WEEK_OF_YEAR));  
      //这个时候我发现一个明显错误和英文字面上意思不一样,返回的数字和周几不一样,今天周一返回的却是2,点开源码看一下
 /**
 * Value of the {@link #DAY_OF_WEEK} field indicating
 * Sunday.
 */
public final static int SUNDAY = 1;

/**
 * Value of the {@link #DAY_OF_WEEK} field indicating
 * Monday.
 */
public final static int MONDAY = 2;

/**
 * Value of the {@link #DAY_OF_WEEK} field indicating
 * Tuesday.
 */
public final static int TUESDAY = 3;

/**
 * Value of the {@link #DAY_OF_WEEK} field indicating
 * Wednesday.
 */
public final static int WEDNESDAY = 4;

/**
 * Value of the {@link #DAY_OF_WEEK} field indicating
 * Thursday.
 */
public final static int THURSDAY = 5;

/**
 * Value of the {@link #DAY_OF_WEEK} field indicating
 * Friday.
 */
public final static int FRIDAY = 6;

/**
 * Value of the {@link #DAY_OF_WEEK} field indicating
 * Saturday.
 */
public final static int SATURDAY = 7;

//发现返回1表示周日、返回2表示周一以此类推,返回7表示周六

      //也可以获取时、分、秒
  Log.e(TAG,"获取时,24小时制:"+calendar.get(Calendar.HOUR_OF_DAY));
      Log.e(TAG,"获取时,12小时制:"+calendar.get(Calendar.HOUR));
      Log.e(TAG,"获取分:"+calendar.get(Calendar.MINUTE));
      Log.e(TAG,"获取秒:"+calendar.get(Calendar.SECOND));
      Log.e(TAG,"获取毫秒:"+calendar.get(Calendar.MILLISECOND));
      Log.e(TAG,"获取完整日期和时间:"+calendar.getTime());

2: calendar.add( );的使用

         目前只能获取当前日期,做成上图效果还不够,需要在日历年月日的基础上加减,该类这个方法可以实现
         calendar.add(Calendar.YEAR,-1);  //该日历指向的年份减1,如果需要加1就把方法的第二个参数改为1,该方法不限于1,也可以加减5、6、2等等。
          year=calendar.get(Calendar.YEAR);   //获取当前年份赋值给year,此时输出year是已经减1年的日期
         Log.e(TAG,"当前年份:"+calendar.get(Calendar.YEAR));//打印一下日历指向年份,发现是去年的2020年
         
          calendar.add(Calendar.MONTH,1);  //该日历指向的月份加1
          month=calendar.get(Calendar.MONTH); //获取当前月份赋值给month,以供输出最新月份
         Log.e(TAG,"当前月份"+(calendar.get(Calendar.MONDAY)+1) ); //该加1是因为月份是0~11,不是1~12,真实月份要在获取月份上+1;
       
         calendar.add(Calendar.DAY_OF_YEAR,1);    //日加1
          day=calendar.get(Calendar.DAY_OF_MONTH);   //获取当前日期赋值给day,以供输出最新日期
           Log.e(TAG,"当前月份"+(calendar.get(Calendar.DAY_OF_MONTH)) );  //打印当前日期

二、格式化时间 SimpleDateFormat的简单使用 , SimpleDateFormat //该类可以格式化时间,在返回有关日期经常使用

    Date date = new Date();
    SimpleDateFormat  simpleDateFormat  = new SimpleDateFormat("yyyy--MM--dd HH:mm:ss");
    Log.e("格式化时间",simpleDateFormat.format(date));

三、DatePicker日期选择器(先留着以后写)

https://github.com/Bigkoo/Android-PickerView //三方控件
implementation ‘com.squareup:android-times-square:1.6.5@aar’ //三方控件

四、TimePicker时间选择器(先留着以后写)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值