1. The Java (service method) side gets the first 12 hours of the current time:
Public Object Getsearchname (HttpServletRequest request) {
Jsonarray arr = new Jsonarray ();
Jsonobject obj = new Jsonobject ();
Jsonobject Objfirst = new Jsonobject ();
Objfirst.put ("ItemName", "--Please Select--");
Objfirst.put ("Itemvalue", "" ");
Arr.add (Objfirst);
Gets the 24-hour number of the current time
for (int i=0;i<24;i++) {
Calendar calendar = Calendar.getinstance ();
Calendar.set (Calendar.hour_of_day, Calendar.get (calendar.hour_of_day)-i);
SimpleDateFormat df = new SimpleDateFormat ("Yyyy-mm-dd hh:mm");
System.out.println (i+ "hours ago:" +df.format (Calendar.gettime ()));
System.out.println ("Current Time:" +df.format (New Date ()));
Obj.put ("ItemName", Df.format (Calendar.gettime ()));
Obj.put ("Itemvalue", Df.format (Calendar.gettime ()));
Arr.add (obj);
}
return arr.tostring ();
}
2, the Java method is written in a static class, can be called directly through the method name: String Searchbefore = Dateutil.getdate (Searchtime,-1);
/**
*
* @Title:
* @Description: Get an advance or deferred date in the format Yyyy-mm-dd hh:mm
* @return String
* @Version: 1.1.0
*/
public static string GetDate (string datestr, int hour) {
try {
SimpleDateFormat format = new SimpleDateFormat (CURRENT_YEAR_PATTERN3);
Java.util.Date Date = Format.parse (DATESTR);
Calendar cal = Calendar.getinstance ();
Cal.settime (date);
Cal.add (Calendar.hour_of_day, HOUR);
Return Format.format (Cal.gettime ());
} catch (Exception e) {
Return "";
}
}
Java side gets the first 12 hours of the current time