Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

API test for date and time in JDK8

2025-01-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article focuses on "API testing on date and time in JDK8". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn the API Test on date and time in JDK8.

Catalogue

API test for date and time in JDK 8

API testing of dates and times prior to JDK 8

/ / currentTimeMillis () public void test1 () {long time = System.currentTimeMillis () in the 1.System class; / / returns the time difference in milliseconds between the current time and 00:00:00 on January 1st, 1970. / / called timestamp System.out.println (time); / / 1628416744054}

/ * java.util.Date class |-java.sql.Date class (the relationship between two child parent classes) 1. Use of two constructors > constructor 1: Date (): create a Date object corresponding to the current time > constructor 2: create a Date object with a specified number of milliseconds. The use of two methods > toString (): displays the current year, month, day, hour, minute, second > getTime (): gets the number of milliseconds corresponding to the current Date object. (timestamp) 3.java.sql.Date corresponds to a date type variable in the database > how to instantiate > how to convert a java.util.Date object to a java.sql.Date object * / @ Test public void test2 () {/ / Constructor 1: Date (): create a Date object corresponding to the current time Date date1 = new Date () System.out.println (date1.toString ()); / / Sun Aug 08 18:07:27 CST 2021 / / Constructor II: create a specified millisecond Date object Date date2 = new Date (1534798293927L); System.out.println (date2.toString ()); / / Tue Aug 21 04:51:33 CST 2018 / / create a java.sql.Date object java.sql.Date date3 = new java.sql.Date (237493269533L) System.out.println (date3); / / 1977-07-12 / / how to convert java.util.Date objects to java.sql.Date objects / / case 1: (object-oriented) / / Date date4 = new java.sql.Date (23682368236343L); / / java.sql.Date date5 = (java.sql.Date) date4; / / case II: Date date6 = new Date () Java.sql.Date date7 = new java.sql.Date (date6.getTime ());}

