java获取时间段内的所有星期一

编程入门 行业动态 更新时间:2024-10-21 16:22:23

java获取时间段内的所有<a href=https://www.elefans.com/category/jswz/34/1768596.html style=星期一"/>

java获取时间段内的所有星期一

原文:

 

前言:

最近有个功能需要用到对于时间的操作,网上有个不错的文章,如上贴出,小编稍微改了点代码,在此对原作者表示感谢,希望能够帮助有需要的人

功能: 

传入开始时间 结束时间 星期一,即可得出这段时间有几个星期一,将时间列出。

 

代码:

package com.bosmon;import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;/**** 获取两个日期之间的所有周四 并计算每个周四在当月是第几周* @author lzw* @Date 2019年3月6日*/
public class DateUtil {/*** @param args* @throws Exception*/public static void main(String[] args) throws Exception {String week = "星期二";List<String> test = getWeekly("2020-01-07 14:19:29","2020-01-14 00:00:00",week);System.err.println(test);}public static boolean weekdayOrNot(String date,String week) throws ParseException{DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");Date currentDate = sdf.parse(date);SimpleDateFormat simpleDateFormat = new SimpleDateFormat("EEEE");String currSun = simpleDateFormat.format(currentDate);//判断当前是星期几if (currSun.equals(week)) {return true;}return false;}public static String getWeek(Date date) throws Exception {Calendar calendar = Calendar.getInstance();calendar.setTime(date);DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");String format = sdf.format(date);String substring = format.substring(0, 7);int number = calendar.get(Calendar.WEEK_OF_MONTH);String week = substring + "-0" + number;return week;}/*** 根据开始时间和结束时间计算之间的星期* @param beginDate* @param endDate* @return* @throws Exception*/public static List<String> getWeekly(String beginDate,String endDate,String week) throws Exception{//获取俩个日期之间的日期List<String> list = findDates(beginDate, endDate);List<String> weekDateList = new ArrayList<>();//遍历for (String date : list) {//判断当前是星期几boolean thursday = weekdayOrNot(date,week);if (thursday==true) {weekDateList.add(date);}}return weekDateList;}public static List<String> findDates(String dBegin, String dEnd) throws ParseException {DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");Calendar calBegin = Calendar.getInstance();calBegin.setTime(format.parse(dBegin));Calendar calEnd = Calendar.getInstance();calEnd.setTime(format.parse(dEnd));List<String> Datelist = new ArrayList<String>();while (format.parse(dEnd).after(calBegin.getTime())) {calBegin.add(Calendar.DAY_OF_MONTH, 1);Datelist.add(format.format(calBegin.getTime()));}Datelist.add(dBegin);return Datelist;}/*** 日期转星期** @param datetime* @return*/public static String currentDataToWeek(String datetime) {SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");String[] weekDays = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};// 获得一个日历Calendar cal = Calendar.getInstance();Date datet = null;try {datet = f.parse(datetime);cal.setTime(datet);} catch (ParseException e) {e.printStackTrace();}// 指示一个星期中的某天。int w = cal.get(Calendar.DAY_OF_WEEK) - 1;if (w < 0)w = 0;return weekDays[w];}
}

 

 

更多推荐

java获取时间段内的所有星期一

本文发布于:2024-03-11 20:19:22,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1729823.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:星期   时间段内   java

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!