单独的日期和时间对象

4 java time date

我有一个网络表单,用户可以从日历弹出窗口中选择日期,从下拉列表中选择时间。目前我正在尝试使用 Date 对象存储日期。

@Required
public Date date;
Run Code Online (Sandbox Code Playgroud)

这个对象的输出类似于:

1970 年 1 月 1 日 00:00:00 GMT

我真正想做的是将其分开并以27/02/2013等格式存储日期,并将时间作为 24 小时格式的单独对象,例如23:45。我不确定如何用 java 来做到这一点。

使用 SimpleDateFormat 解决:

//also the import
import java.text.*;

@Required
public SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd/MM/yy");
public String date = simpleDateFormat.format(new Date());

@Required
public SimpleDateFormat simpleTimeFormat = new SimpleDateFormat("hh:mm");
public String time = simpleTimeFormat.format(new Date());
Run Code Online (Sandbox Code Playgroud)

use*_*ser 5

看看班级吧Calendar。它具有获取不同“日期部分”所需的所有方法。另外,查看SimpleDateFormatjava 中的类以所需的方式格式化日期。

日历 - http://docs.oracle.com/javase/7/docs/api/java/util/Calendar.html

SimpleDateFormat - http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html