Get the App
SLTechnology News&Howtos  ›  Development  › 

What are the common time-related methods in Java

Shulou Source: shulou.com Published: 2022-06-02 23:40:41 10月02日 Update

This article mainly introduces "what are the time-related methods commonly used in Java". In daily operation, I believe that many people have doubts about the time-related methods commonly used in Java. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful for you to answer the doubts about "what are the common time-related methods in Java?" Next, please follow the editor to study!

1. How to get the current time: public static void main (String [] args) {/ / Date Date now = new Date (); System.out.println (now); / / the time of java8 LocalDateTime localDateTime = LocalDateTime.now (); System.out.println (localDateTime); Calendar calendar = Calendar.getInstance (); Date time = calendar.getTime (); System.out.println (time) System.out.println (year + calendar.get (Calendar.YEAR)); System.out.println (month + (calendar.get (Calendar.MONTH) + 1)); / / joda time DateTime dateTime = DateTime.now (); System.out.println (dateTime);}

You can use Date LocalDatetime Calendar Datetime to get the current time

2. Get public static void main on the nth day of the month (String [] args) {/ / it is recommended to use Calendar to set Calendar calendar = Calendar.getInstance (); / 16 calendar.set (Calendar.DAY_OF_MONTH, 16); System.out.println (calendar.getTime ()); / / 16 DateTime now = DateTime.now (); DateTime dateTime = now.withDayOfMonth (16); System.out.println (dateTime) / / 14 LocalDateTime localDateTime = LocalDateTime.now (); System.out.println (localDateTime.withDayOfMonth (14)); / / January 11 System.out.println (localDateTime.withMonth (1). WithDayOfMonth (11));} III. Format to string ```/ / use SimpleDateFormatSimpleDateFormat format = new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss"); System.out.println (format.format (new Date (); / / use CalendarCalendar calendar = Calendar.getInstance () System.out.println (String.format ("% s year% s month% s day% s minutes% s seconds", calendar.get (Calendar.YEAR), calendar.get (Calendar.MONTH) + 1, calendar.get (Calendar.DAY_OF_MONTH), calendar.get (Calendar.HOUR_OF_DAY), calendar.get (Calendar.MINUTE), calendar.get (Calendar.SECOND)); LocalDateTime now = LocalDateTime.now () String str = now.format (DateTimeFormatter.ofPattern ("yyyy-MM-dd HH:mm:ss")); System.out.println (str); ```4. Add and subtract time (in seconds, hours, etc.) public static void main (String [] args) {Date now = new Date (); / / add one hour long time = now.getTime () + (60 * 60 * 1000); System.out.println (new Date (time)) / * cn.hutool hutool-all 5.7.14 * / / introduce Hutool plus one hour System.out.println (DateUtil.offset (now, DateField.HOUR, 1)); / / minus one hour System.out.println (DateUtil.offset (now, DateField.HOUR,-1)); LocalDateTime localDateTime = LocalDateTime.now (); System.out.println ("plus one hour" + localDateTime.plusHours (1)) System.out.println ("minus one hour" + localDateTime.minusHours (1)); DateTime dateTime = DateTime.now (); System.out.println (dateTime.plusHours (1)); System.out.println (dateTime.minusHours (1));}

Both LocalDateTime and DateTime have their own ways to increase and decrease time

5. Obtain the age public static void main (String [] args) {/ / time 1990-12-05 DateTime birthDay = DateTime.now (). WithYear (1990). WithMonthOfYear (10). WithDayOfMonth (23); System.out.println (birthDay); / / the difference between years will be compared with the month and date, such as Years years = Years.yearsBetween (birthDay, new DateTime (); System.out.println (years)) System.out.println (years.getYears ());}

Birthdays can also be obtained by subtracting years and comparing months and days.

6. Judge whether public static void main (String [] args) {DateTime now = DateTime.now (); DateTime start1 = now; DateTime end1 = now.plusMinutes (1); DateTime start2 = now.plusSeconds (50); DateTime end2 = now.plusMinutes (2); Interval interval1 = new Interval (start1, end1); Interval interval2 = new Interval (start2, end2); System.out.println (interval1.overlaps (interval2)) System.out.println (start1.getMillis () < end2.getMillis () & & start2.getMillis () < end1.getMillis ()); 7. Find two intervals public static void main (String [] args) {DateTime now = DateTime.now (); / / start time Date startTime = now.toDate (); / / end time Date endTime = now.plusHours (1). ToDate () / / 1 hour System.out.println ("interval between start time and end time:" + DateUtil.between (startTime, endTime, DateUnit.SECOND)); long time = (endTime.getTime ()-startTime.getTime ()) / 1000; System.out.println (time);} VIII. Conversion between UTC time and Beijing time public static void main (String [] args) throws ParseException {Date now = new Date (); Date utcDate = bj2UTC (now) / / utc time System.out.println (utcDate); / / Beijing time System.out.println (utc2BJ (utcDate)); DateTime dateTime = DateTime.now (). WithDayOfMonth (1). WithHourOfDay (0). WithMinuteOfHour (0). WithsecondOfMinute (0); System.out.println (dateTime); System.out.println (bj2UTC (dateTime.toDate ();} public static Date bj2UTC (Date date) {if (date = = null) {return null } LocalDateTime localDateTime = LocalDateTime.ofInstant (date.toInstant (), ZoneId.of ("- 8")); return Date.from (localDateTime.atOffset (ZoneOffset.UTC). ToInstant ());} public static Date utc2BJ (Date date) {if (date = = null) {return null;} LocalDateTime localDateTime = LocalDateTime.ofInstant (date.toInstant (), ZoneId.of ("+ 8")); return Date.from (localDateTime.atOffset (ZoneOffset.UTC). ToInstant ());}

Beijing time = UTC+8

At this point, the study of "what are the time-related methods commonly used in Java" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Tags: Time method hour Zhongchang study Beijing plus one two date more help practical next unit character string year year month day age Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Apple Docker Linux Redmi OPPO Reno