import java.util.Calendar; public class DateUtils { public static void main(String[] args) { Calendar cal = Calendar.getInstance(); cal.set(Calendar.DAY_OF_MONTH, 1); cal.set(Calendar.HOUR_OF_DAY, 0); cal.set(Calendar.MINUTE, 0); cal.set(Calendar.SECOND, 0); cal.set(Calendar.MILLISECOND, 0); System.out.println(cal.getTime()); } }这段代码怎么优化
时间: 2023-03-25 20:02:39 浏览: 92
java.util包总结
这段代码可以优化的地方是可以使用链式编程来设置时间,可以减少代码行数,例如:
```
Calendar cal = Calendar.getInstance();
cal.set(Calendar.DAY_OF_MONTH, 1)
.set(Calendar.HOUR_OF_DAY, )
.set(Calendar.MINUTE, )
.set(Calendar.SECOND, )
.set(Calendar.MILLISECOND, );
System.out.println(cal.getTime());
```
阅读全文