网站建设外文版要求,wap是什么东西,购物网站项目经验,深圳网站维护服务的公司直接从需求来理解#xff1a;将2024年2月16日增加一个月
如果不使用Calendar的话#xff0c;我们需要定义字符串记住这个日期#xff0c;然后把字符串解析成Date日期对象#xff0c;通过Date日期对象获取其毫秒值#xff0c;然后增加一个月的毫秒值#xff0c;再格式化时…直接从需求来理解将2024年2月16日增加一个月
如果不使用Calendar的话我们需要定义字符串记住这个日期然后把字符串解析成Date日期对象通过Date日期对象获取其毫秒值然后增加一个月的毫秒值再格式化时间毫秒值得到结果。
但是我们用Calendar来解决的话就很简单Calendar代表的是系统此刻时间对应的日历通过它可以单独获取、修改时间中的年月日时分秒。
Calendar是一个抽象类不能直接使用Calendar提供了一个类方法getInstance用于获取此类型的通用对象。Calendar rightNow Calendar.getInstance(); import java.util.Calendar;
import java.util.Date;public class Test {public static void main(String[] args){//1得到系统此刻时间对应的日历对象Calendar now Calendar.getInstance();
// System.out.println(now);//2获取日历中的某个信息int y now.get(Calendar.YEAR); //从日历对象中获取年System.out.println(y);int d now.get(Calendar.DAY_OF_MONTH);System.out.println(d);//3拿到日历中记录的日期对象Date dd now.getTime();System.out.println(dd);//4拿到时间毫秒值long time now.getTimeInMillis();System.out.println(time);//5修改日历中的某个信息//把月份改成10月now.set(Calendar.MONTH,8);int m2 now.get(Calendar.MONTH);System.out.println(m2); //8//6为某个信息增加或减少多少now.add(Calendar.MONTH,2);int m3 now.get(Calendar.MONTH);System.out.println(m3); //10System.out.println(now);}
}