/ * use of SimpleDateFormat: SimpleDateFormat formatting and parsing the date Date class 1. Two operations: 1.1.Formatting: date-> string parsing: inverse process of formatting: string-> instantiation of date 2.SimpleDateFormat * / @ Test public void testSimpleDateFormat () throws ParseException {/ / instantiating SimpleDateFormat SimpleDateFormat sdf = new SimpleDateFormat (); / / formatting: date-> string Date date = new Date () System.out.println (date); String format = sdf.format (date); System.out.println (format); / / parsing: the inverse process of formatting, string-> date String str = "21-8-9 3:17"; Date date1 = sdf.parse (str); System.out.println (date1) / / * format and parse in the specified way: call the constructor with parameters * * / / SimpleDateFormat sdf1 = new SimpleDateFormat ("yyyyy.MMMMM.dd GGG hh:mm aaa"); SimpleDateFormat sdf1 = new SimpleDateFormat ("yyyy-MM-dd hh:mm ss") / / format String format1 = sdf1.format (date); System.out.println (format1); / / 2021-08-09 04:02 13 / / parse: the string must be in accordance with the format recognized by SimpleDateFormat (reflected by constructor parameters), otherwise, report the exception Date date2 = sdf1.parse ("2019-08-09 04:02 13"); System.out.println (date2) Exercise 1: convert the string "2020-09-08" to java.sql.Date * / @ Test public void testExer () throws ParseException {String brith = "2020-09-08"; SimpleDateFormat sdf1 = new SimpleDateFormat ("yyy-MM-dd"); Date date = sdf1.parse (brith); java.sql.Date brithDate = new java.sql.Date (date.getTime ()) System.out.println (brithDate);}

/ * use of the Calendar calendar class (abstract class) * / @ Test public void testCalendar () {/ / 1. Instantiate / / way 1: create its subclass (GregorianCalendar) object / / way 2: call its static method getInstance () Calendar calendar = Calendar.getInstance (); / / 2. Common methods / / get () int days = calendar.get (Calendar.DAY_OF_MONTH); System.out.println (days); System.out.println (calendar.get (Calendar.DAY_OF_YEAR)); / / set () calendar.set (Calendar.DAY_OF_MONTH,22); days = calendar.get (Calendar.DAY_OF_MONTH); System.out.println (days) / / add () calendar.add (Calendar.DAY_OF_MONTH,-3); days = calendar.get (Calendar.DAY_OF_MONTH); System.out.println (days); / / getTime (): calendar class-- > Date Date date1 = calendar.getTime (); calendar.setTime (date1); days = calendar.get (Calendar.DAY_OF_MONTH) API test for date and time in System.out.println (days);} JDK 8

LocalDate, LocalTime and LocalDateTime classes are some of the more important classes, and their instances are immutable objects, representing the date, time, date and time of the calendar system using ISO-8601, respectively. They provide a simple local date or time and do not contain current time information or information related to the time zone.

> LocalDate represents a date in IOS format (yyyy-MM-dd), which can store birthdays, anniversaries and other dates. > LocalTime represents a time, not a date.

> LocalDateTime is used to represent date and time, which is one of the most commonly used classes.

Note: the ISO-8601 calendar system is the representation of the date and time of modern citizens formulated by the International Organization for Standardization, that is, the Gregorian calendar.

/ * use of LocalDate,LocalTime,LocalDateTime * / public void test1 () {/ / now (): get the current date, time, date + time LocalDate localDate = LocalDate.now (); LocalTime localTime = LocalTime.now (); LocalDateTime localDateTime = LocalDateTime.now (); System.out.println (localDate); / / 2021-08-09 System.out.println (localTime) / / 21 09T21:19:25.264 1915 25. 264 System.out.println (localDateTime); / / 2021-08-09T21:19:25.264 / / of (): sets the specified year, month, day, hour, minute, second. No offset LocalDateTime localDateTime1 = LocalDateTime.of (2020 localDateTime1 10, 6 13, 23 43); System.out.println (localDateTime1); / / getXxx (): get related attributes System.out.println (localDateTime.getDayOfMonth ()); System.out.println (localDateTime.getDayOfWeek ()); System.out.println (localDateTime.getMonth ()); System.out.println (localDateTime.getMonthValue ()) System.out.println (localDateTime.getMinute ()); / / immutability / / withXxx (): set relevant properties LocalDate localDate1 = localDate.withDayOfMonth (22); System.out.println (localDate); System.out.println (localDate1); LocalDateTime localDateTime2 = localDateTime.withHour (4); System.out.println (localDate); System.out.println (localDateTime2);}

/ * the use of Instant is similar to the java.util.Date class * / @ Test public void test2 () {/ / now (): get the standard time of the prime meridian Instant instant = Instant.now (); System.out.println (instant); / / 2021-08-09T15:08:46.818Z / / offset of the adding time OffsetDateTime offsetDateTime = instant.atOffset (ZoneOffset.ofHours (8)) System.out.println (offsetDateTime); / / 2021-08-09T23:08:46.818+08:00 / / toEpochMilli (): get the number of milliseconds since 00:00:00 on January 1, 1970 (UTC)-- > getTime () long milli of the Date class = instant.toEpochMilli (); System.out.println (milli) / / 1628521726818 / / ofEpochMilli (): obtain the Instant instance by the given number of milliseconds-> Date (long millis) Instant instant1 = Instant.ofEpochMilli (12243455253L); System.out.println (instant1) / / 1970-05-22T16:57:35.253Z} / * DateTimeFormatter: formatted or parsed date\ time is similar to SimpleDateFormat * / @ Test public void test3 () {/ / method 1: predefined standard format, such as: ISO_LOCAL_DATE_TIME;ISO_LOCAL_DATE;ISO_LOCAL_TIME DateTimeFormatter formatter = DateTimeFormatter.ISO_LOCAL_DATE_TIME / / format: date-> string LocalDateTime localDateTime = LocalDateTime.now (); String str1 = formatter.format (localDateTime); System.out.println (localDateTime); / / 2021-08-09T23:43:52.820 System.out.println (str1) / / 2021-08-09T23:43:52.82 / / parse: string-- > date / / TemporalAccessor parse = formatter.parse ("2021-08-09T23:43:52.82"); / / System.out.println (parse); / / method 2: localization-related format, such as: ofLocalizedDateTime// FormatStyle.LONG / FormatStyle.MEDIUM / FormatStyle.SHORT: applicable to LocalDateTime DateTimeFormatter formatter1 = DateTimeFormatter.ofLocalizedDateTime (FormatStyle.LONG) / / format: String str2 = formatter1.format (localDateTime); System.out.println (str2); / / 10:32:48 on August 10, 2021 / / Localization-related formats, such as: ofLocalizedDate () / / FormatStyle.FULL / FormatStyle.LONG / FormatStyle.MEDIUM / FormatStyle.SHORT: for LocalDate DateTimeFormatter formatter2 = DateTimeFormatter.ofLocalizedDate (FormatStyle.MEDIUM) / / format String str3 = formatter2.format (LocalDate.now ()); System.out.println (str3); / / 2021-8-10 / / focus! Method 3: custom format. For example: ofPattern ("yyyy-MM-dd hh:mm:ss") DateTimeFormatter formatter3 = DateTimeFormatter.ofPattern ("yyyy-MM-dd hh:mm:ss"); / / formatted String str4 = formatter3.format (LocalDateTime.now ()); System.out.println (str4); / / 2021-08-10 10:53:40 / / parsed TemporalAccessor accessor = formatter3.parse ("2021-08-10 10:53:40") System.out.println (accessor); / / {MinuteOfHour=53, NanoOfSecond=0, MicroOfSecond=0, HourOfAmPm=10, SecondOfMinute=40, MilliOfSecond=0}, ISO resolved to 2021-08-10} so far, I believe you have a better understanding of "API testing on date and time in JDK8". You might as well do it in practice! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report