当前位置:网站首页>Time dependent - format, operation, comparison, conversion

Time dependent - format, operation, comparison, conversion

2022-06-11 01:11:00 Lingqiu, baigeiwang

1、 Get the current time

Date currentDate = new Date();

2、 Convert to specified format string

DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
String currentDateStr = df.format(currentDate);

3、 Get a year of time 、 month 、 Zhou 、 Japan

Calendar ca = Calendar.getInstance();
ca.setTime(currentDate);

int day = ca.get(Calendar.DAY_OF_YEAR);// The day of the year 

int week = ca.get(Calendar.WEEK_OF_YEAR);// Weeks of the year 

int month = ca.get(Calendar.MONTH)+1;// The first few months  8

int year = ca.get(Calendar.YEAR);// Year value  2021

4、 Compare the size of the two times
1) Law 1 : use Date Of getTime() Function to get the numeric type of time (long), Then compare the size of the two as a number

Date date01 = new Date();
Date date02 = new Date();

if(date01.getTime() > date02.getTime()){
    
	System.out.println("date01 Bigger ");
}else {
    
	System.out.println("date02 Greater than or equal to date01");
}
原网站

版权声明
本文为[Lingqiu, baigeiwang]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/162/202206102342311547.html