Java DateUtil current day, this week, this month time acquisition method
import org.springframework.util.StringUtils;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
/**
* Time operation Tool class
*
* @author zys
*
*/
public class Time {
/**
* Get today's time range
* @return returns a collection of strings of length 2, such as: [2017-11-03 00:00:00, 2017 -11-03 24:00:00]
*/
public static List<String> getTodayRange() {
List<String> dataList = new ArrayList<>(2);
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd") ;
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.add(Calendar.DATE, 0);
String today = dateFormat.format(calendar.getTime());
dataList.add(today + " 00:00:00");
dataList.add(today + " 24:00:00");
return dataList;
}
/**
* Get yesterday's time range
* @return returns a collection of strings of length 2, Such as: [2017-11-02 00:00:00, 2017-11-02 24:00:00]
*/
public static List<String> getYesterdayRange() {
List<String> dataList = new ArrayList<>(2) ;
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.add(Calendar.DATE, -1);
String yesterday = dateFormat.format(calendar.getTime());
dataList.add( yesterday + " 00:00:00");
dataList.add(yesterday + " 24:00:00");
return dataList;
}
/**
* Get the time range of this week
* @return returns a string of length 2 Collection, such as: [2017-10-30 00:00:00, 2017-11-05 24:00:00]
*/
public static List<String> getCurrentWeekRange() {
List<String> dataList = new ArrayList<>( 2);
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
Calendar calendar = Calendar.getInstance();
calendar.setFirstDayOfWeek(Calendar.MONDAY);//Set Monday as the first day of the week
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.DAY_OF_WEEK, Calendar .MONDAY);
String monday = dateFormat.format(calendar.getTime()) + " 00:00:00";
calendar.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
String sunday = dateFormat.format(calendar.getTime( )) + " 24:00:00";
dataList.add(monday);
dataList.add(sunday);
return dataList;
}
/**
* Get the time range of this week (without hours, minutes and seconds)
* @return returns the length A collection of strings of 2, such as: [2017-10-30, 2017-11-05]
*/
public static List<String> getCurrentWeekRangeNoTime() {
List<String> dataList = new ArrayList<>(2);
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
Calendar calendar = Calendar.getInstance();
calendar.setFirstDayOfWeek(Calendar.MONDAY);//设置周一为一周之内的第一天
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
String monday = dateFormat.format(calendar.getTime());
calendar.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
String sunday = dateFormat.format(calendar.getTime());
dataList.add(monday);
dataList.add(sunday);
return dataList;
}
/**
* Get the time range of this month
* @return returns a string collection of length 2, such as: [2017-11-01 00:00:00, 2017-11-30 24:00:00 ]
*/
public static List<String> getCurrentMonthRange() {
List<String> dataList = new ArrayList<>(2);
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
Calendar calendar = Calendar.getInstance() ;
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.add(Calendar.MONTH, 0);
calendar.set(Calendar.DAY_OF_MONTH, 1);
String firstDayOfMonth = dateFormat.format(calendar.getTime()) + " 00 :00:00";
calendar.add(Calendar. MONTH, 1);
calendar.set(Calendar.DAY_OF_MONTH, 0);
String lastDayOfMonth = dateFormat.format(calendar.getTime()) + " 24:00:00";
dataList.add(firstDayOfMonth);
dataList.add(lastDayOfMonth);
return dataList;
}
/**
* Get the time range of the current year
* @return returns a string set of length 2, such as: [2017-01-01 00:00:00, 2017-12-31 24:00:00]
*/
public static List<String> getCurrentYearRange() {
List<String> dataList = new ArrayList<>(2);
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
Calendar calendar = Calendar.getInstance();
calendar. setTimeInMillis(System.currentTimeMillis());
calendar.add(Calendar.YEAR, 0);
calendar.set(Calendar.DAY_OF_YEAR, 1);
String firstDayOfYear = dateFormat.format(calendar.getTime()) + " 00:00:00";
calendar.add(Calendar. YEAR, 1);
calendar.set(Calendar.DAY_OF_YEAR, 0);
String lastDayOfYear = dateFormat.format(calendar.getTime()) + " 24:00:00";
dataList.add(firstDayOfYear);
dataList.add(lastDayOfYear );
return dataList;
}
/**
* Get the time range of the last few days
* @param lastFewDays How many days are the last
* @return Returns a string set of length 2, such as: [2017-12-25 17:15:33, 2017-12-26 17:15:33]
*/
public static List<String> getLastFewDaysRange(int lastFewDays) {
List<String> dataList = new ArrayList<>(2);
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
String endTime = dateFormat.format(calendar.getTime());
calendar.add(Calendar.DATE, -lastFewDays);
String startTime = dateFormat.format(calendar.getTime());
dataList.add(startTime);
dataList.add(endTime);
return dataList;
}
/**
* 获取当前时间
* @param pattern specifies the format for returning the current time, for example: "yyyy-MM-dd HH:mm:ss"
* @return returns the current time in the specified format, for example: "2018-01-25 10:14:30"
* /
public static String getCurrentTime(String pattern) {
String currentTime;
SimpleDateFormat dateFormat = new SimpleDateFormat(pattern);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
currentTime = dateFormat.format(calendar. getTime());
return currentTime;
}
/***
* Offset the specified time by a few hours
* @param time specifies the time, accurate to minutes, for example: "2018-01-25 10:48"
* @param offset offset Amount: Negative number means minus several hours, positive number means add several hours, for example: 1
* @return returns the time after the offset, such as: "2018-01-25 11:48"
* @throws ParseException
*/
public static String offsetHours(String time, int offset) throws ParseException {
String offsetHours = null;
if (StringUtils .hasText(time)) {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
Calendar calendar = Calendar.getInstance();
calendar.setTime(dateFormat.parse(time));
calendar.add( Calendar.MINUTE, offset);
offsetHours = dateFormat.format(calendar.getTime());
}
return offsetHours;
}
/**
* Offset the specified month by several months
* @param month specifies the month
* @param offset offset: negative number represents the previous month, positive number represents the next month
* @return returns the month after the offset, such as: 2018-01
* @throws ParseException
*/
public static String offsetMonths(String month, int offset) throws ParseException {
String offsetMonth = null;
if (StringUtils.hasText(month)) {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM");
Calendar calendar = Calendar.getInstance();
calendar.setTime(dateFormat.parse(month));
calendar.add(Calendar.MONTH, offset);
offsetMonth = dateFormat.format(calendar.getTime());
}
return offsetMonth;
}
/**
* Get the day of the week for the specified date (set Monday as the start of the week)
* @param day specifies the date
* @return returns the day of the week, such as: 1
* @throws ParseException
*/
public static int getDayOfWeek(String day) throws ParseException {
int dayOfWeek = 0;
if (StringUtils.hasText(day)) {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
Calendar calendar = Calendar.getInstance();
calendar.setTime(dateFormat.parse( day));
calendar.add(Calendar.DATE, -1); //Subtract one day from the specified date to meet Chinese habits
dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
}
return dayOfWeek;
}
/**
* 获取指定月份有多少天
* @param month 指定月份
* @return 返回天数,如:31
* @throws ParseException
*/
public static int getDaysInMonth(String month) throws ParseException {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM");
Calendar calendar = Calendar.getInstance();
calendar.setTime(dateFormat.parse(month));
calendar.add(Calendar.MONTH, 1);
calendar.set(Calendar.DAY_OF_MONTH, 0);
return calendar.get(Calendar.DAY_OF_MONTH);
}
}
Guess you like
Origin http://43.154.161.224:23101/article/api/json?id=326035120&siteId=291194637
Ranking