20155208 Xu Zihan 2016-2017-2 "Java Programming" 7th Week study Summary

Source: Internet
Author: User
Tags class definition date1 dateformat time and date

20155208 Xu Zihan 2016-2017-2 "java program Design" 7th Week study summary textbook study content summary 13th time and date

13.1 Recognize time and date

    • For now, even if the label is GMT (either a file description or a date-time string description of the API), it actually refers to the UTC time.

    • The unit definition of the second is based on the Tai, which is the number of radiation vibrations of the cesium atom.

    • Epoch is the beginning of a particular era, a moment on the timeline.

    • UTC takes into account that the rotation of the earth is slower and has a leap-second correction, ensuring that UTC differs from UT by no more than 0.9 seconds.

    • Unix time is the number of seconds elapsed from January 1, 1970 00:00:00 for the starting point, regardless of leap seconds.

13.2 Understanding Date and Calendar

If you want to get system time, one method is to use the System.currentTimeMillis() method, which returns a long integer. Such as:

import java.util.*; Import static java.lang.system.*; Public class datedemo {public  Static void Main (string[] args) { date date1 = new Date ( Currenttimemillis ()); date date2 = new Date (); Out.println (Date1.gettime ()); Out.println (Date2.gettime ()); }} 
    • Date has two constructors that can be used, one can be built using the epoch millisecond number, the other is no argument constructor, and the internal is using System.currenttimemillis () to get the number of milliseconds. Call GetTime () to obtain an internally saved epoch millisecond value. The sample execution results are as follows:
    • DateFormat is an abstract class whose operating class is Java.text.SimpleDateFormat, you can build SimpleDateFormat instances directly, or use DateFormat getdateinstance (), Gettimeinstance (), getdatetimeinstance and other static methods, in a more convenient way to obtain SimpleDateFormat instances according to different requirements.

    • The benefit of building SimpleDateFormat directly is that you can use a pattern string to customize the format.

*simpledateformat also has a parse () method that can parse the specified string into a date instance in the format specified when the SimpleDateFormat was built. Such as:

