编程知识 cdmana.com

Introduction to Java lesson 102 - Calendar calls get method to get date component

There are so many on the Internet java The article , Why it depends on my ?

The system is comprehensive : One article in the East and one in the West , I feel dizzy .

It's not easy to code words , Please pay attention to the official account. , forward , give the thumbs-up , Just move your fingers , Return video , Just to achieve you !

problem

     On the basis of the previous case , Use Calendar Class get Method to get Calendar Date represented by class - The components of time , The detailed requirements are : First , Use Calendar Class represents time , Set the date to 2013 year 12 month 25 Japan , Time is the time at this moment , then , Get the date - What day of the week .

programme

     Use Calendar Class get Method to get Calendar Time represented by class - Each component of the date , The declaration of this method is as follows :

int get(int field)

     This method returns the value of the specified calendar field .

step

     To implement this case, follow the steps below .

Step one :

     First , by TestCalendar Class add test method testGet, then , In this method , First , adopt Calendar Class getInstance Method to get the current date - Time ; then , Use set Method to change the date to 2013 year 12 month 25 Japan , The code is as follows :

    package day03;
    
    import java.util.Calendar;
    import java.util.GregorianCalendar;
    import org.junit.Test;
    
    public class TestCalendar{
    
     /**
      * test get Method 
      */
      @Test
      public void testGet(){
          Calendar c=Calendar.getInstance();
          c.set(Calendar.YEAR,2013);
          c.set(Calendar.MONTH,Calendar.DECEMBER);
          c.set(Calendar.DATE,25);
      }
    }

Step two : Use get Method

     Use Calendar Class get Method to get 2013 year 12 month 25 The day of the week corresponding to the day , The code is as follows :

    package day03;
    
    import java.util.Calendar;
    import java.util.GregorianCalendar;
    import org.junit.Test;
    
    public class TestCalendar{
    
     /**
      * test get Method 
      */
      @Test
      public void testGet(){
          Calendar c=Calendar.getInstance();
          c.set(Calendar.YEAR,2013);
          c.set(Calendar.MONTH,Calendar.DECEMBER);
          c.set(Calendar.DATE,25);
          
          int dayOfWeek=c.get(Calendar.DAY_OF_WEEK);
          System.out.println(dayOfWeek);// Output is 4, Wednesday , Sunday is the day of the week 1 God .
      }
    }

Step three : function

     function testGet Method , The console output is as follows :

    4

     You can see that the output of the console is 4, Wednesday , Sunday is the day of the week 1 God .

Official account , Get a learning video

版权声明
本文为[Monkeys learn to program]所创,转载请带上原文链接,感谢

Scroll to Top