Date類、LocalDate類基本操作


Date類和LocalDate類

Date類用來表示時間點,LocalDate類是作為日歷表示法的類。

示例:

package riqi_test;

 

import java.time.LocalDate;

import java.util.Date;

/**

 * 時間點、日期。Date類與LocalDate類

 * @authorfandi1993

 *

 */

public class RiQiTest1 {

       publicstatic void main(String[] args){

             

              Dated = new Date(); //時間點

              System.out.println(d);

              System.out.println(d.toString());

              Dated1 = d;  //兩個變量d和d1共同引用一個變量

              System.out.println(d1);

             

              System.out.println(LocalDate.now());  //時間、日期

              LocalDateld = LocalDate.of(2016, 3, 8);

              System.out.println(ld+""+ld.getYear()+" "+ld.getDayOfMonth()+""+ld.getMonthValue());

              LocalDateldp = ld.plusYears(2);  //ldp通過計算得到

              System.out.println(ldp.getYear()+""+ld.getYear());

       }

}

運行結果如下:

TueNov 22 09:45:03 CST 2016

TueNov 22 09:45:03 CST 2016

TueNov 22 09:45:03 CST 2016

2016-11-22

2016-03-082016 8 3

20182016

Date類

Date類使用new date()的方式構造一個新的對象。這個對象被初始化為當前的日期和時間。

LocalDate類

日歷計算示例:

 

publicclass RiLiTest {

         public static void main(String[] args){

                   //獲取當前日期

                   LocalDate date = LocalDate.now();

                   System.out.println(date);

                   int month =date.getMonthValue();

                   int today =date.getDayOfMonth();

                   System.out.println(date+""+month+" "+today);

                   //將date設置為這個月的第一天

                   date =date.minusDays(today-1);

                   System.out.println(date);

                   //獲得這一天為星期幾

                   DayOfWeek weekday =date.getDayOfWeek();

                   int value =weekday.getValue();

                   System.out.println(value);

                   //設置月份的第一天指向相應的星期幾

                   System.out.println("MonTue Wed Thu Fri Sat Sun");

                   for (int i = 1; i < value;i++) {

                            System.out.print("   ");

                   }

                   while (date.getMonthValue()== month) {

                            System.out.printf("%3d",date.getDayOfMonth());

                            if(date.getDayOfMonth() == today) {

                                     System.out.print("*");

                            } else {

                                     System.out.print("");

                            }

                            date =date.plusDays(1);

                            if (date.getDayOfWeek().getValue()== 1) {

                                     System.out.println();

                            }

                   }

                  

         }

}

 

運行結果如下:

2016-11-22

2016-11-2211 22

2016-11-01

2

MonTue Wed Thu Fri Sat Sun

     1  2   3   4  5   6

  7  8   9  10 11  12  13

 14 15  16  17 18  19  20

 21  22*23  24 25  26  27

 28 29  30


注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
粤ICP备14056181号  © 2014-2020 ITdaan.com