Содержание
- 2. Agenda Java 7 Date Time API Java 8 Date Time API Typical Uses of I/O System
- 3. Java 7 Date Time API
- 4. Backstory The Date class was the work of James Gosling and Arthur van Hoff. Added in
- 5. The Date class The Date class available in java.util package, this class encapsulates the current date
- 6. Getting Current Date and Time The Date object stores information about current date and time. Methods
- 7. Useful Methods of the Date class long getTime() Returns the number of milliseconds that have elapsed
- 8. Problems Getting a Date class Conceptually an instant, not a date Properties have random offsets: Some
- 9. The GregorianCalendar Class The GregorianCalendar is a concrete implementation of a Calendar class that implements the
- 10. Useful Methods of the Calendar class void add(int field, int amount) Adds the specified (signed) amount
- 11. Useful Methods of the Calendar class void set(int field, int value) Sets the time field with
- 12. Problems Getting a Calendar class Conceptually an instant, not a calendar. Zero-based offsets Stores internal state
- 13. Java 8 Date Time API
- 14. Backstory 2002 - Stephen Colebourne starts open source Joda-Time project 2005 - Release of Joda-Time 1.0
- 15. New Packages java.time instants, durations, dates, times, time zones, periods. java.time.format formatting and parsing. java.time.temporal field,
- 16. Design Principles Distinguish between machine and human views. Well-defined and clear purpose. Immutable, thread-safe. Reject null
- 17. Commonly Used Classes LocalDate ISO 8601 date without time zone and time Corresponds to SQL DATE
- 18. The ISO 8601 Standard The International standard for representation of dates and times. Uses the Gregorian
- 19. The LocalDate Class Obtain a LocalDate object corresponding to the local date of today. Create a
- 20. The LocalDate Class Access the date information of a LocalDate object. int year = localDate.getYear(); Month
- 21. The LocalDate Class Simple date calculations with the LocalDate object. LocalDate localDate = LocalDate.of(2020, Month.MARCH, 16);
- 22. The LocalTime Class Create a LocalTime object that represents the exact time of now. Create a
- 23. The LocalTime Class Access the time information of a LocalTime object. int hour = localTime.getHour(); int
- 24. The LocalTime Class Simple time calculations with the LocalTime object. LocalTime localTime = LocalTime.of(15, 30, 25,
- 25. The LocalDateTime Class The LocalDateTime class represents a local date and time without any time zone
- 26. The DateTimeFormatter Class The DateTimeFormatter class is used to parse and format dates represented with the
- 27. The DateTimeFormatter Class The format() method is declared on both the formatter objects and the date/time
- 28. Others useful Classes and Interfaces Temporal Basic interface for DateTime classes LocalDate / LocalTime / LocalDateTime
- 29. The Instant Class Point on a discretized time-line Stored to nanosecond resolution long for seconds since
- 30. The Clock Class Gets the current instant using a time-zone Use instead of System.currentTimeMillis() Use an
- 31. The Period Class A length of elapsed time. Defined using calendar fields years, months, and days
- 32. The Duration and Chronology The Duration class Precise length of elapsed time, in nanoseconds Does not
- 33. Staying Constant Day of week, for example DayOfWeek.FRIDAY Month , for example Month.MAY Time units, for
- 34. Input/Output Streams API
- 35. What Is a Stream? A stream is an ordered sequence of bytes of undetermined length. Input
- 36. Streams Usual Purpose: Storing data to «nonvolatile» devices, e.g. harddisk. Classes provided by package java.io. Data
- 37. Streams JAVA distinguishes between 2 types of streams: Text Streams, containing “characters” Binary Streams, containing 8
- 38. Streams Streams in JAVA are Objects, of course! 2 types of streams (text / binary) and
- 39. The File Class A File object can refer to either a file: or a directory: File
- 40. The File Class String dirName = "D:\\Development"; String fileName = "data.txt"; File newDirectory = new File(dirName);
- 41. Useful Methods of the File class isFile() / isDirectory() Returns true if and only if the
- 42. Binary Files Stores binary images of information identical to the binary images stored in main memory.
- 43. Byte-Oriented Output Stream Classes The following is the byte-oriented output stream class hierarchy: OutputStream ByteArrayOutputStream FileOutputStream
- 44. Methods of OutputStream Class Writing data: write(...) methods write data to the stream. Written data is
- 45. Methods of OutputStream Class flush() To improve performance, almost all output protocols buffer output. Data written
- 46. Creating a FileOutputStream Instance String fileName = "data.txt"; String str = "Hello I/O!"; byte[] wData =
- 47. Byte-Oriented Input Stream Classes The following is the byte-oriented input stream class hierarchy: InputStream ByteArrayInputStream FileInputStream
- 48. Methods of InpupStream Class Reading data: read() methods will block until data is available to be
- 49. Methods of InpupStream Class available() Returns the number of bytes which can be read without blocking.
- 50. Creating a FileInputStream Instance byte[] rData = new byte[15]; try (FileInputStream fileInputStream = new FileInputStream(fileName)) {
- 51. Creating a FileInputStream Instance String fileName = "data.txt"; try (FileInputStream fileInputStream = new FileInputStream(fileName)) { int
- 52. Character-Oriented Writer Classes The following is the character-oriented output stream class hierarchy: Writer BufferedWriter CharArrayWriter OutputStreamWriter
- 53. Character-Oriented Reader Classes The following is the character-oriented input stream class hierarchy: Reader BufferedReader CharArrayReader FilterReader
- 54. FileReader and FileWriter Classes String fileName = "data.txt"; String data = "Hello Java I/O! Streams"; try
- 55. BufferedWriter and BufferedReader Classes The BufferedReader and BufferedWriter classes use an internal buffer to store data
- 56. BufferedWriter and BufferedReader Classes String fileName = "data.txt"; String data = "Hello Java I/O Streams!"; try
- 57. BufferedWriter and BufferedReader Classes try (BufferedReader bufferedReader = new BufferedReader( new FileReader(fileName))) { String line =
- 58. Useful Links Java Basic I/O Tutorial http://docs.oracle.com/javase/tutorial/essential/io Tutorialspoint Java.io package tutorial http://www.tutorialspoint.com/java/io
- 60. Скачать презентацию