inotgo.com


Tool version compatibility problem
Date class
be careful : yes java.util.Date;
Not java.sql.Date, This class is used when accessing the database
Correct

This video is interpretive , So I hope you have read the content of this knowledge point , And after writing the corresponding code , Watch with questions , Only in this way can we gain more . It is not recommended to watch the video at the beginning



9 branch 38 second
This video uses html5 Play mode , If it cannot be played normally , Please upgrade your browser to the latest version , Recommend Firefox ,chrome,360 browser . If thunderbolt is installed , Play the video and show the direct download status , Please adjust Thunderbolt system settings - Basic settings - Start - Monitor all browsers ( Remove this option ). chrome of Video download Plug-in will affect playback , as IDM etc. , Please close or switch other browsers



Example 1 : The concept of time origin   
Example 2 : Create date object   
Example 3 : getTime   
Example 4 : System.currentTimeMillis()   
Example 5 : practice - Date   
Example 6 : answer - Date   

Example 1 :

The concept of time origin

edit top fracture
All data types , Whether it's an integer , Bull , Floating point number or string , Finally, it needs to be expressed in the form of numbers .

Date types are no exception , let me put it another way , A date , For example 2020 year 10 month 1 day , In the computer , Will use a number instead of .

So the most special number , It's zero . The number zero , It means Java The origin of time in , The corresponding date is 1970 year 1 month 1 day 8 Point 0 branch 0 second . ( Why 8 Point , Because China's Pacific time zone is UTC-8, Just short of Greenwich mean time 8 An hour )

Why does it correspond to 1970 Years ? Because 1969 In, the first UNIX edition :AT&T, Consider comprehensively , At that time 1970 Year is the origin of time .

All the dates , All thought this 0 Point as datum , Every millisecond , Just +1.
 Create date object
package date; // import java.util.Date; public class TestDate { public static void main(String[] args) { // Current time Date d1 = new Date(); System.out.println(" Current time :"); System.out.println(d1); System.out.println(); // From 1970 year 1 month 1 day morning 8 Point 0 branch 0 second The number of milliseconds that begin to elapse Date d2 = new Date(5000); System.out.println(" From 1970 year 1 month 1 day morning 8 Point 0 branch 0 second Began to experience 5 Seconds "); System.out.println(d2); } }
getTime() Get a long Integer of type
This integer represents From 1970.1.1 08:00:00:000 Start Every millisecond , Add 1
Print objects directly , You'll see “Tue Jan 05 09:51:48 CST 2016” Such a format , Poor readability , In order to obtain “2016/1/5 09:51:48” Such a format Please refer to Date formatting
getTime
package date; // import java.util.Date; public class TestDate { public static void main(String[] args) { // be careful : yes java.util.Date; // Not java.sql.Date, This class is used when accessing the database Date now= new Date(); // Print the current time System.out.println(" Current time :"+now.toString()); //getTime() Get a long Integer of type // This integer represents 1970.1.1 08:00:00:000, Every millisecond , Add 1 System.out.println(" Current time getTime() The returned value is :"+now.getTime()); Date zero = new Date(0); System.out.println(" use 0 As a construction method , The date obtained is :"+zero); } }
Example 4 :

System.currentTimeMillis()

edit top fracture
The number of milliseconds of the current date
new Date().getTime() and System.currentTimeMillis() It's the same
However, due to the performance of the machine , There may be a difference of tens of milliseconds , After all, every line of code executed , It takes time
System.currentTimeMillis()
package date; // import java.util.Date; public class TestDate { public static void main(String[] args) { Date now= new Date(); // The number of milliseconds of the current date System.out.println("Date.getTime() \t\t\t Return value : "+now.getTime()); // Through System.currentTimeMillis() Gets the number of milliseconds of the current date System.out.println("System.currentTimeMillis() \t Return value : "+System.currentTimeMillis()); } }
package date;
  
//
import java.util.Date;
  
public class TestDate {
  
    public static void main(String[] args) {
        Date now= new Date();
 
        // The number of milliseconds of the current date 
        System.out.println("Date.getTime() \t\t\t Return value : "+now.getTime());
        // Through System.currentTimeMillis() Gets the number of milliseconds of the current date 
        System.out.println("System.currentTimeMillis() \t Return value : "+System.currentTimeMillis());
          
    }
}
Example 5 :

practice - Date

edit top fracture   Wrong posture , get half the results with double the effort ! Click to see the correct posture for the exercise
With the help of Random number , Create one from 1995.1.1 00:00:00 To 1995.12.31 23:59:59 Random dates between
 practice - Date
Before looking at the answers , Try to finish it yourself first , See the answer when you encounter a problem , The harvest will be more
Before looking at the answers , Try to finish it yourself first , See the answer when you encounter a problem , The harvest will be more
Before looking at the answers , Try to finish it yourself first , See the answer when you encounter a problem , The harvest will be more
Viewing this answer will cost 4 Points , You currently have a total of Point integral . It doesn't cost extra points to see the same answer . Points increase method Or One time purchase JAVA Base total 0 One answer ( Total required 0 Integral )
Viewing this answer will cost 4 Points , You currently have a total of Point integral . It doesn't cost extra points to see the same answer . Points increase method Or One time purchase JAVA Base total 0 One answer ( Total required 0 Integral )
Account not activated Account not activated , Limited functionality . Please click activate
This video is interpretive , So I hope you have read the content of this answer , Watch with questions , Only in this way can we gain more . It is not recommended to watch the video at the beginning

