Java-implemented date + Integer Conversion to date class

Source: Internet
Author: User

<PRE class = "Java" name = "code"> Import java. Text. parseexception;
Import java. Text. simpledateformat;
Import java. util. calendar;

/**
* This class mainly verifies and filters characters.
* @ Author
*
*/
Public class stringutil {

// Convert the Date Format
Public static java. SQL. Date parsedate (string s) throws exception {
String [] STR = S. Split ("-");
If (Str. length! = 3 ){
Throw new exception ("the date format is incorrect! ");
}

Int year = integer. parseint (STR [0]);
Int month = integer. parseint (STR [1]);
Int day = integer. parseint (STR [2]);

If (Year> 9999 | year <0 ){
Throw new exception ("the date format is incorrect! ");
}
If (month <1 | month> 12 ){
Throw new exception ("the date format is incorrect! ");
}
If (day <1 | day> 31 ){
Throw new exception ("the date format is incorrect! ");
}

Return new java. SQL. date (year-1900, month-1, Day );
}
/**
* Parses a string into a date object.
* @ Param STR date string
* @ Return date object
* @ Throws parseexception
*/
Public static java. util. Date strtodate (string Str) throws parseexception {
// Specify the date format
Simpledateformat format = new simpledateformat ("yyyy-mm-dd hh: mm ");
Java. util. Date = NULL;
// Parse the string into a date object
Date = format. parse (STR );

Return date;
}
/**
* Returns a date object based on the month of Mar.
* @ Param date object
* @ Return string
*/
Public static string datetostr (Java. util. Date ){
// Specify the date format
Simpledateformat format = new simpledateformat ("yyyy-mm-dd hh: mm: SS ");
// Parse the date object to a string in the specified format
String STR = format. Format (date );
Return STR;
}
/**
* Date + integer = Date (minutes)
* @ Param DT date
* @ Param count integer
* @ Return date
*/
Public static java. SQL. Date minute_shift (Java. util. Date DT, int count)
{
Calendar Cal = calendar. getinstance ();
// Use the specified date to set the time of the calendar.
Cal. settime (DT );
// Add or subtract the specified time amount for the specified calendar field according to the calendar rule
// Calendar. Minute, used to set the sum of integers and dates. The value of calendar. minute is a clock.
Cal. Add (calendar. Minute, count );
Return new java. SQL. Date (Cal. gettimeinmillis ());
// Java. SQL. Date (Cal. gettimeinmillis (). tostring () // format the date in the format of yyyy-mm-dd by default
}

/* This shows the effect
Public static Java. util. date minute_shift (Java. util. date DT, int count)
{< br> calendar Cal = calendar. getinstance ();
// use the given date to set the time of this calendar
Cal. settime (DT);
// Based on calendar rules, add or subtract the specified time amount for the specified calendar field
// calendar. minute is used to set the sum of integers and dates in the calendar. minute is the clock
Cal. add (calendar. minute, count);
return New Java. util. date (Cal. gettimeinmillis ();
}< br> */

}< br>
& nbsp;

Related Article

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.