1. Conversion between date and calendar:
Java code
- Date = new date ();
- Calendar calendar = calendar. getinstance ();
- Calendar. settime (date );
- Calendar. Add (calendar. Date,-1); // obtain the previous day
- Calendar. Add (calendar. Month,-1); // obtain the previous month.
In this way, the calendar instance can be used to obtain the date and time.
2. Conversion between date and string:
Java code
- String datestr = "2008-8-13 ";
- String Pattern = "yyyy-mm-dd ";
- Dateformate dateformat = new simpledateformat (pattern );
- Date = dateformat. parse (datestr );
- Date = dateformat. Format (date );
In this way, the instance date of a date can be converted to the calendar ar type, and then the date operation is performed.
3. Convert calendar to date
Calendar CA = calendar. getinstance ();
Date Ss = (date) Ca. gettime ();
4. the query date range cannot exceed one month
Import java. util. date;
Import java. util. calendar;
Import java. Text. simpledateformat;
Import java. Text. dateformat;
String start = '2017-01-02'
String end = '2017-02-12'
Dateformat = new simpledateformat ("yyyy-mm-dd ");
Try {
Date startdate = dateformat. parse (start );
Date enddate = datefformat. parse (end );
Calendar Cal = calendar. getinstance ();
Cal. settime (startdate );
Cal. Add (calendar. Month, + 1 );
Startdate = (date) Cal. gettime ();
If (startdate. gettime () <enddate. gettime ()){
Return "the query date range cannot exceed one month ";
} Catch (parseexception e ){
E. printstacktrace ();
}