JAVA to seek yesterday, tomorrow's algorithm (two days ago, after three days to seek the law)

Source: Internet
Author: User
Tags benchmark set time
JAVA to seek yesterday, tomorrow's algorithm (two days ago, after three days to seek the law)
The Java time class is supported by pointing to a day earlier or later, in this way, we bring great convenience to the development of the program. For example, we ask yesterday or tomorrow is what day is not hard to calculate the number of days in the end, not to think of the month or the beginning of how to calculate. Everything is handled by the JDK. We ask for a time before or after a certain time (this function is more widely used, for example, than I want to get the first 5 days of data for the table).
Next, let's introduce the Java time class
Java.lang.Object
Java.util.Calendar
Java.util.GregorianCalendar
Java.lang.Object
Java.util.Date

Class Date represents a specific moment, accurate to milliseconds.
Before JDK 1.1, class Date had two other functions. It allows you to interpret dates as year, month, day, hour, minute, and second values. It also allows formatting and parsing of date strings. However, the APIs for these functions are not easy to internationalize. Starting with JDK 1.1, you should use the Calendar class to implement conversion between date and Time fields, and use the DateFormat class to format and parse date strings. The corresponding method in Date is deprecated.

The Calendar class is an abstract class that provides methods for a specific moment to transform between a set of calendar fields such as year, MONTH, Day_of_month, HOUR, and for manipulating calendar fields, such as getting the date of next week. Instantly available in milliseconds, it is an offset from the 00:00:00.000 (i.e. Greenwich Mean time, January 1, 1970, Gregorian calendar).

GregorianCalendar is a specific subclass of calendar that provides the standard calendar system used in most countries/regions of the world.
GregorianCalendar is a hybrid calendar that supports both the Julian calendar and the Gregorian calendar system with the support of a single discontinuity, and by default it corresponds to the Gregorian calendar when it was founded (some countries/regions were created on October 15, 1582, in other countries/ Area to be late).

The date class was almost obsolete in the later JDK. Therefore, the date can also be used to point to a day or a late day corresponding method.

Now let's look at the code for the day before the implementation:
Calendar yestoday = Calendar.getinstance ();
Cal1.set (2000,1,29);
Yestoday. Add (Calendar.date,-1); The Add (int field, int amount) method of the calendar adds the specified (signed) amount of time to the given Calendar field, based on the calendars rules. Where field can be all calendar fields for calendar. (Note Cal1.set (2000,1,29) Set time month is starting from 0)
The yestoday of the previous generation have pointed to the present time of yesterday.

The time period is usually done in the java+ database
Like the first 3 days of data (including today)
Calendar Cal1 = Calendar.getinstance ();
Cal1.add (Calendar.date,-2);
Cal1.set (2000,1,29);
SimpleDateFormat SDF = new SimpleDateFormat ("Yyyy-mm-dd");
String Towdaysbefore = Sdf.format (Cal1.gettime ());

String today = Sdf.format (Calendar.getinstance (). GetTime ());

String sql = "SELECT * from" + tablename + "Where time between '" + Towdaysbefore
+ "and" + Today; The above SQL is the first 3 days of data (including today) SQL
In general, the data for a time period is based on the day, no need to be a second to the benchmark, so to SimpleDateFormat. Of course, what is the benchmark depends on the specific needs.

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.