java calendar new_Java Calendar set()用法及代码示例

Calendar类中的set(int calndr_field,int new_val)方法用于将calndr_field值设置为new_val。此日历的旧字段将替换为新字段。

用法:

public void set(int calndr_field, int new_val)

参数:该方法有两个参数:

calndr_field:这是“日历”类型,是指要更改的日历字段。

new_val:这是指要替换的新值。

返回值:该方法不返回任何值。

以下示例程序旨在说明Calendar类的set()方法的用法:

示例1:

// Java code to illustrate

// set() method

import java.util.*;

public class Calendar_Demo {

public static void main(String args[])

{

// Creating a calendar

Calendar calndr = Calendar.getInstance();

// Displaying the month

System.out.println("The Current Month is: "

+ calndr.get(

Calendar.MONTH));

// Replacing with a new value

calndr.set(Calendar.MONTH, 11);

// Displaying the modified result

System.out.println("Altered Month is: "

+ calndr.get(

Calendar.MONTH));

}

}

输出:

The Current Month is: 1

Altered Month is: 11

示例2:

// Java code to illustrate

// set() method

import java.util.*;

public class Calendar_Demo {

public static void main(String args[])

{

// Creating a calendar

Calendar calndr = Calendar.getInstance();

// Displaying the Year

System.out.println("The Current year is: "

+ calndr.get(

Calendar.YEAR));

// Replacing with a new value

calndr.set(Calendar.YEAR, 1996);

// Displaying the modified result

System.out.println("Altered year is: "

+ calndr.get(

Calendar.YEAR));

}

}

输出:

The Current year is: 2019

Altered year is: 1996

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值