LocalDate的一些使用方法
今天半天的時間都用在了LocalDate上,然后呢,也是自己的第一次寫博客。
首先來看看會用上的方法吧。
兩個構造器,用的是靜態工廠方法
static LocalTime now()
構造一個表示當前日期的對象 如果直接輸出這個對象的話,會是 年-月-日
static LocalTime of(int year,int month,int day)
自己設置年月日
int getYear()
int getMonthValue()
int getDayOfMonth()
獲取當前的年月日
DayOfWeek getDayOfWeek()
得到這天是星期幾,不過要用getValue方法來得到 1-7 的int值 星期1就是1
LocalDate plusDays(int) 往后多少天,然后返回一個新的LocalDate對象
LocalDate minusDay(int) 往前 多少天
下面是個例子,輸出當前月的日歷

1 public static void main(String[] args) { 2 LocalDate date = LocalDate.now(); 3 int month = date.getMonthValue(); 4 int today = date.getDayOfMonth(); 5 date = date.minusDays(today-1); 6 DayOfWeek weekday = date.getDayOfWeek(); 7 int value = weekday.getValue(); 8 // System.out.println("value的值:"+value); 9 System.out.println("Mon Tue Wed Thu Fri Sat Sun"); 10 for (int i = 1;i<=value;i++){ 11 12 System.out.print(" "); 13 } 14 while(date.getMonthValue() ==month){ 15 System.out.printf("%3d",date.getDayOfMonth()); 16 if(date.getDayOfMonth()==today){ 17 System.out.print("*"); 18 }else{ 19 System.out.print(" "); 20 } 21 date = date.plusDays(1); 22 if (date.getDayOfWeek().getValue()==1){ 23 System.out.println(); 24 } 25 } 26 if(date.getDayOfWeek().getValue()!=1){ 27 System.out.println(); 28 } 29 }