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

What are the common time format conversion tool classes in Java

2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

Editor to share with you what Java commonly used time format conversion tools are, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

In the development process, we often encounter the conversion of various time formats. Today, we hereby record the format conversion of various types of time that may be encountered in the development process in the way of blog, and introduce it in the way of tool class, which is convenient for everyone to use in development!

Public final class DataUtils {

/ * *

* preset different time formats

* /

/ / accurate to year, month and day (English) eg:2019-11-11

Public static String FORMAT_LONOGRAM = "yyyy-MM-dd"

/ / full time of exact minutes and seconds (English) eg:2010-11-11 12:12:12

Public static String FORMAT_FULL = "yyyy-MM-dd HH:mm:ss"

/ / accurate to millisecond full time (English) eg:2019-11-11 12 purl 12 purl 12.55

Public static String FORMAT_FULL_MILL = "yyyy-MM-dd HH:mm:ss.SSS"

/ / accurate to year, month, day (Chinese) eg: November 11, 2019

Public static String FORMAT_LONOGRAM_CN = "yyy year MM month dd day"

/ / full time of exact minutes and seconds (Chinese) eg: 12:12:12 on November 11, 2019

Public static String FORMAT_FULL_CN = "yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy

/ / accurate to millisecond full time (Chinese)

Public static String FORMAT_FULL_MILL_CN = "yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy

/ * *

* default time format is preset

* /

Public static String getDefaultFormat () {return FORMAT_FULL}

/ * *

* default format format date

* /

Public static String format (Date date) {

Return format (date, getDefaultFormat ())

}

/ * *

* Custom format format date

* /

Public static String format (Date date,String format) {

String value = ""

If (dateworthy null) {

SimpleDateFormat sdf = new SimpleDateFormat (format)

Value = sdf.format (date)

}

Return value

}

/ * *

* returns the current date according to the default format

* /

Public static String getNow () {return format (new Date ());}

/ * *

* customize the time format to return the current date

* /

Public static String getNow (String format) {return format (new Date (), format);}

/ * *

* according to the default time String- > Date

* /

Public static Date parse (String strDate) {return parse (strDate,getDefaultFormat)}

/ * *

* Custom time format: Stirng- > Date

* /

Public static Date parse (String strDate,String format) {

SimpleDateFormat sdf = new SimpleDateFormat (format)

Try {

Return sdf.parse (strDate)

} catch (ParseException e) {

E.printStackTrace ()

Return null

}

}

/ * *

* increase the year based on the specified date

* @ param num integer is pushed back, negative number is moved forward

* /

Public static Date addMobth (Date date,int num) {

Calendar= Calendar.getInstance ()

Cal.setTime (date)

Cal.add (Calendar.YEAR,num)

Return cal.getTime ()

}

/ * *

* add the whole month based on the specified date

* @ param num integer is pushed back, negative number is moved forward

* /

Public static Date addMobth (Date date,int num) {

Calendar= Calendar.getInstance ()

Cal.setTime (date)

Cal.add (Calendar.MONTH,num)

Return cal.getTime ()

}

/ * *

* increase the number of days based on a specified date

* @ param num integer is pushed back, negative number is moved forward

* /

Public static Date addDay (Date date, int num) {

Calendar cal = Calendar.getInstance ()

Cal.setTime (date)

Cal.add (Calendar.DATE, num)

Return cal.getTime ()

}

/ * *

* add minutes based on the specified date

* @ param num integer is pushed back, negative number is moved forward

* /

Public static Date addMinute (Date date, int num) {

Calendar cal = Calendar.getInstance ()

Cal.setTime (date)

Cal.add (Calendar.MINUTE, num)

Return cal.getTime ()

}

/ * *

* obtain the timestamp eg:yyyy-MM-dd HH:mm:ss.S

* /

Public static String getTimeStamp () {

SimpleDateFormat sdf = new SimpleDateFormat (FORMAT_FULL_MILL)

Calendar cal = Calendar.getInstance ()

Return sdf.format (Calendar.getTime ())

}

/ * *

* get date and year

* /

Public static String getYear (Date date) {return format (date) .substring (0P4);}

/ * *

* obtain year + month

* /

Public static String getYearMonth (Date date) {return format (date) .substring (0J7);}

/ * *

* get the number of hours of the date

* /

Public static int getHour (Date date) {

Calendar calendar = Calendar.getInstrance ()

Calendar.setTime (date)

Return calendar.get (Calendar.HOUR_OFDAY)

}

/ * *

* Custom time format string number of days from today

* /

Public static int countDays (String strDate,String format) {

Long time = Calendar.getInstance.getTime () .getTime ()

Calendar cal = Calendar.getInstance ()

Cal.setTime (parse (strDate,format))

Long diff = cal.getTime () .getTime ()

Return (int) (time/1000-diff/1000) / 3600 Universe 24

}

/ * *

* the number of days from the preset format of the string to today

* /

Public static int countDays (String strDate) {return countDays (strDate,getDefaultFormat ());}

/ * *

* get the difference in days (dependent on time)

* @ return

* /

Public static int diffDays (Date date1, Date date2) {

If (date1 = = null | | date2 = = null) return 0

Return (int) (Math.abs (date1.getTime ()-date2.getTime ()) / (60 * 60 * 24 * 1000))

} which is a good http://mobile.zyyyzz.com/ for Zhengzhou abortion Hospital

/ * *

* get the year difference

* /

Public static int diffYear (Date year1,Date year2) {return diffDays (year1,year2) / 365;}

/ * *

* get the day difference (depending on the date of the Date type)

* @ return

* /

Public static int diffByDays (Date D1, Date D2) {

Date S1 = parse (format (D1, FORMAT_LONOGRAM), FORMAT_LONOGRAM)

Date S2 = parse (format (D2, FORMAT_LONOGRAM), FORMAT_LONOGRAM)

Return diffDays (S1, S2)

}

/ * *

* get time division collection

*

* @ param date query date

* @ param strs with split point in time

* @ return

* /

Public static List collectTimes (Date date, String [] strs) {

List result = new ArrayList ()

List times = Arrays.asList (strs)

String dateStr = format (date, FORMAT_LONOGRAM)

String pattern = FORMAT_LONOGRAM + "k"

If (times.size () > 0) {

Times.stream () .forEach (t-> {

Result.add (parse (dateStr + "+ t, pattern))

})

}

Return result

}

/ * *

* query the current day of the week according to the date

* @ param dt

* @ return

* /

Public static String getWeekOfDate (Date dt) {

String [] weekDays = {"1", "2", "3", "4", "5", "6", "7"}

Calendar cal = Calendar.getInstance ()

Cal.setTime (dt)

Int w = cal.get (Calendar.DAY_OF_WEEK)

If (0 = = w) {

W = 7

}

Return weekDays [w]

}

Public static String intToCn (int hourInt, String [] timeArray) {

String result = ""

If (0

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