Java in the date and time to deal with the example

  Abstract: java in the date and time to deal with the example 

  </ Td> </ tr> <tr> <td height="35" valign="top" class="ArticleTeitle"> <table width="754" border="0"> <tr> <td> </ td> </ tr> </ table> 

  Import java.util .*; 
  Import java.text.SimpleDateFormat; 
  Import java.sql.Date; 
  Import java.sql.Time; 
  Import java.sql.Timestamp; 

  Public class TestDateTime 
  ( 
  Public static void main (String argv []) 
  ( 
  Long nCurrentTime = System.currentTimeMillis (); 
  Java.util.Date utilDate = new java.util.Date (nCurrentTime); 

  / / The following is the database for the operation of the date and time of 
  Java.sql.Date sqlDate = new java.sql.Date (utilDate.getTime ()); 

  / / Java.util.Date conversion and java.sql.Date 
  Java.sql.Time sqlTime = new java.sql.Time (utilDate.getTime ()); 
  Java.sql.Timestamp sqlTimestamp = new java.sql.Timestamp (nCurrentTime); 

  / / Calendar for the acquisition of a specified period of time (Field) 
  Calendar calendar = Calendar.getInstance (); 
  Calendar.setTimeInMillis (nCurrentTime); 

  Int year = calendar.get (Calendar.YEAR); 
  Int month = calendar.get (Calendar.MONTH) + 1; / / NOTE: the return of the month is based on 0! 
  Int day = calendar.get (Calendar.DAY_OF_MONTH); 
  Int hour = calendar.get (Calendar.HOUR_OF_DAY); 
  Int minute = calendar.get (Calendar.MINUTE); 
  Int second = calendar.get (Calendar.SECOND); 

  System.out.println ( "Current date is:" + year + "" + month + "," + + day ","); 
  System.out.println ( "Current time is:" + hour + "" + + minute "-" + second + "seconds"); 

  / / SimpleDateFormat format for the date and time 
  SimpleDateFormat df = new SimpleDateFormat ( "yyyy-MM-dd HH: mm: ss"); 
  String szDatetime1 = df.format (utilDate); 
  System.out.println ( "Current datetime is:" + szDatetime1); 

  / / Value of the following two dates of the time difference between 
  Java.util.Date date2 = new java.util.Date (nCurrentTime - 24600 * 1000); 

  UtilDate.getTime long nMilliSeconds = () - date2.getTime (); 
  Long nSeconds = nMilliSeconds / 1000; / / ms converted to the second 
  Double nDays nSeconds = / (24d * 60d * 60d); / / conversion of the second Narita 

  String szDatetime2 = df.format (date2); 
  System.out.println ( "Time between" szDatetime1 + + "and" + + szDatetime2 "is" nSeconds + + "seconds (" + + nDays "days)"); 
  ) 
  ) 

  Running Results: 


  C: \ java> java TestDateTime 
  Current date is: January 2, 2005 
  Current time is: 15:55:45 
  Current datetime is: 2005-01-02 15:55:45 
  Time between 2005-01-02 15:55:45 and 2005-01-02 09:05:45 is 24,600 seconds (0.2847222222222222 days) 

  C: \ java> </ td> </ tr> <tr> 

  ↑ Back 

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • Facebook
  • DotNetKicks
  • DZone
  • Netvouz
  • Propeller

Tags: , ,

Releated Java Articles

Comments

Leave a Reply