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

标签 java javascript jsp date datetime

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/

相关文章:

java - Jface ContentProposalAdapter 不适用于第一个输入的字符

javascript - 有没有办法在函数范围内迭代公共(public)方法?

javascript - 单击类别时如何显示不同的项目列表

java - 如何使用 Spring MVC 将列表中的项目绑定(bind)到表单 modelAttribute

java - mvc 的正确文件结构(servlet、jsp、web.xml)

javascript - 无法在服务器上运行jsp

java - 如何分享 Java 游戏?

java - MySQL 分区表的运行速度比同等的未分区表慢

java - 使用 AmazonSQSClient 的消息消耗缓慢

javascript - 等待条件为真,然后继续执行函数