gpt4 book ai didi

java - 在jsp中合并日期和时间

转载 作者:行者123 更新时间:2023-12-02 06:22:38 25 4
gpt4 key购买 nike

var dateString=$('#strtime').val();//starttime retrieved from db,(type is Time in the database)
var dateString2=$('#endtime').val();//endtime retrieved from db,(type is Time in the database)
var dateString1=$('#date').val();//date retrieved from db,(type is date in the database)
var d1 = new Date(dateString1+" "+dateString);//combine date and starttime retireved from the database.
var d5= new Date(dateString1+" "+dateString2);//combine date and endtime retireved from the database.

我想知道上述 javascript 的 Java 等效项

我试过了

Date d1 = new Date(dateString1.getTime()+dateString.getTime());
System.out.println("d1="+d1);

//输出

d1=2013-12-27

这仅输出日期,而不输出时间

实际输出应该是

d1=2013-12-27 14:30:00

最佳答案

尝试以下

DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println("d1 = " + dateFormat.format(d1));

编辑

尝试以下方法

private Date combineDateTime(Date date, Date time)
{
Calendar calendarA = Calendar.getInstance();
calendarA.setTime(date);
Calendar calendarB = Calendar.getInstance();
calendarB.setTime(time);

calendarA.set(Calendar.HOUR_OF_DAY, calendarB.get(Calendar.HOUR_OF_DAY));
calendarA.set(Calendar.MINUTE, calendarB.get(Calendar.MINUTE));
calendarA.set(Calendar.SECOND, calendarB.get(Calendar.SECOND));
calendarA.set(Calendar.MILLISECOND, calendarB.get(Calendar.MILLISECOND));

Date result = calendarA.getTime();
return result;
}

关于java - 在jsp中合并日期和时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20876897/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com