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 commonly used Java Date methods

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

In this issue, the editor will bring you what are the commonly used Java Date methods. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.

1.Java Date calculates the number of days between two arbitrary times (this is more common):

(1) input Calendar object:

Public int getIntervalDays (Calendar startday,Calendar endday)... {if (startday.after (endday))... {Calendar cal=startday; startday=endday; endday=cal;} long sl=startday.getTimeInMillis (); long el=endday.getTimeInMillis (); long ei=el-sl; return (int) (ei/ (1000 / 60 / 60 / 24));}

(2) input Date object:

Public int getIntervalDays (Date startday,Date endday)... {if (startday.after (endday))... {Date cal=startday; startday=endday; endday=cal;} long sl=startday.getTime (); long el=endday.getTime (); long ei=el-sl; return (int) (ei/ (1000 / 60 / 60 / 24));}

(3) improve the method of accurately calculating the number of days apart:

Public int getDaysBetween (Calendar D1, Calendar D2)... {if (d1.after (D2))... {java.util.Calendar swap = D1; D1 = D2; D2 = swap;} int days = d2.get (Calendar.DAY_OF_YEAR)-d1.get (Calendar.DAY_OF_YEAR); int y2 = d2.get (Calendar.YEAR) If (d1.get (Calendar.YEAR)! = y2) {D1 = (Calendar) d1.clone (); do... {days = d1.getActualMaximum (Calendar.DAY_OF_YEAR); / / get the actual number of days of the year d1.add (Calendar.YEAR, 1);} while (d1.get (Calendar.YEAR)! = y2);} return days }

Note: any time can be derived from the above method. For example, to find out the email received within three weeks (get the current system time-then get the time three weeks ago), use the pickup time to match * * disguised as long to compare.

For example, the date 1 year ago (pay attention to the conversion in milliseconds):

Java.util.Date myDate=new java.util.Date (); long myTime= (myDate.getTime () / 1000)-60,60,24,365; myDate.setTime (myTime*1000); String mDate=formatter.format (myDate)

2. Conversion between String, Date and Long of Java Date (most commonly used):

The string is converted to a time type (the string can be of any type, as long as it is consistent with the format in SimpleDateFormat)

Usually when we take a time span, we will substring a specific time-long- comparison.

Java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat ("M/dd/yyyy hh:mm:ss a", java.util.Locale.US); java.util.Date d = sdf.parse ("5new SimpleDateFormat 10:31:37 AM"); long dvalue=d.getTime (); SimpleDateFormat formatter = new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss"); String mDateTime1=formatter.format (d)

3. Calculate the time through the time of Java Date:

Year, month, week, date:

SimpleDateFormat formatter2 = new SimpleDateFormat ("yyyy-MM F E"); java.util.Date date2= formatter2.parse ("2003-05 Friday"); SimpleDateFormat formatter3 = new SimpleDateFormat ("yyyy-MM-dd"); String mydate2=formatter3.format (date2)

Ask for the day of the week:

Mydate= myFormatter.parse ("2001-1-1"); SimpleDateFormat formatter4 = new SimpleDateFormat ("E"); String mydate3=formatter4.format (mydate)

4. Combine the java of Java Date with specific database:

In the development of web applications, we need to convert different date types in our program according to different database date types. If the corresponding database data is the Date type of oracle, that is, you only need the year, month and day, you can choose to use the java.sql.Date type. If the corresponding is the DateTime type of the MSsqlserver database, that is, you need the year, month, day, hour, minute and second, select the java.sql.Timestamp type, you can use dateFormat to define the format of the time and date, and convert it to a string.

Class Datetest {* method converts a date of string type to a timestamp (timestamp java.sql.Timestamp) * @ param dateString string that needs to be converted to timestamp * @ return dataTime timestamp public final static java.sql.Timestamp string2Time (String dateString) throws java.text.ParseException {DateFormat dateFormat; dateFormat = new SimpleDateFormat ("yyyy-MM-dd kk:mm:ss.SSS", Locale.ENGLISH) / / set format / / dateFormat = new SimpleDateFormat ("yyyy-MM-dd kk:mm:ss", Locale.ENGLISH); dateFormat.setLenient (false); java.util.Date timeDate = dateFormat.parse (dateString); / / util type java.sql.Timestamp dateTime = new java.sql.Timestamp (timeDate.getTime ()); / / Timestamp type, timeDate.getTime () returns a long return dateTime } * method converts a date of string type into a Date (java.sql.Date) * @ param dateString string that needs to be converted to Date * @ return dataTime Date public final static java.sql.Date string2Date (String dateString) throws java.lang.Exception {DateFormat dateFormat; dateFormat = new SimpleDateFormat ("yyyy-MM-dd", Locale.ENGLISH); dateFormat.setLenient (false); java.util.Date timeDate = dateFormat.parse (dateString) / / util type java.sql.Date dateTime = new java.sql.Date (timeDate.getTime ()); / / sql type return dateTime;} public static void main (String [] args) {Date da = new Date ()

Note: what da.getTime () gets here is a long value.

System.out.println (da.getTime ())

The conversion of 5.Java Date from date date to timestamp:

* methods:

Use new Timestamp (long)

Timestamp t = new Timestamp (new Date () .getTime ()); System.out.println (t)

The second method: use Timestamp (int year,int month,int date,int hour,int minute,int second,int nano)

Timestamp tt = new Timestamp (Calendar.getInstance (). Get (Calendar.YEAR)-1900, Calendar.getInstance (). Get (Calendar.MONTH), Calendar.getInstance (). Get (Calendar.DATE), Calendar.getInstance (). Get (Calendar.HOUR), Calendar.getInstance (). Get (Calendar.MINUTE), Calendar.getInstance (). Get (Calendar.SECOND), 0); System.out.println (tt) Try {String sToDate = "2005-8-18"; / / the string used to convert to java.sql.Date String sToTimestamp = "2005-8-18 14 java.sql.Date 21 System.out.println 12.123"; / / the string used to convert to java.sql.Timestamp Date date1 = string2Date (sToDate); Timestamp date2 = string2Time (sToTimestamp); System.out.println ("Date:" date1.toString ()) / / the result shows that System.out.println ("Timestamp:" date2.toString ()); / / the result shows} catch (Exception e) {e.printStackTrace ();} these are the common Java Date methods shared by the editor. If you happen to have similar doubts, please refer to the above analysis for understanding. If you want to know more about it, you are welcome to follow the industry information channel.

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