从前端拿到的日期数据是什么格式呢?
问题描述:
从前端拿到的日期数据是什么格式呢?是毫秒数,还是yyyy-mm-dd。我记得之前有个地方后端转化日期格式(java.util.date)后,还要转成java.sql.date,这里不用吗?SimpleDateFormat只是把格式相同的字符串转化成了Date数据吗?
相关代码:
</el-descriptions-item>
<el-descriptions-item label="请假时间">
<el-form-item prop="timeRange" label-width="0px">
<div class="block">
<el-date-picker
v-model="form.timeRange"
type="datetimerange"
range-separator="至"
start-placeholder="开始日期"
end-placeholder="结束日期"
@change="changeTimeRange">
</el-date-picker>
</div>
</el-form-item>
</el-descriptions-item>
相关代码:
data() {
return {
employee:{},
department:{},
form: {
formType: "1",
timeRange: "",
startTime: "",
endTime: "",
reason: "",
eid: ""
},
// 表单验证,需要在 el-form-item 元素中增加 prop 属性
rules: {
timeRange: [
{required: true, message: '请选择请假时间', trigger: 'blur'}
],
reason: [
{required: true, message: '请填写请假原因', trigger: 'blur'}
]
}
}
}
,methods:{
changeTimeRange : function(){
console.info(this.form.timeRange);
this.form.startTime = this.form.timeRange[0].getTime();
this.form.endTime = this.form.timeRange[1].getTime();
}
,onSubmit(formName){
const objApp = this;
const formData = this.form;
const $message = this.$message;
this.$refs[formName].validate(function(valid){
if(valid){
const params = new URLSearchParams();
params.append("formType", formData.formType);
params.append("startTime", formData.startTime);
params.append("endTime", formData.endTime);
params.append("reason", formData.reason);
params.append("eid", sessionStorage.eid);
axios.post("/api/leave/create",params)
.then(function(response){
console.info(response);
const json = response.data;
if(json.code == "0"){
objApp.$alert("请假单已提交,等待上级审批",{
callback:function(){
window.location.href = "/notice.html";
}
})
}else{
$message.error({message:json.message,offset:100})
}
})
}
})
}
}
相关代码:
private void create(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String strEmployeeId = request.getParameter("eid");
String formType = request.getParameter("formType");
//从1970年到现在的毫秒数
String startTime = request.getParameter("startTime");
String endTime = request.getParameter("endTime");
String reason = request.getParameter("reason");
LeaveForm form = new LeaveForm();
form.setEmployeeId(Long.parseLong(strEmployeeId));
form.setStartTime(new Date(Long.parseLong(startTime)));
form.setEndTime(new Date(Long.parseLong(endTime)));
form.setFormType(Integer.parseInt(formType));
form.setReason(reason);
form.setCreateTime(new Date());
ResponseUtils resp = null;
try {
leaveFormService.createLeaveForm(form);
resp = new ResponseUtils();
} catch (Exception e) {
e.printStackTrace();
resp = new ResponseUtils(e.getClass().getSimpleName(), e.getMessage());
}
response.getWriter().println(resp.toJsonString());
}
相关代码:
public LeaveForm createLeaveForm(LeaveForm form) {
LeaveForm f = (LeaveForm) MybatisUtils.executeUpdate(sqlSession -> {
//1.持久化form表单数据,8级以下员工表单状态为processing,8级(总经理)状态为approved
EmployeeMapper employeeMapper = sqlSession.getMapper(EmployeeMapper.class);
Employee employee = employeeMapper.selectById(form.getEmployeeId());
if (employee.getLevel() == 8) {
form.setState("approved");
} else {
form.setState("processing");
}
LeaveFormMapper leaveFormMapper = sqlSession.getMapper(LeaveFormMapper.class);
leaveFormMapper.insert(form);
NoticeMapper noticeMapper = sqlSession.getMapper(NoticeMapper.class);
//2.增加第一条流程数据,说明表单已提交,状态为complete
ProcessFlowMapper processFlowMapper = sqlSession.getMapper(ProcessFlowMapper.class);
ProcessFlow flow1 = new ProcessFlow();
flow1.setFormId(form.getFormId());
flow1.setOperatorId(employee.getEmployeeId());
flow1.setAction("apply");
flow1.setCreateTime(new Date());
flow1.setOrderNo(1);
flow1.setState("complete");
flow1.setIsLast(0);
processFlowMapper.insert(flow1);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd-HH时");
//3.分情况创建其余流程数据
//3.1 7级以下员工,生成部门经理审批任务,请假时间大于等于72小时,还需生成总经理审批任务
if (employee.getLevel() < 7) {
Employee dmanager = employeeService.selectLeader(employee.getEmployeeId());
ProcessFlow flow2 = new ProcessFlow();
flow2.setFormId(form.getFormId());
flow2.setOperatorId(dmanager.getEmployeeId());
flow2.setAction("audit");
flow2.setCreateTime(new Date());
flow2.setOrderNo(2);
flow2.setState("process");
long diff = form.getEndTime().getTime() - form.getStartTime().getTime();
float hours = diff / (1000 * 60 * 60) * 1f;
if (hours >= 72) {
flow2.setIsLast(0);
processFlowMapper.insert(flow2);
Employee manager = employeeService.selectLeader(dmanager.getEmployeeId());
ProcessFlow flow3 = new ProcessFlow();
flow3.setFormId(form.getFormId());
flow3.setOperatorId(manager.getEmployeeId());
flow3.setAction("audit");
flow3.setCreateTime(new Date());
flow3.setState("ready");
flow3.setOrderNo(3);
flow3.setIsLast(1);
processFlowMapper.insert(flow3);
} else {
flow2.setIsLast(1);
processFlowMapper.insert(flow2);
}
//向请假者发送通知
String notice1 = String.format("您的请假申请[%s-%s]已提交,请等待上级审批.", sdf.format(form.getStartTime()), sdf.format(form.getCreateTime()));
noticeMapper.insert(new Notice(employee.getEmployeeId(), notice1));
//向部门经理发送通知,注意,此时不向总经理发送通知,只有在部门经理审批通过时才向总经理发通知
String notice2 = String.format("%s-%s提起请假申请[%s-%s],请尽快审批", employee.getTitle(), employee.getName(), sdf.format(form.getStartTime()), sdf.format(form.getEndTime()));
noticeMapper.insert(new Notice(dmanager.getEmployeeId(), notice2));
} else if (employee.getLevel() == 7) {
//3.2 7级员工,仅生成总经理审批任务
Employee manager = employeeService.selectLeader(employee.getEmployeeId());
ProcessFlow flow2 = new ProcessFlow();
flow2.setFormId(form.getFormId());
flow2.setOperatorId(manager.getEmployeeId());
flow2.setAction("audit");
flow2.setCreateTime(new Date());
flow2.setState("process");
flow2.setOrderNo(2);
flow2.setIsLast(1);
processFlowMapper.insert(flow2);
//请假单已提交消息
String notice1 = String.format("您的请假申请[%s-%s]已提交,请等待上级审批."
, sdf.format(form.getStartTime()), sdf.format(form.getEndTime()));
noticeMapper.insert(new Notice(employee.getEmployeeId(), notice1));
//通知总经理审批消息
String notice2 = String.format("%s-%s提起请假申请[%s-%s],请尽快审批",
employee.getTitle(), employee.getName(), sdf.format(form.getStartTime()), sdf.format(form.getEndTime()));
noticeMapper.insert(new Notice(manager.getEmployeeId(), notice2));
} else if (employee.getLevel() == 8) {
//3.3 8级员工,生成总经理审批任务,系统自动通过
ProcessFlow flow2 = new ProcessFlow();
flow2.setFormId(form.getFormId());
flow2.setOperatorId(employee.getEmployeeId());
flow2.setAction("audit");
flow2.setResult("approved");
flow2.setReason("自动通过");
flow2.setCreateTime(new Date());
flow2.setAuditTime(new Date());
flow2.setState("complete");
flow2.setOrderNo(2);
flow2.setIsLast(1);
processFlowMapper.insert(flow2);
String noticeContent = String.format("您的请假申请[%s-%s]系统已自动批准通过.",
sdf.format(form.getStartTime()), sdf.format(form.getEndTime()));
noticeMapper.insert(new Notice(employee.getEmployeeId(), noticeContent));
}
return form;
});
return f;
}
3
收起
正在回答 回答被采纳积分+1
1回答
好帮手慕小蓝
2024-07-30 11:18:03
同学你好~
1.前端获取的日期格式是通过后端代码指定的,即Java代码中格式化之后的形式。例如格式化为yyyy-MM-dd,那么前端收到的也是yyyy-MM-dd。
2.java.util.Date转换为java.sql.Date是数据存入数据库时需要做的,反向转换是从数据库读取时做的。这主要是因为数据库并不能直接兼容java.util.Date,必须进行转化。
3.SimpleDateFormat类用于java.util.Date和String之间转换用的,将java.util.Date转换为java.sql.Date需要使用java.sql.Date的构造方法,new java.sql.Date(new java.util.Date().getTime());
祝学习愉快~
Java工程师 2024版
- 参与学习 2020 人
- 提交作业 1249 份
- 解答问题 1202 个
2024重磅革新,超百小时内容豪华升级,加速提升高级技能与高薪就业竞争力 课程紧贴企业最新人才需求,历经7年持续迭代,帮助万名学子入行转行 从零起点到高阶实战,学习路径稳健顺滑,成就从小白到工程师高薪
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星