In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
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!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.