Calendar常用API

简介: Calendar常用API

  为特定瞬间与一组诸如 YEAR、MONTH、DAY_OF_MONTH、HOUR 等 日历字段之间的转换提供了一些方法,并为操作日历字段(例如获得下星期的日期)提供了一些方法。

A:获取

  public int get(int field)

返回给定日历字段的值。日历类中的每个日历字段都是静态的成员变量,并且是int类型。

public class CalendarDemo {
  public static void main(String[] args) {
    // 其日历字段已由当前日期和时间初始化:
    Calendar rightNow = Calendar.getInstance(); // 子类对象

    // 获取年
    int year = rightNow.get(Calendar.YEAR);
    // 获取月
    int month = rightNow.get(Calendar.MONTH);
    // 获取日
    int date = rightNow.get(Calendar.DATE);

    System.out.println(year + "年" + (month + 1) + "月" + date + "日");
  }
}

B:设置

  public void add(int field,int amount)

根据给定的日历字段和对应的时间,来对当前的日历进行操作。

  public final void set(int year,int month,int date)

设置当前日历的年月日

public class CalendarDemo {
  public static void main(String[] args) {
    // 获取当前的日历时间
    Calendar c = Calendar.getInstance();

    // 获取年
    int year = c.get(Calendar.YEAR);
    // 获取月
    int month = c.get(Calendar.MONTH);
    // 获取日
    int date = c.get(Calendar.DATE);
    System.out.println(year + "年" + (month + 1) + "月" + date + "日");

    // // 三年前的今天
    // c.add(Calendar.YEAR, -3);
    // // 获取年
    // year = c.get(Calendar.YEAR);
    // // 获取月
    // month = c.get(Calendar.MONTH);
    // // 获取日
    // date = c.get(Calendar.DATE);
    // System.out.println(year + "年" + (month + 1) + "月" + date + "日");

    // 5年后的10天前
    c.add(Calendar.YEAR, 5);
    c.add(Calendar.DATE, -10);
    // 获取年
    year = c.get(Calendar.YEAR);
    // 获取月
    month = c.get(Calendar.MONTH);
    // 获取日
    date = c.get(Calendar.DATE);
    System.out.println(year + "年" + (month + 1) + "月" + date + "日");
    System.out.println("--------------");

    c.set(2011, 11, 11);
    // 获取年
    year = c.get(Calendar.YEAR);
    // 获取月
    month = c.get(Calendar.MONTH);
    // 获取日
    date = c.get(Calendar.DATE);
    System.out.println(year + "年" + (month + 1) + "月" + date + "日");
  }
}
//获取到当年的三月第一天,然后减去一天,得到二月最后一天,即可获取二月的天数
public class Demo {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请录入年份:");
        int year = sc.nextInt();
        Calendar c = Calendar.getInstance();
        c.set(year,2,1);
        c.add(Calendar.DATE,-1);
        int day = c.get(Calendar.DAY_OF_MONTH);
        System.out.println(year + "年2月有 " + day + " 天");
    }
}

目录
打赏
0
1
1
0
42
分享
相关文章
|
11月前
|
什么是API Token?
API令牌是一种用于访问和验证API(应用程序编程接口)的安全凭证。它是一个字符串,用于识别和授权应用程序或用户访问特定的API服务或资源。==API令牌可以是访问令牌(Access Token)或密钥(API Key)==。
1192 0
java常用API方法
java常用API方法
127 0
|
8月前
|
API
Date和SimpleDateFormat常用API
Date和SimpleDateFormat常用API
70 1
API在实际有什么运用?
API(Application Programming Interface,应用程序接口)是一组定义、协议和工具的集合,用于建立软件与软件之间的互操作性。它允许开发人员使用预先定义的方法来请求服务、访问数据或执行特定功能,而无需了解底层代码的具体实现。在现代软件开发中,API扮演着至关重要的角色,从简单的网页应用到复杂的企业系统,都离不开API的支持。
621 1
SofaMQ一些常用的API
SofaMQ一些常用的API
559 1
什么是API?谈对API的理解
什么是API?谈对API的理解
269 1
简单使用时间API(LocalDateTime)
简单使用时间API(LocalDateTime)
163 0
Java常用API(三)Date,DateFormat和Calendar
Java常用API(三)Date,DateFormat和Calendar
115 0

热门文章

最新文章

AI助理

你好,我是AI助理

可以解答问题、推荐解决方案等