单独的日期和时间对象 [英] Separate Date and Time objects
问题描述
我有一个网络表单,用户可以在其中从日历弹出窗口中选择日期,并从下拉菜单中选择时间。目前,我正在尝试使用Date对象存储日期。
I have a web-form where users can select a date from a calendar pop-up and a time from a dropdown. At the moment I am trying to store the date using a Date object.
@Required
public Date date;
该对象的输出类似于:
1970年1月1日,格林尼治标准时间
January 1, 1970, 00:00:00 GMT
我真正想要的是要做的就是将其分开并以 2013年2月27日的格式存储日期,并将时间作为24小时格式的单独对象,例如 23:45 。我不确定如何使用Java。
What I would really like to do is separate this and store the date in a format like 27/02/2013 and have the time as a separate object in 24 hour format e.g. 23:45. I am unsure how to do this with java.
使用SimpleDateFormat解决:
Resolved using 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());
推荐答案
看看日历
类。它具有获取不同日期部分所需的所有方法。另外,请查看Java中的 SimpleDateFormat
类,以所需的方式设置日期格式。
Have a look at the Calendar
class. It has all of the methods required to get different "date parts". Also, look at the SimpleDateFormat
class in java to format the date in needed way.
日历- 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
SimpleDateFormat - http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html
这篇关于单独的日期和时间对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!