问题与Java日期格式 [英] Issue with Java Date format
问题描述
在我的应用程序中,我允许我的用户输入一些日期,我应该保存在数据库中。现在,由于我的一些应用用户位于全球各地,每个都有自己的日期/时间格式,所以我想接受他们的任何日期格式,然后将这些格式更改为固定格式,然后我可以保存在DB中。 / p>
然而,我的问题是,当尝试从用户输入的日期改为我的应用程序格式时,我收到错误,下面是我目前正在使用的代码
-
实体字段:
@Temporal(TemporalType.TIMESTAMP)
@Column(name =DDATE,nullable = false,unique = false)
private Date dDate;
-
保存在DB中的控制器代码:
日期rDate,dDate;
String Date1 = request.getParameter(Date1);
String Date2 = request.getParameter(Date2);
//这里的日期显示为01/29/2014(即MM / DD / YYYY)
System.out.println(Date1 ::+ Date1);
System.out.println(Date2 ::+ Date2);
SimpleDateFormat parseRDate = new SimpleDateFormat(yyyy-MM-dd HH:mm:ss);
SimpleDateFormat parseDDate = new SimpleDateFormat(yyyy-MM-dd HH:mm:ss);
尝试{
// #########崩溃在接下来的两行######### ...
rDate =(Date)parseRDate。解析(Date1);
dDate =(Date)parseDDate.parse(Date2);
} catch(ParseException e){
e.printStackTrace();
}
建议我如何将用户输入的任何日期格式化为应用程序静态日期格式?
感谢
我的建议是使用 http://en.wikipedia.org/wiki / ISO_8601 作为所有人的通用日期。
如果任何人可以输入任何字符串,您永远不会知道08/12/75是12月8日或8月12日
从维基百科:
日期和时间页面生成)
根据ISO 8601表示:
日期:2014-01-11
UTC中的合并日期和时间:2014-01-11T12:21:05 + 00:00
2014-01-11T12:21Z
周:2014-W02
周数:2014-W02-6
有序日期:2014-011
Inside my application I allow my users to enter some dates, which I should save in DB. Now since some of my application users are located in different locations worldwide and each have their own date/time format, I thought of accepting any date format from them, then change those formats to a fixed format which I can then save in DB.
Yet my problem now is that I getting an error when trying to change date fromat from that entered by the user to my application format, below is the code I am currently working with
Entity field:
@Temporal(TemporalType.TIMESTAMP) @Column(name="DDATE", nullable=false, unique=false) private Date dDate;
Controller code that save in DB:
Date rDate, dDate; String Date1 = request.getParameter("Date1"); String Date2 = request.getParameter("Date2"); //Here the date get display for example as 01/29/2014 (i.e. MM/DD/YYYY) System.out.println("Date1:: "+ Date1); System.out.println("Date2:: "+ Date2); SimpleDateFormat parseRDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); SimpleDateFormat parseDDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); try { //#########Crashes in the next two lines#########... rDate = (Date)parseRDate.parse(Date1); dDate = (Date)parseDDate.parse(Date2); } catch (ParseException e) { e.printStackTrace(); }
So can someone please suggest how I can format any date entered by my users to the application static date format?
Thanks
My suggestion is to use http://en.wikipedia.org/wiki/ISO_8601 as universal date for everyone.
If anyone can enter any string, you'll never know if 08/12/75 is December 8th or August 12th
From the wikipedia:
Date and time (current at page generation)
expressed according to ISO 8601:
Date: 2014-01-11
Combined date and time in UTC: 2014-01-11T12:21:05+00:00
2014-01-11T12:21Z
Week: 2014-W02
Date with week number: 2014-W02-6
Ordinal date: 2014-011
这篇关于问题与Java日期格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!