在Java中,可以使用Calendar类的set方法来设置日期和时间的值。以下是一些示例:
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.YEAR, 2021);
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.MONTH, Calendar.JANUARY);
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.DAY_OF_MONTH, 1);
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 12);
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.MINUTE, 30);
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.SECOND, 0);
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.MILLISECOND, 0);
需要注意的是,Calendar类的月份从0开始,即0表示一月,11表示十二月。另外,Calendar类的set方法也可以同时设置多个字段的值,例如:
Calendar calendar = Calendar.getInstance();
calendar.set(2021, Calendar.JANUARY, 1, 12, 30, 0);
这样就可以一次性设置年份、月份、日期、小时、分钟、秒的值。