欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

获取时间工具

程序员文章站 2022-12-22 18:31:44
/** * 获取相差天数 * @param beginTime * @param endTime * @return */ public static int getDaysBetween(String beginTime,String endTime) { SimpleDateFormat sdf ......

/**
* 获取相差天数
* @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;
}