编程知识 cdmana.com

Getting started with Java lesson 104 - output a year later minus three months of date

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 . Just follow my route , You can't find me .

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 , Output the current date based on one year and then subtract 3 The year of the month 、 month 、 Japan .

programme

     First , Use Calendar Class getInstance Method to get the current date - Time ; then , Use Calendar Class add Method , Plus a year ; Last , Use add Method , add -3 month , Minus 3 Month (s) and output the date (s) after calculation .

step

Step one : Build test methods

     First , stay TestCalendar Class to add test methods testAdd, then , Use Calendar Class getInstance Method to get the current date - Time ; Last , Use Calendar Class add Method , Plus a year , The code is as follows :

    package day03;
    
    import java.util.Calendar;
    import java.util.GrogorianCalendar;
    import org.junit.Test;
    
    public class TestCalendar{
     /**
      * Output a year later and subtract 3 The date of the month 
      */
      
      @Test
      public void testAdd(){
          Calendar calendar=Calendar.getInstance();
          calendar.add(Calendar.YEAR,1);// Add a year 
      }
    }

Step two : Use add Method , To achieve subtraction 3 Months

     Again , Use add Method , add -3 month , Minus 3 Month (s) and output the date (s) after calculation , The code is as follows :

    package day03;
    
    import java.util.Calendar;
    import java.util.GrogorianCalendar;
    import org.junit.Test;
    
    public class TestCalendar{
     /**
      * Output a year later and subtract 3 The date of the month 
      */
      
      @Test
      public void testAdd(){
          Calendar calendar=Calendar.getInstance();
          calendar.add(Calendar.YEAR,1);// Add a year 
          
          calendar.add(Calendar.MONTH,-3);// reduce 3 Months 
          System.out.println("year:"+calendar.get(Calendar.YEAR));
          System.out.println("month:"+(calendar.get(Calendar.MONTH)+1));
          System.out.println("day:"+calendar.get(Calendar.DAY_OF_MONTH));
      }
    }

     Note here ,Calendar.MONTH The field represents the month , The starting value of the month is not 1, It is 0, So you need to add... To the value of the month you get 1 To match the actual month .

Step three : function

     function testAdd Method , The console output is as follows :( notes : My run time is 2014 year 2 month 24 Japan )

    year:2014
    month:11
    day:24

     As can be seen from the output , Realize the output of the current date based on a year later minus 3 The year of the month 、 month 、 Japan .

Official account , Get a learning video

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

Scroll to Top