对于想了解MySQL表1新增数据,计算开始、结束日期之间所有时间,插入到表2中的读者,本文将是一篇不可错过的文章,我们将详细介绍mysql新增时间字段,并且为您提供关于C#获取两个时间段之间的所有时间
对于想了解MySQL表1新增数据,计算开始、结束日期之间所有时间,插入到表2中的读者,本文将是一篇不可错过的文章,我们将详细介绍mysql新增时间字段,并且为您提供关于C# 获取两个时间段之间的所有时间与获取当前时间所在的季度开始和结束时间、java 根据开始时间和结束时间,计算出时间工作日天数(包含开始和结束时间)、java根据开始时间和结束时间,计算中间天数,并打印、Java获取指定时间段的年份(开始、结束时间)、月份(开始、结束时间)、天数(开始、结束时间)的有价值信息。
本文目录一览:- MySQL表1新增数据,计算开始、结束日期之间所有时间,插入到表2中(mysql新增时间字段)
- C# 获取两个时间段之间的所有时间与获取当前时间所在的季度开始和结束时间
- java 根据开始时间和结束时间,计算出时间工作日天数(包含开始和结束时间)
- java根据开始时间和结束时间,计算中间天数,并打印
- Java获取指定时间段的年份(开始、结束时间)、月份(开始、结束时间)、天数(开始、结束时间)
MySQL表1新增数据,计算开始、结束日期之间所有时间,插入到表2中(mysql新增时间字段)
新建表c3
#id设置自增量,kk是要同步的一些数据值,dd是开始时间,dde是结束时间
CREATE TABLE c3 (
id int(11) NOT NULL auto_increment PRIMARY KEY,
kk varchar(255),
dd datetime,
dde datetime
)
新建表c4
#id也设置自增量,kk是要同步的一些数据值,dd是统计出开始与结束之间的所有日期
CREATE TABLE c4 (
id int(11) NOT NULL auto_increment PRIMARY KEY,
id_c3 int(11),
kk varchar(255),
dd datetime
)
创建触发器
# 设定一个变量i,默认值是0,DATEDIFF(new.dde,new.dd)是计算日期差值,
#DATE_ADD(new.dd,interval i DAY)通过变量i来补充所有日期,
CREATE TRIGGER c3_insert_c4 AFTER INSERT
ON c3 FOR EACH ROW
BEGIN
DECLARE i int DEFAULT 0;
WHILE i <= DATEDIFF(new.dde,new.dd)
DO
INSERT INTO c4(id_c3,kk,dd) VALUES (new.id,new.kk,DATE_ADD(new.dd,INTERVAL i DAY));
SET i=i+1;
END WHILE;
END
想表c3插入一条数据,并查询c3与c4
INSERT INTO c3(kk,dd,dde) VALUES(''ee'',''2020-04-07'',''2020-04-08'');
select * from c3;
select * from c4;
C# 获取两个时间段之间的所有时间与获取当前时间所在的季度开始和结束时间
一:C# 获取两个时间段之间的所有时间
public List<string> GetTimeList(string rq1, string rq2)
{
List<string> timeList = new List<string>();
// 首先保证 rq1<=rq2
DateTime time1 = Convert.ToDateTime(rq1);
DateTime time2 = Convert.ToDateTime(rq2);
while (time1 <= time2)
{
timeList.Add(time1.ToString("yyyy-MM-dd"));
time1 = time1.AddDays(1);
}
return timeList;
}
使用:
List<string> sjList = GetTimeList(sj1, sj2);
二:获取当前时间所在季度的开始和结束时间
// 本季度
private List<string> FunSelectSJNow(string jd)
{
List<string> lis = new List<string>();
string NowYear = jd.Substring (0, 4); // 截取当前年份
string Nowjd = jd.Substring (jd.Length - 2, 2); // 截取当前季度
string StartTime = ""; // 当前季度的开始时间
string endTime = ""; // 当前季度的结束时间
if (Nowjd == "01")
{
StartTime = "-01-01";
endTime = "-03-01";
}
if (Nowjd == "02")
{
StartTime = "-04-01";
endTime = "-06-01";
}
if (Nowjd == "03")
{
StartTime = "-07-01";
endTime = "-09-01";
}
if (Nowjd == "04")
{
StartTime = "-10-01";
endTime = "-12-01";
}
StartTime = NowYear + StartTime;
endTime = NowYear + endTime;
lis.Add(StartTime);
lis.Add(endTime);
return lis;
}
// 上季度
private List<string> FunSelectLastSJ(string jd)
{
List<string> lis = new List<string>();
string NowYear = jd.Substring (0, 4); // 截取当前年份
string Nowjd = jd.Substring (jd.Length - 2, 2); // 截取当前季度
string StartTime = ""; // 上季度的开始时间
string endTime = ""; // 上季度的结束时间
string endYear = ""; // 上季度年
string endjd = ""; // 上季度季度
if (Nowjd == "01")
{
endYear = (Convert.ToInt32(NowYear) -1).ToString() ;
endjd = "Q4";
}
if (Nowjd == "02")
{
endYear = NowYear;
endjd = "Q1";
}
if (Nowjd == "03")
{
endYear = NowYear;
endjd = "Q2";
}
if (Nowjd == "04")
{
endYear = NowYear;
endjd = "Q3";
}
if (endjd == "Q1")
{
StartTime = "-01-01";
endTime = "-03-01";
}
if (endjd == "Q2")
{
StartTime = "-04-01";
endTime = "-06-01";
}
if (endjd == "Q3")
{
StartTime = "-07-01";
endTime = "-09-01";
}
if (endjd == "Q4")
{
StartTime = "-10-01";
endTime = "-12-01";
}
StartTime = endYear + StartTime;
endTime = endYear + endTime;
lis.Add(StartTime);
lis.Add(endTime);
return lis;
}
使用:
List<string> li = FunSelectSJNow(NowTime);
rq1 = li[0];
rq2 = li[1];
//jd: Q1,Q2,Q3,Q4 (四个季度)
List<string> li = FunSelectLastSJ(jd);
rq1 = li[0];
rq2 = li[1];
java 根据开始时间和结束时间,计算出时间工作日天数(包含开始和结束时间)
public int weekDay(String strStartDate, String strEndDate) { //strStartDate:"2019-10-25",strEndDate:"2019-12-10" SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd"); Calendar clOne = Calendar.getInstance(); Calendar clTwo = Calendar.getInstance(); try { clOne.setTime(df.parse(strStartDate)); clTwo.setTime(df.parse(strEndDate)); } catch (ParseException e) { System.out.println("日期格式非法"); e.printStackTrace(); } int days = 0; while (clOne.compareTo(clTwo) <= 0) { if (clOne.get(Calendar.DAY_OF_WEEK) != Calendar.SATURDAY && clOne.get(Calendar.DAY_OF_WEEK) != Calendar.SUNDAY) days++; clOne.add(Calendar.DAY_OF_MONTH, 1); } return days; }
java根据开始时间和结束时间,计算中间天数,并打印
java根据开始时间和结束时间,计算中间天数,并打印
import java.text.SimpleDateFormat;
import java.util.Date;
public class Calcdate {
static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
static SimpleDateFormat ymd = new SimpleDateFormat("yyyy-MM-dd");
public static void main(String[] args)throws Exception{
Date beginTime = sdf.parse("2015-04-23 12:32:44");
// Date endTime = sdf.parse("2015-04-23 14:56:44");
Date endTime = sdf.parse("2015-04-29 14:56:44");
long diff = (endTime.getTime() - beginTime.getTime()) / (86400 * 1000);
System.out.println("diff = " + diff);
Date sb = null;
Date se = null;
if(diff == 0){
sb = beginTime;
se = endTime;
String ss = String.format("%d> Sb=%s, Se=%s", 1, Coder.DateTimeToStr(sb), Coder.DateTimeToStr(se));
System.out.println(ss);
}else {
int i = 0;
while (i <= diff) {
if (i == 0) {
sb = beginTime;
se = sdf.parse(ymd.format(sb) + " 23:59:59");
} else if (i == diff) {
sb = sdf.parse(ymd.format(endTime) + " 00:00:00");
se = endTime;
} else {
se = new Date(beginTime.getTime() + i * 24 * 60 * 60 * 1000);
sb = sdf.parse(ymd.format(se) + " 00:00:00");
se = sdf.parse(ymd.format(se) + " 23:59:59");
}
String ss = String.format("%d> Sb=%s, Se=%s", i, Coder.DateTimeToStr(sb), Coder.DateTimeToStr(se));
System.out.println(ss);
i++;
}
}
}
}
diff = 6
0> Sb=2015-04-23 12:32:44, Se=2015-04-23 23:59:59
1> Sb=2015-04-24 00:00:00, Se=2015-04-24 23:59:59
2> Sb=2015-04-25 00:00:00, Se=2015-04-25 23:59:59
3> Sb=2015-04-26 00:00:00, Se=2015-04-26 23:59:59
4> Sb=2015-04-27 00:00:00, Se=2015-04-27 23:59:59
5> Sb=2015-04-28 00:00:00, Se=2015-04-28 23:59:59
6> Sb=2015-04-29 00:00:00, Se=2015-04-29 14:56:44
Java获取指定时间段的年份(开始、结束时间)、月份(开始、结束时间)、天数(开始、结束时间)
package test;
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.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
public class DateTest {
public static void main(String[] args) {
System.out.println(getYears("2016-01-01 00:00:00","2018-07-01 00:00:00"));
System.out.println(getMonths("2018-01-01 00:00:00","2018-07-01 00:00:00"));
System.out.println(getDays("2018-06-01 00:00:00","2018-07-01 00:00:00"));
}
/***
* 获取两个时间段的年份/年第一天/年最后一天
* @param startTime
* @param endTime
* @return
*/
public static List<Map> getYears(String startTime, String endTime) {
List<Map> res=new ArrayList<Map>();
DateFormat dateFormat = new SimpleDateFormat("yyyy");
DateFormat dateFormat2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
Date start = dateFormat.parse(startTime);
Date end = dateFormat.parse(endTime);
Calendar tempStart = Calendar.getInstance();
tempStart.setTime(start);
Calendar tempEnd = Calendar.getInstance();
tempEnd.setTime(end);
tempEnd.add(Calendar.YEAR,1);// 日期加1(包含结束)
while (tempStart.before(tempEnd)) {
String year=dateFormat.format(tempStart.getTime());
String first=year+"-01-01 00:00:00";
String last=year+"-12-31 23:59:59";
Map<String,Object> map=new HashMap<String,Object>();
map.put("year", year);
map.put("first", dateFormat2.parse(first));
map.put("last", dateFormat2.parse(last));
res.add(map);
tempStart.add(Calendar.YEAR,1);
}
} catch (ParseException e) {
e.printStackTrace();
}
return res;
}
/***
* 获取两个时间段的年份-月份/月第一天/月最后一天
* @param startTime
* @param endTime
* @return
*/
public static List<Map> getMonths(String startTime, String endTime) {
List<Map> res=new ArrayList<Map>();
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM");
DateFormat dateFormat2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
DateFormat dateFormat3 = new SimpleDateFormat("yyyy-MM-dd");
try {
Date start = dateFormat.parse(startTime);
Date end = dateFormat.parse(endTime);
Calendar tempStart = Calendar.getInstance();
tempStart.setTime(start);
Calendar tempEnd = Calendar.getInstance();
tempEnd.setTime(end);
tempEnd.add(Calendar.MONTH,1);// 日期加1(包含结束)
while (tempStart.before(tempEnd)) {
String month=dateFormat.format(tempStart.getTime());
tempStart.set(Calendar.DAY_OF_MONTH, 1);
String first=dateFormat3.format(tempStart.getTime());
tempStart.set(Calendar.DAY_OF_MONTH, tempStart.getActualMaximum(Calendar.DAY_OF_MONTH));
Map<String,Object> map=new HashMap<String,Object>();
map.put("month", month);
map.put("first", dateFormat2.parse(first+" 00:00:00"));
map.put("last", dateFormat2.parse(first+" 23:59:59"));
res.add(map);
tempStart.add(Calendar.MONTH,1);
}
} catch (ParseException e) {
e.printStackTrace();
}
return res;
}
/***
* 获取两个时间段的天数/开始时间/结束时间
* @param startTime
* @param endTime
* @return
*/
public static List<Map> getDays(String startTime, String endTime) {
// 返回的日期集合
List<Map> res=new ArrayList<Map>();
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
DateFormat dateFormat2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
Date start = dateFormat.parse(startTime);
Date end = dateFormat.parse(endTime);
Calendar tempStart = Calendar.getInstance();
tempStart.setTime(start);
Calendar tempEnd = Calendar.getInstance();
tempEnd.setTime(end);
tempEnd.add(Calendar.DATE, +1);// 日期加1(包含结束)
while (tempStart.before(tempEnd)) {
String day=dateFormat.format(tempStart.getTime());
Map<String,Object> map=new HashMap<String,Object>();
map.put("day", day);
map.put("first", dateFormat2.parse(day+" 00:00:00"));
map.put("last", dateFormat2.parse(day+" 23:59:59"));
res.add(map);
tempStart.add(Calendar.DAY_OF_YEAR, 1);
}
} catch (ParseException e) {
e.printStackTrace();
}
return res;
}
}
我们今天的关于MySQL表1新增数据,计算开始、结束日期之间所有时间,插入到表2中和mysql新增时间字段的分享已经告一段落,感谢您的关注,如果您想了解更多关于C# 获取两个时间段之间的所有时间与获取当前时间所在的季度开始和结束时间、java 根据开始时间和结束时间,计算出时间工作日天数(包含开始和结束时间)、java根据开始时间和结束时间,计算中间天数,并打印、Java获取指定时间段的年份(开始、结束时间)、月份(开始、结束时间)、天数(开始、结束时间)的相关信息,请在本站查询。
本文标签: