获取时间工具

2019-10-16 08:04:38来源:博客园 阅读 ()

新老客户大回馈,云服务器低至5折

获取时间工具

/**
* 获取相差天数
* @param beginTime
* @param endTime
* @return
*/
public static int getDaysBetween(String beginTime,String endTime) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
int day = 0;
try {
Date date1 = sdf.parse(beginTime);
Date date2 = sdf.parse(endTime);
Long time = (date2.getTime() - date1.getTime())/(1000*3600*24);
day = Integer.parseInt(time.toString());
} catch (ParseException e) {
e.printStackTrace();
}
return day;
}

/**
* 获取每个15分钟的时间
* @param begin
* @param end
* @return
*/
public static List<String> getBetweenMinute(String beginTime,String endTime){
beginTime = beginTime + " 00:00";
endTime = endTime + " 23:45";
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
List<String> betweenList = Lists.newArrayList();
try{
Calendar startMonth = Calendar.getInstance();
startMonth.setTime(format.parse(beginTime));
while(true){
Date newDate = startMonth.getTime();
String newend=format.format(newDate);
betweenList.add(newend);
if(endTime.equals(newend)){
break;
}
startMonth.add(Calendar.MINUTE, +15);
}
}catch (Exception e) {
e.printStackTrace();
}
return betweenList;
}
/**
* 获取每个小时
* @param begin
* @param end
* @return
*/
public static List<String> getBetweenHour(String beginTime,String endTime){
beginTime = beginTime + " 00";
endTime = endTime + " 23";
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH");
List<String> betweenList = Lists.newArrayList();
try{
Calendar endMonth = Calendar.getInstance();
endMonth.setTime(format.parse(endTime));
String newEnd=format.format(endMonth.getTime());

Calendar startMonth = Calendar.getInstance();
startMonth.setTime(format.parse(beginTime));
startMonth.add(Calendar.HOUR, -1);
while(true){
startMonth.add(Calendar.HOUR, +1);
Date newDate = startMonth.getTime();
String newend=format.format(newDate);
betweenList.add(newend);
if(newEnd.equals(newend)){
break;
}
}
}catch (Exception e) {
e.printStackTrace();
}
return betweenList;
}

/**
* 获取每天
* @param begin
* @param end
* @return
*/
public static List<String> getBetweenDay(String beginTime,String endTime){
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
List<String> betweenList = new ArrayList<String>();
try{
Calendar startDay = Calendar.getInstance();
startDay.setTime(format.parse(beginTime));
while(true){
Date newDate = startDay.getTime();
String newend=format.format(newDate);
betweenList.add(newend);
if(endTime.equals(newend)){
break;
}
startDay.add(Calendar.DATE, 1);
}
}catch (Exception e) {
e.printStackTrace();
}

return betweenList;
}

/**
* 获取当前时间的前三天
* @param beginTime
* @return
*/
public static String getAddThreeDay(String beginTime) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar endDay = Calendar.getInstance();
try {
endDay.setTime(sdf.parse(beginTime));
} catch (ParseException e) {
e.printStackTrace();
}
endDay.add(Calendar.DATE, +3);
return sdf.format(endDay.getTime());
}

/**
* 获取月度
* @param begin
* @param end
* @return
*/
public static List<String> getBetweenMonth(String beginTime,String endTime){
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM");
List<String> betweenList = Lists.newArrayList();
try{
Calendar endMonth = Calendar.getInstance();
endMonth.setTime(format.parse(endTime));
String newEnd=format.format(endMonth.getTime());

Calendar startMonth = Calendar.getInstance();
startMonth.setTime(format.parse(beginTime));
while(true){
Date newDate = startMonth.getTime();
String newend=format.format(newDate);
betweenList.add(newend);
if(newEnd.equals(newend)){
break;
}
startMonth.add(Calendar.MONTH, 1);
}
}catch (Exception e) {
e.printStackTrace();
}
return betweenList;
}
/**
* 获取季度
* @param beginDate
* @param endDate
* @return
*/
public static List<String> getYearSecond(String beginTime,String endTime){
List<String> rangeSet = Lists.newArrayList();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
Date begin_date = null;
Date end_date = null;
String[] numStr =null;
String Q=null;
try {
begin_date = sdf.parse(beginTime);//定义起始日期
end_date = sdf.parse(endTime);//定义结束日期
} catch (ParseException e) {
System.out.println("时间转化异常,请检查你的时间格式是否为yyyy-MM或yyyy-MM-dd");
}
Calendar dd = Calendar.getInstance();//定义日期实例
dd.setTime(begin_date);//设置日期起始时间
while(!dd.getTime().after(end_date)){//判断是否到结束日期
numStr= sdf.format(dd.getTime()).split("-",0);
Q = getQuarter(Integer.valueOf(numStr[1]))+"";
rangeSet.add(numStr[0].toString()+"年第"+Q+"季度");
dd.add(Calendar.MONTH, 1);//进行当前日期月份加1
}
for ( int i = 0 ; i < rangeSet.size() - 1 ; i ++ ) {
for ( int j = rangeSet.size() - 1 ; j > i; j -- ) {
if (rangeSet.get(j).equals(rangeSet.get(i))) {
rangeSet.remove(j);
}
}
}
System.out.println(rangeSet);
return rangeSet;
}

/**
* 根据月获得季度
* @param month 月
* @return 季度
*/
private static int getQuarter(int month) {
if(month == 1 || month == 2 || month == 3){
return 1;
}else if(month == 4 || month == 5 || month == 6){
return 2;
}else if(month == 7 || month == 8 || month == 9){
return 3;
}else{
return 4;
}
}
/**
* 获取年
* @param begin
* @param end
* @return
*/
public static List<String> getBetweenYear(String beginTime,String endTime){
SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
List<String> betweenList = new ArrayList<String>();
try{
Calendar startDay = Calendar.getInstance();
startDay.setTime(sdf.parse(beginTime));
startDay.add(Calendar.YEAR, -1);
String endYear = endTime.substring(0,4);
while(true){
startDay.add(Calendar.YEAR, 1);
Date newDate = startDay.getTime();
String newend=sdf.format(newDate);
betweenList.add(newend);
if(endYear.equals(newend)){
break;
}
}
}catch (Exception e) {
e.printStackTrace();
}

return betweenList;
}


/**********************前一周的开始时间***********************/
public static Date lastMonday() throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date start = sdf.parse("2019-09-25");// 定义起始日期
Calendar calendar = Calendar.getInstance();
calendar.setTime(start);
while (calendar.get(Calendar.DAY_OF_WEEK) != Calendar.SUNDAY) {
calendar.add(Calendar.DAY_OF_WEEK, -1);
}
int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK) - 1;
int offset = 1 - dayOfWeek;
calendar.add(Calendar.DATE, offset - 7);
return getFirstDayOfWeek(calendar.getTime(), 1);
}
public static Date getFirstDayOfWeek(Date date, int firstDayOfWeek) {
Calendar cal = Calendar.getInstance();
if (date != null)
cal.setTime(date);
cal.setFirstDayOfWeek(firstDayOfWeek);//设置一星期的第一天是哪一天
cal.set(Calendar.DAY_OF_WEEK, firstDayOfWeek);//指示一个星期中的某天
cal.set(Calendar.HOUR_OF_DAY, 0);//指示一天中的小时。HOUR_OF_DAY 用于 24 小时制时钟。例如,在 10:04:15.250 PM 这一时刻,HOUR_OF_DAY 为 22。
cal.set(Calendar.MINUTE, 0);//指示一小时中的分钟。例如,在 10:04:15.250 PM 这一时刻,MINUTE 为 4。
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date sundayDate = cal.getTime();
String weekEnd = sdf.format(sundayDate);
System.out.println("所在周星期日的日期:" + weekEnd);
return cal.getTime();
}

///////////////////////在时间范围内显示周、周的起始时间/////////////////////////////////////
public static void main(String[] args) throws Exception {
Map<String, Object> map = getBetweenWeeks("2019-12-20", "2020-01-25");
System.out.println("week************"+ map.get("week"));
System.out.println("time************"+ map.get("time"));
}

