在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);
這樣就可以一次性設置年份、月份、日期、小時、分鐘、秒的值。