6 branch 52 second This video uses html5 Play mode , If it cannot be played normally , Please upgrade your browser to the latest version , Recommend Firefox ,chrome,360 browser . If thunderbolt is installed , Play the video and show the direct download status , Please adjust Thunderbolt system settings - Basic settings - Start - Monitor all browsers ( Remove this option ). chrome of Video download Plug-in will affect playback , as IDM etc. , Please close or switch other browsers


package date; import java.util.Date; public class TestDate { public static void main(String[] args) { long second = 1000; long minute = 60*second; long hour = minute * 60; long day = hour*24; long year = day * 365; long year1995Start = (1995-1970) * year; long leapDay = (1995-1970)/4*day; // Every 4 There is a moist day in year1995Start+=leapDay; long eightHour = hour*8; //8 The number of milliseconds in an hour , Because from 0 Milliseconds corresponds to 1970.1.1 08:00:00 year1995Start-=eightHour; Date dStart = new Date(year1995Start); System.out.println("1995 First day of the year :"+dStart); long year1995End = year1995Start + year - 1; Date dEnd = new Date(year1995End); System.out.println("1995 The last day of the year :"+dEnd); long randomTime = (long) (Math.random()*(year-1) + year1995Start); Date dRandom = new Date(randomTime); System.out.println("1995 A random time of the year :" + dRandom); } }


The official account of programming , Follow and get the latest tutorials and promotions in real time , thank you .


Q & A area    
2021-09-11 answer
mzrwalzy

about JAVA Basics - Date -Date Your questions



answer
import java.util.Calendar;
import java.util.Date;
import java.util.Random;

public class TestDate {

    public static void main(String[] args) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(1995, Calendar.DECEMBER, 31, 23, 59, 59);
        long lastTimeStamp = calendar.getTimeInMillis();
        calendar.set(1995, Calendar.JANUARY, 1, 0, 0, 0);
        long firstTimeStamp = calendar.getTimeInMillis();
        long l = firstTimeStamp + (long) (new Random().nextDouble() * (lastTimeStamp - firstTimeStamp));
        System.out.println(new Date(l));
    }
}
 
               


1 One answer

szy000178519
Answer time :2021-10-16



The answer has been submitted successfully , Auditing . Please My answer Check the answer record at , thank you
answer Or code please Fill in at least one , If you have a problem , Please ask again , Otherwise, the webmaster may not see





2021-08-13 My answer
halfup

about JAVA Basics - Date -Date Your questions



I don't know if the answer is right
 Loading
package Date;

import java.util.Date;

public class Test {
    public static void main(String[] args)  {
        Date start=new Date(95,0,1,0,0,0);
        Date end=new Date(95,11,31,23,59,59);
        System.out.println("1995 First day of the year "+start);
        System.out.println("1995 The last day of the year :"+end);


        long random1=(long)(Math.random()*(end.getTime()-start.getTime()+1)+start.getTime());
        Date random=new Date(random1);

        System.out.println("1995 Random time of the year :"+random);



    }
}
 
               


1 One answer

Ayuaa
Answer time :2021-08-15
package Basis.practice10.Study1; import java.text.ParseException; import java.text.SimpleDateFormat; import java.time.Year; import java.util.Date; import static java.lang.Math.*; public class MyClass { public static void main(String[] args) { var time1 = new Date(); System.out.println(time1); var time2 = new Date(0);//Thu Jan 01 08:00:00 CST 1970 Milliseconds elapsed System.out.println(time2); System.out.println(time1.getTime()); System.out.println( System.currentTimeMillis()); SimpleDateFormat sdf =new SimpleDateFormat("yyyy.MM.dd HH:mm:ss" ); String str_start = "1995.1.1 00:00:00"; String str_end = "1995.12.31 24:00:00"; try { Date d_start = sdf.parse(str_start); Date d_end = sdf.parse(str_end); long year_min = d_end.getTime()-d_start.getTime(); System.out.println(year_min); Date d_random = new Date((long)(random() *year_min)); System.out.println(d_random); System.out.println(sdf.format(d_random)); } catch (ParseException e) { // TODO Auto-generated catch blocK e.printStackTrace(); } } }



The answer has been submitted successfully , Auditing . Please My answer Check the answer record at , thank you
answer Or code please Fill in at least one , If you have a problem , Please ask again , Otherwise, the webmaster may not see





2021-08-12 answer
2021-07-21 2021-7-21-AM-8-40
2021-07-16 My answer


Too many questions , Page rendering is too slow , To speed up rendering , Only a few questions are displayed on this page at most . also 58 Previous questions , please Click to view

Please... Before asking questions land
The question has been submitted successfully , Auditing . Please My question Check the question record at , thank you
about JAVA Basics - Date -Date Your questions

Try to provide Screenshot code and Abnormal information , Help to analyze and solve problems . You can also enter this station QQ Group communication : 496725845
Ask questions and try to provide complete code , Environment description , The more conducive to the recurrence of the problem , The faster your question can be answered .
Have questions about the code in the tutorial , Please provide which step , Which line is in doubt , This makes it easy to quickly locate the problem , Improve the speed at which questions are answered
In the thousands of questions that already exist , A considerable proportion , Because of the use of and webmaster Different versions of the development environment Resulting in , For example jdk, eclpise, idea, mysql,tomcat Wait, the version of the software is inconsistent .
Please use the same version as the webmaster , You can save yourself a lot of learning time . The webmaster sorted out the software versions used in teaching , It's all here , Convenient for everyone to download : /k/helloworld/helloworld-version/1718.html

Upload screenshot