public static Map<String, Object> getBetweenWeeks(String startDate, String endDate) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date start = sdf.parse(startDate);// 定义起始日期
Date end = sdf.parse(endDate);// 定义结束日期
Map<String, Object> result = new HashMap<String, Object>();
Calendar tempStart = Calendar.getInstance();
tempStart.setTime(start);

Calendar tempEnd = Calendar.getInstance();
tempEnd.setTime(end);

List<String> list_Week = Lists.newArrayList();
List<String> list_Time = Lists.newArrayList();

boolean b = false;
String weekBetween = "";
int week = tempStart.get(Calendar.WEEK_OF_YEAR);

while (tempStart.before(tempEnd) || tempStart.equals(tempEnd)) {
if (!b) {
int we = tempStart.get(Calendar.DAY_OF_WEEK);
if (we != 1) {
b = false;
} else {
if (tempStart.getTimeInMillis() > tempEnd.getTimeInMillis()) {
weekBetween = sdf.format(end);
} else {
weekBetween = sdf.format(tempStart.getTime());
}
b = true;
list_Week.add(tempStart.get(Calendar.YEAR) + "年第"+ week + "周");
list_Time.add(sdf.format(start) + "至" + weekBetween);
weekBetween = "";
}
tempStart.add(Calendar.DAY_OF_YEAR, 1);
} else {
week = tempStart.get(Calendar.WEEK_OF_YEAR);
int we = tempStart.get(Calendar.DAY_OF_WEEK);
if (we == 2) {
weekBetween = sdf.format(tempStart.getTime());
}
if (weekBetween.isEmpty()) { // 检测map是否为空
tempStart.add(Calendar.DAY_OF_YEAR, 1);
} else {
tempStart.add(Calendar.DAY_OF_YEAR, 6);
if (tempStart.getTimeInMillis() > tempEnd.getTimeInMillis()) {
list_Time.add(weekBetween + "至" +sdf.format(end));
} else {
list_Time.add(weekBetween + "至" + sdf.format(tempStart.getTime()));
}
list_Week.add(tempStart.get(Calendar.YEAR) + "年第"+ week+ "周");
weekBetween = "";
}
result.put("time",list_Time);
result.put("week",list_Week);
}
}
return result;
}
///////////////////////在时间范围内显示周、周的起始时间/////////////////////////////////////


/*************************java获取当前时间所在一周的周一和周日日期*************************/
public static Map<String,String> getWeekDate(String startDate) throws ParseException {
Map<String,String> map = new HashMap();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

Date start = sdf.parse(startDate);// 定义起始日期
Calendar cal = Calendar.getInstance();
cal.setTime(start);
cal.setFirstDayOfWeek(Calendar.MONDAY);// 设置一个星期的第一天,按中国的习惯一个星期的第一天是星期一
int dayWeek = cal.get(Calendar.DAY_OF_WEEK);// 获得当前日期是一个星期的第几天
if(dayWeek==1){
dayWeek = 8;
}
System.out.println("要计算日期为:" + sdf.format(cal.getTime())); // 输出要计算日期

cal.add(Calendar.DATE, cal.getFirstDayOfWeek() - dayWeek);// 根据日历的规则,给当前日期减去星期几与一个星期第一天的差值
Date mondayDate = cal.getTime();
String weekBegin = sdf.format(mondayDate);
System.out.println("所在周星期一的日期:" + weekBegin);


cal.add(Calendar.DATE, 4 +cal.getFirstDayOfWeek());
Date sundayDate = cal.getTime();
String weekEnd = sdf.format(sundayDate);
System.out.println("所在周星期日的日期:" + weekEnd);

map.put("mondayDate", weekBegin);
map.put("sundayDate", weekEnd);
return map;
}
/*************************java获取当前时间所在一周的周一和周日日期*************************/

