Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to use the Java time utility class

Shulou Source: shulou.com Published: 2022-06-01 20:38:21 10月04日 Update

This article mainly introduces "how to use the Java time tool class". In daily operation, I believe many people have doubts about how to use the Java time tool class. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "how to use the Java time tool class". Next, please follow the editor to study!

Don't say much, just go to the code.

Package testDate;import java.text.SimpleDateFormat;import java.util.Calendar;import java.util.Date;// project case www.1b23.compublic class DateUtil {private static String YYYY_MM_DD = "yyyy-MM-dd"; private static String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss"; private static String YYYY_MM_DD_T_HH_MM_SS_SSS_Z = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'" Public static void main (String [] args) {/ / previous year Date D1 = getLastNTime (Calendar.YEAR,-1); System.out.println ("previous year:" + DateToStr (D1, YYYY_MM_DD_HH_MM_SS)); / / the next day Date D2 = getLastNTime (Calendar.DATE, 1); System.out.println ("the next day:" + DateToStr (D2, YYYY_MM_DD_HH_MM_SS)); / / conversion to UTC time Date D3 = convertToUTC (new Date ()) System.out.println ("UTC time:" + DateToStr (d3, YYYY_MM_DD_T_HH_MM_SS_SSS_Z)); / / get the last day of the current month Date d4 = getCurrentMonthLastDay (); System.out.println ("the last day of the current month:" + DateToStr (d4, YYYY_MM_DD)); / / get the number of days of the current month System.out.println ("days of the current month:" + getCurrentMonthDayCount ()) / / get the number of days in the current year System.out.println ("days in the previous year:" + getCurrentYearDayCount ());} / * get the last n time of the current time (year / month / day.) * @ param timeType time type (year / month / day.) * @ param n * @ return * / private static Date getLastNTime (int timeType, int n) {Calendar c = Calendar.getInstance (); c.add (timeType, n) Return c.getTime ();} / * convert to UTC time * @ param date * @ return * / private static Date convertToUTC (Date date) {Calendar c = Calendar.getInstance (); c.setTime (date); / / time offset int zoneOffset = c.get (Calendar.ZONE_OFFSET); / / Daylight Saving time int dstOffset = c.get (Calendar.DST_OFFSET) / / by deducting these differences from the time, you can obtain the UTC time c.add (Calendar.MILLISECOND,-(zoneOffset + dstOffset)); return c.getTime ();} / * * get the last day of the current month * @ return * / private static Date getCurrentMonthLastDay () {Calendar c = Calendar.getInstance (); c.set (Calendar.DAY_OF_MONTH, c.getActualMaximum (Calendar.DAY_OF_MONTH)); return c.getTime () } / * get current month days * @ return * / private static int getCurrentMonthDayCount () {Calendar c = Calendar.getInstance (); return c.getActualMaximum (Calendar.DAY_OF_MONTH);} / * get current year days * @ return * / private static int getCurrentYearDayCount () {Calendar c = Calendar.getInstance (); return c.getActualMaximum (Calendar.DAY_OF_YEAR) } / * formatting time * @ param date * @ param formatType * @ return * / private static String DateToStr (Date date, String formatType) {SimpleDateFormat sdf = new SimpleDateFormat (formatType); return sdf.format (date);}} at this point, the study on "how to use the Java time tool class" is over. I hope you can 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 days tools learning more help practical next code daylight saving article method jet lag format case theory knowledge article type website Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Microsoft Shulou Tech Info macOS Huawei MariaDB