Import Java.util.*;import java.text.*;PublicClass Howold {public static void main (string[] args) throws Exception { System.out.print ( "Enter Birth date (YYYY-MM-DD):"); DateFormat DateFormat = new SimpleDateFormat ( "YYYY-MM-DD"); Date birthDate = dateformat.parse (new Scanner (system.in). nextline ()); Date currentdate = new date (); long life = Currentdate.gettime ()-birthdate.gettime (); System.out.println ( "Your Age this year is:" + (Life/(365 * 24 * 60 * 60 * 1000L)); }} 
    • Date is now recommended as an instant on the timeline, to format the time date through DateFormat, and to use the calendar instance if you want to get a time-date information or to manipulate the time-date. Such as:

Calendar calendar = Calendar.getinstance (); Calendar is an abstract class, Java.util.GregorianCalendar is its subclass, manipulating the Julian calendar and Gregorian calendar of the mixed calendar, through the calendar getinstance () to obtain the calendar instance, the default is to obtain the GregorianCalendar instance. Such as:

Out.println (Calendar.get (calendar.year)); Out.println (Calendar.get (calendar.month)); Out.println (Calendar.get (calendar.date)); To obtain the default time zone information, you can use the Java.util.TimeZone Getdefault () method. Such as:

import static java.lang.system. Out;import Java.util.TimeZone; public class TimeZoneDemo {public static void main (String[] args) {TimeZone TimeZone = Timezone.getdefault (); out.println (Timezone.getdisplayname ()); out.println ( "\ t time zone ID:" + timezone.getid ()); out.println (" \ t Daylight Time: "+ timezone.getdstsavings ()); out.println ( "\TUTC offset milliseconds:" + timezone.getrawoffset ());}}   

13.3 JDK8 New Time Date API

    • You can use the static method of instance now () to get the number of milliseconds representing the Java epoch. After obtaining the instance instance, you can use Plusseconds (), Plusmills (), Plusnanos (), Minusseconds (), Minusmills (), Minusnanos () to do the operation on the timeline, The instance instance itself will not change, and these operations will return the new instance instance, representing the instantaneous after the operation.

    • If you get a date instance and want to use instance instead, you can call the date city's ToInstance () method to get it, and if you have a instance instance, you can use Date's static method from () to date.

    • Class names such as LocalDateTime, Localdate, localtime, and so on, start with local, indicating that they are all just a description of the time and no time zone information.

    • For time measurement, the new time and date API is defined by class duration, which can be used to measure the difference in days, hours, minutes, seconds, precision adjustment can be the Dana second level, and the maximum value of seconds can be a long type to save the value. For the time difference between year, month, week, and day, use the period class definition. Such as:

import java.time.*; import Java.util.Scanner; import static java.lang.System.out; public class HowOld2 { public static void Main (string[] args) {Out.print (" Enter Birth date (YYYY-MM-DD): "); Localdate birth = localdate.parse (new Scanner (System.  IN). Nextline ()); Localdate now = Localdate.now (); Period Period = Period.between (birth, now); out.printf ("You live%d%d months%d days%n", Period.getyears (), Period.getmonths (), period.getdays ());}}   
Problems in teaching materials learning and the solving process

Problem:

After studying the operation of 13.3.3 on time, it is found that period and duration are very similar, and it is unclear where the concrete difference is.

Resolution process:

The following differences are summed up by learning materials and surfing the Internet:

Period is a date difference, the Between () method accepts only localdate, does not represent a smaller unit than "Day", however duration is a time difference, between () can accept temporal operations objects, which means that you can use Localdate, LocalTime, LocalDateTime to calculate the Duration, does not represent a larger unit than "day".

Problems in code debugging and the resolution process

Problem:

"Calender calender = (calender) Begin.clone ()" In the book p435 page code snippet (below) is not understood.

public static long Yearsbetween (calendar begin, Calendar end) {Calendar calendar = (Calendar) begin.clone (); Long years = 0; while (Calendar.before (end)) {Calendar.add (calendar.year, 1); years++;} return years-1; }

public static long daysBetween(Calendar begin, Calendar end) { Calendar calendar = (Calendar) begin.clone(); long days = 0; while (calendar.before(end)) { calendar.add(Calendar.DATE, 1); days++; } return days - 1; }

Resolution process:

By reading the code example of the analysis, get the following explanation:

If an operation such as add () is made on the calendar instance, the calendar instance itself is modified, in order to avoid the passing of the calendar argument that was passed after calling Yearsbetween (), Daysbetween (), to be modified. Each of the two methods clones () The action of the first argument to copy the object.

Other (sentiment, thinking, etc., optional)

This week I learned the 13th chapter, I learned some historical issues of time and date, and I know how to use Java programs to handle time and date, and I think the most important thing in the new time and date processing API is to clearly separate the concept of time from the human concept Let the machine and human to the time concept of the boundaries become clear, the sense of harvest is great.

Last week's summary of the wrong quiz

Which of the following commands can copy F1.txt to F2.txt?

A. CP F1.txt F2.txt

B. Copy F1.txt F2.txt

C. Cat F1.txt > F2.tx

D. CP F1.txt | F2.tx

E. Copy F1.txt | F2.tx

Answer: A C

3. The following () method will cause the thread to go into a blocking state?

A. Thread.Sleep ()

B. Wait ()

C. Notify ()

D. Interrupt ()

Answer: A and B

Code hosting reviewed the classmates blog and code

Study No. 1

Study No. 2

Study No. 3

Study No. 4

Study No. 5

Learning progress Bar new/accumulated
Number of lines of code (new/accumulated) Blog Volume (learning Time (new/cumulative)
Goal 5000 rows 30 Articles 400 hours
First week 16/16 1/1 8/8
Second week 120/120 2/2 12/12
Third week 130/150 1/1 9/9
Week Four 180/200 1/1 15/15
Week Five 150/300 1/1 12/12
Week Six 120/300 1/1 8/8
Seventh Week 300/300 2/2 10/10
Resources
    • Java Learning Notes
    • Java Learning Notes (8th Edition) Learning Guide

20155208 Xu Zihan 2016-2017-2 Java programming 7th Week of study summary

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.