///////////////////////////////Java获取本周一、上周一、下周一时间/////////////////////////////////
public static Date geLastWeekMonday(Date date) {
Calendar cal = Calendar.getInstance();
cal.setTime(getThisWeekMonday(date));
cal.add(Calendar.DATE, -7);
return cal.getTime();
}

public static Date getThisWeekMonday(Date date) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
// 获得当前日期是一个星期的第几天
int dayWeek = cal.get(Calendar.DAY_OF_WEEK);
if (1 == dayWeek) {
cal.add(Calendar.DAY_OF_MONTH, -1);
}
// 设置一个星期的第一天,按中国的习惯一个星期的第一天是星期一
cal.setFirstDayOfWeek(Calendar.MONDAY);
// 获得当前日期是一个星期的第几天
int day = cal.get(Calendar.DAY_OF_WEEK);
// 根据日历的规则,给当前日期减去星期几与一个星期第一天的差值
cal.add(Calendar.DATE, cal.getFirstDayOfWeek() - day);
return cal.getTime();
}

public static Date getNextWeekMonday(Date date) {
Calendar cal = Calendar.getInstance();
cal.setTime(getThisWeekMonday(date));
cal.add(Calendar.DATE, 7);
return cal.getTime();
}

public static void main(String[] args) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
try {
Date date = sdf.parse("2017-09-10");
System.out.println("今天是" + sdf.format(date));
System.out.println("上周一" + sdf.format(geLastWeekMonday(date)));
System.out.println("本周一" + sdf.format(getThisWeekMonday(date)));
System.out.println("下周一" + sdf.format(getNextWeekMonday(date)));
} catch (Exception e) {
e.printStackTrace();
}
}
///////////////////////////////Java获取本周一、上周一、下周一时间/////////////////////////////////

/**************************本周的所有日期*******************************/
public static List<String> getDateToWeek(Date date){
List<String> dateWeekList = new ArrayList<String>();
SimpleDateFormat sdf = new SimpleDateFormat("MM月dd日");
String time = "";
//flag用来存取与当天日期的相差数
int flag = 0;
for(int i=1;i<8;i++){
//新建日历
Calendar cal = Calendar.getInstance();
//在日历中找到当前日期
cal.setTime(date);
//当前日期时本周第几天,默认按照西方惯例上周星期天为第一天
flag = -cal.get(Calendar.DAY_OF_WEEK);
//根据循环。当天与上周星期天和本周一到周五相差的天数
cal.add(Calendar.DATE, flag+i);
//转化格式
time = sdf.format(cal.getTime());
//存入list
dateWeekList.add(time);
}
return dateWeekList;
}

/**
* 获取上一周日的日期
* @param beginTime
* @return
* @throws Exception
*/
public static String lastSunday(String beginTime){
String lastTime = null;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
try{
Date start = sdf.parse(beginTime);// 定义起始日期
Calendar calendar = Calendar.getInstance();
calendar.setTime(start);
while (calendar.get(Calendar.DAY_OF_WEEK) != Calendar.SUNDAY) {
calendar.add(Calendar.DAY_OF_WEEK, -1);
}
int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK) - 1;
int offset = 1 - dayOfWeek;
calendar.add(Calendar.DATE, offset - 7);

calendar.setFirstDayOfWeek(1);//设置一星期的第一天是哪一天
calendar.set(Calendar.DAY_OF_WEEK, 1);//指示一个星期中的某天
calendar.set(Calendar.HOUR_OF_DAY, 0);//指示一天中的小时。HOUR_OF_DAY 用于 24 小时制时钟。例如,在 10:04:15.250 PM 这一时刻,HOUR_OF_DAY 为 22。
calendar.set(Calendar.MINUTE, 0);//指示一小时中的分钟。例如,在 10:04:15.250 PM 这一时刻,MINUTE 为 4。
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
Date sundayDate = calendar.getTime();
lastTime = sdf.format(sundayDate);
}catch (Exception e) {
e.printStackTrace();
}
return lastTime;
}

 


原文链接:https://www.cnblogs.com/run1/p/11671640.html
如有疑问请与原作者联系

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:6 种微服务 RPC 框架,你知道几个?

下一篇:请求转发和重定向的区别