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

How to use DateTime in java

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces "how to use DateTime in java". In daily operation, I believe many people have doubts about how to use DateTime in java. 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 DateTime in java". Next, please follow the editor to study!

Introduction of org.joda.time.DateTime

Joda-time joda-time 2.9.81. Initialization time DateTime time = new DateTime (2018, 4), 23, 23, 7, 1888, 2018, April 23, 2018, 23:07:18 888ms 2. Output time in format (convert DateTime format to string) String time = dateTime.toString ("yyyy-MM-dd hh:mm:ss.SSSa")

Lowercase hh is 12-hour, uppercase HH is 24-hour.

3. Convert the string to the DateTime format DateTimeFormatter format = DateTimeFormat. ForPattern ("yyyy-MM-dd HH:mm:ss"); DateTime dateTime = DateTime.parse ("2018-4-23 23:12:16", format); 4. Get the current time DateTime time= new DateTime (); 5. Calculate the number of days between two dates: LocalDate start=new LocalDate (2018 LocalDate start=new LocalDate); LocalDate end=new LocalDate (2019, 06, 16); int days = Days.daysBetween (start, end). GetDays (); 6. Add date DateTime dateTime = DateTime.parse ("2018-04-23"); dateTime = dateTime1.plusDays (1); dateTime = dateTime1.plusHours (2); dateTime = dateTime1.plusMinutes (3); dateTime = dateTime1.plusMonths (4); dateTime = dateTime1.plusSeconds (5); dateTime = dateTime1.plusWeeks (6) DateTime = dateTime1.plusYears (7); 7. Decrease date DateTime dateTime = DateTime.parse ("2018-04-23"); dateTime = dateTime1.minusMillis (1); dateTime = dateTime1.minusHours (1); dateTime = dateTime1.minusSeconds (1); 8. Judge whether a leap month is DateTime time = new DateTime (); org.joda.time.DateTime.Property month = time.monthOfYear (); System.out.println ("Leap month:" + month.isLeap ()); conversion between 9.DateTime and Date DateTime time = new DateTime (new Date ()); Date date = time.toDate (); DateTime time2 = new DateTime (System.currentTimeMillis ()); time2.getMillis (); 10.DateTime and Calendar conversion Calendar calendar = Calendar.getInstance ()

Add:

String and java.utils.Date convert to each other

Convert a string to a Date type

DateFormat format = new SimpleDateFormat ("yyyy-MM-dd kk:mm:ss"); / / the time format to be converted is String str = "2016-12-11 17:17:10"; / / the time string java.utils.Date date = null;try {date = format.parse (str);} catch (Exception e) {e.printStackTrace ();}

Convert Date type to string

DateFormat format = new SimpleDateFormat ("yyyy-MM-dd kk:mm:ss"); / / fill in here is the time format Date date = new java.utils.Date () that you want to convert; / / get the current time String str = format.format (date); / / str = "2016-12-11 17:17:10" java.sql.Date write date to the database

Here you need to convert java.sql.Date and java.utils.Date to each other, which is generally easy to convert using the constructor and the .getTime () method.

Java.utils.Date date = new java.utils.Date (); / / get the current time java.sql.Date sql_date = new java.sql.Date (date.getTime ()); / / convert to java.sql.Date

Then write the time to the database using PreparedStatement.

Ps.setDate (1, new java.sql.Date (date.getTime (); / / set the java.sql.Timestamp write time to the database

It is important to note that the above method can only write the date to the database, in order to be able to accurate the time to s or even ms. We need to use the java.sql.Timestamp type.

Constructors of type java.sql.Timestamp can also be initialized with Long

Date date = new Date (); Timestamp timeStamp = new Timestamp (date.getTime ())

Therefore, when generating java.utils.Date, the time is accurate to s, and then the precompiled statement is used to execute the SQL statement

Date date = new Date (); Timestamp timeStamp = new Timestamp (date.getTime ()); sql= "insert into flowmeter2 (total,std_flow,temp,press,time) values (?)"; try {PreparedStatement ps = connection.prepareStatement (sql); ps.setString (1, total); ps.setString (2, std_flow); ps.setString (3, temp) Ps.setString (4, press); ps.setTimestamp (5, timeStamp); ps.executeUpdate (); System.out.println ("added successfully!") ; connection.close (); at this point, the study on "how to use DateTime in java" is over. I hope I 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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report