Java programming time and date API instance parsing, java programming api instance

Source: Internet
Author: User

Java programming time and date API instance parsing, java programming api instance

The examples in this article are mainly about the new features in Java 8 and related instances of the time and date api, as follows:

Package com. valid tive. common. base. date; import java. time. instant; import java. time. localDate; import java. time. localDateTime; import java. time. localTime; import java. time. period; import java. time. zoneId; import java. time. format. dateTimeFormatter; import java. time. temporal. chronoUnit; import java. util. date;/*** Date tool class * @ author yanweiqi * @ since 2016-5-6 **/public class LocalDateUtils {private static ZoneId zone = ZoneId. systemDefault ();/*** convert String to Date * @ param date * @ return * @ throws Exception */public static Date convertToDate (String date) throws Exception {LocalDate localDate = null; if (null = date) {throw new NullPointerException ("date isn't null");} else {localDate = LocalDate. parse (date); return convertToDate (localDate) ;}/ *** convert String to LocalDateTime * @ param date * @ return localDateTime */public static LocalDateTime convertToLocalDateTime (String date) {LocalDateTime localDateTime = null; if (null = date) {throw new NullPointerException ("date isn't null");} else {localDateTime = LocalDateTime. parse (date); return localDateTime;}/*** convert LocalDate to Date * @ param localDate * @ return Date */public static Date convertToDate (LocalDate localDate) {Instant instant = localDate. atStartOfDay (). atZone (zone ). toInstant (); return Date. from (instant);}/*** convert LocalDate to Date * @ param localDateTime * @ return Date */public static Date convertToDate (LocalDateTime localDateTime) {Instant instant = localDateTime. atZone (zone ). toInstant (); return Date. from (instant);}/*** Date to LocalDate * @ param date * @ return localDate */public static LocalDate convertToLocalDate (Date date) {Instant instant = date. toInstant (); return convertToLocalDateTime (instant ). toLocalDate ();}/*** Date to LocalTime * @ param date * @ return localDate */public static LocalTime convertToLocalTime (Date date) {Instant instant = date. toInstant (); return convertToLocalDateTime (instant ). toLocalTime ();}/*** Date to LocalDatetime * @ param date * @ return localDate */public static LocalDateTime convertToLocalDateTime (Date date) {Instant instant = date. toInstant (); return convertToLocalDateTime (instant);}/*** convert Instant to LocalDateTime * @ param instant * @ return */public static LocalDateTime convertToLocalDateTime (Instant instant) {return LocalDateTime. ofInstant (instant, zone);}/*** convert LocalDateTime to Instant * @ param localDateTime * @ return */public static Instant convertToInstant (LocalDateTime localDateTime) {return localDateTime. atZone (zone ). toInstant ();}/*** convert LocalDate to Instant * @ param localDate * @ return */public static Instant convertToInstant (LocalDate localDate) {return localDate. atStartOfDay (zone ). toInstant ();}/*** convert LocalDate to LocalDateTime * @ param localDate * @ return LocalDateTime */public static LocalDateTime convertToLocalDateTime (LocalDate localDate) {return localDate. atStartOfDay ();}/*** daily cycle formatting * @ param localDateTime * @ param formatStyle * @ return */public static String formatter (LocalDateTime localDateTime, String formatStyle) {return DateTimeFormatter. ofPattern (formatStyle ). format (localDateTime);}/*** set year * @ param sourceDate * @ param year * @ return LocalDateTime */public static LocalDateTime setYear (LocalDateTime sourceDate, Integer year) {return sourceDate. withYear (year);}/*** set month * @ param sourceDate * @ param month * @ return LocalDateTime */public static LocalDateTime setMonth (LocalDateTime sourceDate, Integer month) {return sourceDate. withMonth (month);}/*** set day * @ param sourceDate * @ param month * @ return LocalDateTime */public static LocalDateTime setDayOfMonth (LocalDateTime sourceDate, Integer dayOfMonth) {return sourceDate. withDayOfMonth (dayOfMonth);}/*** set hour * @ param sourceDate * @ param hour * @ return */public static LocalDateTime setHour (LocalDateTime sourceDate, Integer hour) {return sourceDate. withHour (hour);}/*** set minute * @ param sourceDate * @ param minute * @ return */public static LocalDateTime setMinute (LocalDateTime sourceDate, Integer minute) {return sourceDate. withMinute (minute);}/*** sets the second * @ param sourceDate * @ param second * @ return */public static LocalDateTime setSecond (LocalDateTime sourceDate, Integer second) {return sourceDate. withSecond (second );} /*** modify the year, month, and day * @ param sourceDate * @ param year * @ param month * @ param dayOfMonth * @ return */public static LocalDateTime setYMD (LocalDateTime sourceDate, Integer year, integer month, Integer dayOfMonth) {return sourceDate. withYear (year ). withMonth (month ). withDayOfMonth (dayOfMonth );} /*** modify hour/minute/second * @ param sourceDate * @ param hour * @ param minute * @ param second * @ return */public static LocalDateTime setHMS (LocalDateTime sourceDate, Integer hour, integer minute, Integer second) {return sourceDate. withHour (hour ). withMinute (minute ). withSecond (second);}/*** calculate the number of days of difference * @ param beginDate * @ param endDate * @ return */public static int getInteverDays (LocalDate beginDate, LocalDate endDate) {Period period = Period. between (beginDate, endDate); return period. getDays ();} /*** add or subtract a date * @ param num quantity * @ param unit * @ param LocalDate original date * @ return date added by LocalDate */@ SuppressWarnings ("static-access") public static LocalDate addLocalDate (long num, ChronoUnit, final LocalDate localDate) {LocalDate resultDate; if (num> 0) {resultDate = localDate. now (). plus (num, unit);} else {resultDate = localDate. now (). minus (Math. abs (num), unit);} return resultDate ;} /*** add the number of @ param num * @ param unit * @ param localDateTime to the original date * @ return date added by LocalDateTime */@ SuppressWarnings ("static-access ") public static LocalDateTime addLocalDateTime (long num, ChronoUnit, LocalDateTime localDateTime) {LocalDateTime resultDateTime; if (num> 0) {resultDateTime = localDateTime. now (). plus (num, unit);} else {resultDateTime = localDateTime. now (). minus (Math. abs (num), unit);} return resultDateTime ;} /*** add or subtract the number of @ param num * @ param unit * @ param localTime original date * @ return LocalDateTime added date */@ SuppressWarnings ("static-access") public static LocalTime addLocalTime (long num, ChronoUnit, LocalTime localTime) {LocalTime resultTime; if (num> 0) {resultTime = localTime. now (). plus (num, unit);} else {resultTime = localTime. now (). minus (Math. abs (num), unit);} return resultTime;} public static void main (String [] args) {LocalDateTime time = LocalDateTime. now (); String rr = formatter (time, "yyyy-MM-dd HH: mm: ss"); System. out. println (rr); LocalDateTime time2 = addLocalDateTime (-2, ChronoUnit. HOURS, time); String yy = formatter (time2, "yyyy-MM-dd HH: mm: ss"); System. out. println (yy );}

The Code involves the use of time and date classes and has simple annotations for your reference.

Summary

The above is all the content about parsing Java programming time and date API instances. I hope it will be helpful to you. If you are interested, you can continue to refer to other related topics on this site. If you have any shortcomings, please leave a message. Thank you for your support!

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.