Complete setup notes-processing of dates

Source: Internet
Author: User
Tags timestamp to date

During the completion process, the date format needs to be processed multiple times. Today, the start and end times of each week are generated based on the number of weeks, the start date of the week, and the number of weeks, to determine the current stage of the system, so we plan to spend some time writing a simple date operation class. The data types include string, date, and timestamp (the timestamp type generated by hibernate for the date field of MSSQL by default, and the date generated by MySQL) are converted to each other to obtain the current date, calculate the date after a certain number of days of the current date difference (it is easy to use gregoriancalendar ).CodeAs follows:

 Package  Com. sxpt. util;  Import  Java. SQL. timestamp;  Import  Java. Text. parseexception;  Import  Java. Text. simpledateformat;  Import  Java. util. calendar;  Import Java. util. date;  Import  Java. util. gregoriancalendar;  Public   Class  Dateoperater {  /**  * Convert character to date *  @ Param  Date *  @ Param  Format "yyyy-mm-dd" or "yyyy-mm-dd hh: mm: SS "*  @ Return       */      Static  Public  Date strtodate (string date, string format) {simpledateformat SDF = New  Simpledateformat (format); date de = Null  ;  Try  {De = SDF. parse (date );}  Catch  (Parseexception e ){  //  Todo auto-generated Catch Block E. printstacktrace ();}  Return  De ;}  /**  * Convert date to character *  @ Param  De *  @ Param  Format "yyyy-mm-dd" or "yyyy-mm-dd hh: mm: SS "*  @ Return       */      Static   Public  String datetostring (date de, string format) {string Str =Null  ; Simpledateformat SDF = New  Simpledateformat (format); Str = SDF. Format (de );  Return  STR ;}  /**  * Convert date to timestamp *  @ Param  De *  @ Return       */      Static   Public Timestamp datetotime (date de) {string Str = Datetostring (De, "yyyy-mm-dd hh: mm: SS" ); Timestamp TS = Timestamp. valueof (STR );  Return  TS ;}  /**  * Convert timestamp to date *  @ Param  TS *  @ Return       */      Static   Public Date timetodate (timestamp TS) {date de = New  Date (); De = TS;  Return  De ;}  /**  * Convert timestamp to string *  @ Param  TS *  @ Param  Format "yyyy-mm-dd" or "yyyy-mm-dd hh: mm: SS "*  @ Return       */     Static   Public  String timetostr (timestamp ts, string format) {simpledateformat SDF = New  Simpledateformat (format); string Str = SDF. Format (TS );  Return  STR ;}  /**  * Calculate the date of a few days after the current date difference *  @ Param  Currentdate current date *  @ Param Num Days *  @ Return       */      Static   Public Date getfurdate (date currentdate, Int  Num) {gregoriancalendar Cal = New  Gregoriancalendar (); Cal. settime (currentdate); Cal. Add (gregoriancalendar. Date, num );  //  Add 7 days to the date          Return  Cal. gettime ();} /**  * Get the current date *  @ Return       */      Static   Public  Date currentdate () {calendar ca = Calendar. getinstance ();  Int Year = Ca. Get (calendar. Year)-1900; //  Get year            Int Month = Ca. Get (calendar. month ); //  Get month System. Out. println (month );  Int Day = Ca. Get (calendar. date ); //  Get day            Int Minute = Ca. Get (calendar. Minute ); //  Minute            Int Hour = Ca. Get (calendar. hour ); //  Hours            Int Second = Ca. Get (calendar. Second ); //  Seconds Date de =New  Date (year, month, day, hour, minute, second );  Return  De ;}} 

The following is the number of weeks based on this implementation, the start and end time of each week after the week is generated:

 

// Beginweek: actual week, begindate: start date of the week, weeknum: calculate the number of weeks
 Public   Void Getweekdate ( Int Beginweek, string begindate, Int  Weeknum ){  //  Start time of calculation Date de = dateoperater. strtodate (begindate + "0: 0", "yyyy-mm-dd hh: mm: SS" );  //  Calculate the start time and end time of each week to generate records.          For ( Int I = beginweek; I <beginweek + weeknum; I ++ ){  Int Tmpnum = (I-beginweek) * 7 ; System. Out. println (dateoperater. datetotime (dateoperater. getfurdate (De, tmpnum); system. Out. println (dateoperater. datetotime (dateoperater. getfurdate (De, tmpnum + 7 )));}} 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.