编程知识 cdmana.com

Getting started with Java lesson 97 - using setTime and gettime methods to operate in milliseconds

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

     Use setTime and getTime Method to operate on the date in milliseconds - Time , The detailed requirements are as follows :

    1) obtain 1970 year 1 month 1 The number of milliseconds between day zero hour and current time .

    2) Get the date at this time tomorrow - Time .

programme

    1) First , Instantiation Date Class to get the current date - Time , The code is as follows :

Date date=new Date();

     then , call Date Class getTime Method , obtain 1970 year 1 month 1 The number of milliseconds between day zero hour and current time , The code is as follows :

long time=date.getTime();

    2) First , Instantiation Date Class to get the current date - Time date object , then , call Date Class getTime Method , obtain 1970 year 1 month 1 The number of milliseconds between day zero hour and current time , And calculate the current date - The number of milliseconds in a day , Last , call Date Class setTime Method to change the current date - Time date Set to add one day's date - Time .

step

     To implement this case, follow the steps below .

Step one : Build test methods

     First , In a JavaSE Under the project of src The new name is day03 My bag , then , Create a new class under the package TestDate, Add test methods to this class testGetTime, The code is as follows :

    package day03;
    
    import java.util.Date;
    import org.junit.Test;
    
    public class TestDate{
        /**
         * test getTime Method 
         */
         @Test
         public void testGetTime(){
         
         }
    }

Step two : obtain 1970 year 1 month 1 The number of milliseconds between day zero hour and current time

     First , Instantiation Date Class to get the current time ; then , call Date Class getTime Method , obtain 1970 year 1 month 1 The number of milliseconds between day zero hour and current time , The code is as follows :

    package day03;
    
    import java.util.Date;
    import org.junit.Test;
    
    public class TestDate{
        /**
         * test getTime Method 
         */
         @Test
         public void testGetTime(){
             Date date=new Date();
             System.out.println(date);
             
             //1970 year 1 month 1 The number of milliseconds between zero and the present 
             long time=date.getTime();
             System.out.println(time);
         }
    }

Step three : function

     function testGetTime Method , The running results are as follows :

    Mon Jan 13 17:56:52 CST 2014
    1389607012302

     Observe the output , function testGetTime Date of method - Time is 2014 year 1 month 13 Japan 17:56:52,1970 year 1 month 1 The number of milliseconds between day zero hour and the time is 1389607012302.

Step four : Implementation current date - Add one day to the time base

     First , Get current date - Time date, then , call Date Class getTime Method , obtain 1970 year 1 month 1 The number of milliseconds between day zero hour and current time , And calculate the current date - The number of milliseconds in a day , Last , call Date Class setTime Method to change the current date - Time date Set to add a day's date and time , The code is as follows :

    package day03;
    
    import java.util.Date;
    import org.junit.Test;
    
    public class TestDate{
        /**
         * test getTime Method 
         */
         @Test
         public void testGetTime(){
             Date date=new Date();
             System.out.println(date);
             
             //1970 year 1 month 1 The number of milliseconds between zero and the present 
             long time=date.getTime();
             System.out.println(time);
         }
         
         /**
          * test testSetTime Method 
          */
          @Test
          public void testSetTime(){
              Date date=new Date();
              
              // Output the date and time at this time of the day 
              System.out.println(date);
              
              long time=date.getTime();
              // Increase the number of milliseconds a day goes through 
              time+=60*60*24*1000;
              date.setTime(time);
              // Output the date and time at this moment tomorrow 
              System.out.println(date);
          }
    }

Step five : function

     function testSetTime Method , The running results are as follows :

    Mon Jan 13 18:02:38 CST 2014
    Tue Jan 14 18:02:38 CST 2014

     Observe the output , function testSetTime The time and date of the method is 2014 year 1 month 13 Japan 18:02:38, Add a day's time on this basis to 2014 year 1 month 14 Japan 18:02:38.

Official account , Get a learning video

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

Scroll to Top