Java side gets the first 12 hours of the current time

Source: Internet
Author: User

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

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.