编程知识 cdmana.com

Introduction to Java lesson 101 - Calendar calls set method to set date component

It's not easy to code words , Please pay attention to the official account. , forward , give the thumbs-up , Just move your fingers , No money , Thank you, old fellow. !

problem

     On the basis of the previous case , Use Calendar Class set Method to set the date - The components of time , The detailed requirements are as follows :

    1) Use Calendar Class represents time , Set the date to 2013 year 12 month 25 Japan , Time is the time at this moment , And put the date - Time changes to Date Class output .

    2) modify Calendar Class object , Set the day in the date to 32, The rest remains the same , Then put the date - Time date is converted to Date Class output .

programme

     Use Calendar Class set Method to set the components of date and time , The method is declared as follows :

void set(int field,int value)

     This method means setting the given calendar field to the given value . This method requires a int Type of field Parameters ,field yes Calendar Class static fields , Such as Calendar.YEAR、Calendar.MONTH And so on represent year 、 month 、 Japan 、 Hours 、 minute 、 Second wait for time field .

step

     To implement this case, follow the steps below .

Step one : Add test method

     stay TestCalendar Class to add test methods testSet, The code is as follows :

    package day03;
    
    import java.util.Calendar;
    import java.util.GregorianCalendar;
    import org.junit.Test;
    
    public class TestCalendar{
     /**
      * test set Method 
      */
      @Test
      public void testSet(){
      
      }
    }

Step two : Use Calendar Class set Method to set the date component

     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 , And will Calendar The time represented is converted to Date Represents the time output to the console , The code is as follows :

    package day03;
    
    import java.util.Calendar;
    import java.util.GregorianCalendar;
    import org.junit.Test;
    
    public class TestCalendar{
     /**
      * test set Method 
      */
      @Test
      public void testSet(){
          Calendar c=Calendar.getInstance();
          c.set(Calendar.YEAR,2013);
          c.set(Calendar.MONTH,Calendar.DECEMBER);
          c.set(Calendar.DATE,25);
          System.out.println(c.getTime());//Wed Des 25 16:45:01 CST 2013
      }
    }

Step three : function    

     function testSet Method , The console output is :

    Wed Dec 25 14:53:53 CST 2013

     You can see from the output that the date has been set to 2013 year 12 month 25 Japan , Time is the current time .

Step four : Put the object in step 2 c The day of is set to 32

     Use set Method will take the object in step 2 c Of " Japan " Set to 32, And will Calendar Indicated date - Time changes to Date Indicated date - Time output to console , The code is as follows :

    package day03;
    
    import java.util.Calendar;
    import java.util.GregorianCalendar;
    import org.junit.Test;
    
    public class TestCalendar{
     /**
      * test set Method 
      */
      @Test
      public void testSet(){
          Calendar c=Calendar.getInstance();
          c.set(Calendar.YEAR,2013);
          c.set(Calendar.MONTH,Calendar.DECEMBER);
          c.set(Calendar.DATE,25);
          System.out.println(c.getTime());//Wed Des 25 16:45:01 CST 2013
          
          // The actual date value is 1 month 1 Japan 
          c.set(Calendar.DATE,32);
          System.out.println(c.getTime());//Wed Jan 01 16:45:01 CST 2014
      }
    }

Step five : function

     function testSet Method , The console output is :

    Wed Dec 25 14:53:53 CST 2013
    Wed Jan 01 14:53:53 CST 2014

     As you can see, set the day to 32 after , Date change 2014 year 1 month 1 Japan , This is because , When the modified field exceeds its allowed range , Carry will occur , That is, the upper level fields will be larger . In this case , Set the day to 32 after , however ,2013 year 12 Month only 31 God , It's beyond the range allowed by the day , So carry , Turn into 1 month 1 Japan .

Official account , Get a learning video

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

Scroll to Top