The biggest feature of Hive is to analyze big data through SQL-like, and avoid writing MapReduce programs to analyze data, which makes it easier to analyze data.
Calculate the new time of day:
Take out the time of the day to format and get the zero point, for example, 2019/3/12 22:21 becomes 2019/3/12 00:00 is the start time of the day.
After getting the time of the day, use the Calendar to add one to the basis of the day, which is today’s end time 2019/3/13 00:00, as long as these two
Between the time periods is what was added today.
Code:
/**
*Calculate the start time of a day (milliseconds)
*/
@Test
public void testStartTime() throws ParseException {
Date d = new Date();
long ms = getZeroDate(d).getTime();
System.out.println(ms);
}
/**
* Get the zero hour of the specified date.
*/
private Date getZeroDate(Date d){
try {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd 00:00:00");
return sdf.parse(sdf.format(d));
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
*Calculate the end time of a day (milliseconds)
*/
@Test
public void testEndTime() throws ParseException {
Date d = new Date();
Date zeroDate = getZeroDate(d);
//Calendar
Calendar c = Calendar.getInstance();
c.setTime(zeroDate);
c.add(Calendar.DAY_OF_MONTH,1); //Add 1 on the basis of the day
Date endDate = c.getTime();
System.out.println(endDate.getTime());
}
Calculate the new time of week:
Note that the rule for adding and subtracting the calendar at the end of the week is, if today is the third day of the week, then you use Calendar.DAY_OF_MONTH,-(n-1)
(- represents a few days forward, + represents a few days backward), 3-1 days forward is the starting day of the week, Sunday. If the end of the week is (8-n) n is 3
Use getZeroDate() This method must call this.
Code:
/**
* Test week start time
*/
@Test
public void testWeekStartTime(){
Date d = new Date();
Date zeorDate = getZeroDate(d);
Calendar c = Calendar.getInstance();
c.setTime(zeorDate);
//Sunday is the first day of the week
int n = c.get(Calendar.DAY_OF_WEEK); //Get it is the day of the week
//add is for digital processing
c.add(Calendar.DAY_OF_MONTH,-(n-1)); //-represents a few days forward, + represents a few days backward
// The way to get the millisecond value
// Date time = c.getTime();
// long l = time.getTime();
long ms = c.getTimeInMillis();
System.out.println(ms );
}
/**
* End of test week
*/
@Test
public void testWeekEndTime(){
Date d = new Date();
Calendar c = Calendar.getInstance();
c.setTime(d);
int n = c.get(Calendar.DAY_OF_WEEK);
c.add(Calendar.DAY_OF_MONTH,(8-n));
//
Date weekFirstDate = c.getTime();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
System.out.println(sdf.format(weekFirstDate));
}
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.