Java Date Processing

Source: Internet
Author: User
Tags string format

Query criteria Date time period is empty if the default setting is the month to tomorrow (0 o'clock), the query is less than tomorrow

String begindate=request.getparameter ("begindate");
String enddate=request.getparameter ("endDate");

if (! Commonmethod.isnull (begindate)) {
Qm.setbegindate (dateutil. Parsestringtodate(begindate, "yyyy-mm-dd"));
}else{
Qm.setbegindate (Dateutil.parsestringtodate (dateutil. Getformatdatestr(New Date (), "yyyy-mm") + " -01", "yyyy-mm-dd");
}
if (! Commonmethod.isnull (endDate)) {
Qm.setenddate (Dateutil.getnextndaydate (dateutil. Parsestringtodate(endDate, "Yyyy-mm-dd"), 1);
}else{
Qm.setenddate (dateutil. Getnextndaydate(New Date (), 1));
}

Use the function:

/**
  * @ Function Description: Transform string to date
  * @param datestr
  * @return
  * @ Create time 2008-7-13
  * @author Administrator
  */
 public static Date parsestringtodate (String datestr, String formate)
   throws Exception {
  try {
   simpledateformat SDF = Getsimpledateformat (formate);
   return Sdf.parse (DATESTR);
  } catch (Exception ex) {
   return null;
  }
 }

/**
  * Get current date
  * @return
  * @throws Exception
  */
 public static Date GetC Urrentdate () throws Exception {
  date currtime = new Date (System.currenttimemillis ());
  return Currtime;
 }
 /**
  * get date n days after date
  * @param date
  * @param day
  * @return
  *
 public Static Date getnextndaydate (date date, int day) {
  calendar c=calendar.getinstance ();
  c.settime (date);
  c.add (Calendar.date, day);
//  long d = date.gettime ();
//  long n = day * 24 * 60 * 60 * 1000;
//  d = d + N;
  return c.gettime ();
 }

/** * @ Function Description: Gets the string in the specified format * @return * @ creation Time 2008-7-26 * @author Beedoor */public static string Getformatdatestr (O   Bject obj, String format) throws Exception {Date d = null;   SimpleDateFormat SDF = getsimpledateformat (format);   if (obj instanceof String) {d = Sdf.parse (Obj.tostring ()); } else if (obj instanceof date) {d = (date) obj;

} if (null! = d) {return Sdf.format (d); }

Return ""; }

Java Date Processing

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.