[英]convert XMLGregorianCalendar to date i.e “MM/DD/YYYY hh:mm:ss AM”
I have a date in XMLGregorianCalendar format like "2013-05-16T09:54:13" which i have to convert to timestamp "MM/DD/YYYY hh:mm:ss AM" for inserting into oracle database table using java. 我有XMLGregorianCalendar格式的日期,如“2013-05-16T09:54:13”,我必须转换为时间戳“MM / DD / YYYY hh:mm:ss AM”,以便使用java插入到oracle数据库表中。
how can i do this in Java? 我怎么能用Java做到这一点?
You can do this to return a Date
: 您可以这样做以返回
Date
:
calendar.toGregorianCalendar().getTime()
I found that code from this tutorial . 我发现了本教程中的代码。 From there, you can use a SimpleDateFormat to turn it into a string in the format you want.
从那里,您可以使用SimpleDateFormat将其转换为您想要的格式的字符串。
But, if you're using JDBC to save the date in the database, you probably can pass in the Date
directly with this method : 但是,如果您使用JDBC在数据库中保存日期,则可以使用此方法直接传递
Date
:
preparedStatement.setDate(colNum, myDate);
Here is more clear answer: 这里有更明确的答案:
Get instance of Date from XMLGregorianCalendar instance: 从XMLGregorianCalendar实例获取Date实例:
Date date = xmlCalendar.toGregorianCalendar().getTime();
I found that code from Convert XMLGregorianCalendar to Date in Java 我发现代码从Convert XMLGregorianCalendar转换为Java中的Date
Format that Date instance with format "MM/dd/yyyy hh:mm:ss a", you will get MM/DD/YYYY hh:mm:ss AM format 格式化格式为“MM / dd / yyyy hh:mm:ss a”的日期实例,您将获得MM / DD / YYYY hh:mm:ss AM格式
DateFormat formatter = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss a");
String formattedDate = formatter.format(date)
From Convert Date to String in Java 从Java中的转换日期到字符串
For inserting database you would do what Daniel suggested 对于插入数据库,你会做丹尼尔建议的
If you want to insert your date on a database I would first do what Daniel suggested: 如果你想在数据库上插入你的日期,我会先做Daniel建议的事情:
XMLGregorianCalendar xgc=<assume this is initialized>;
Date timestamp=xgc.toGregorianCalendar().getTime();
and later insert it through a PreparedStatement as Timestamp in milliseconds (Epoch time). 然后通过PreparedStatement作为Timestamp插入它,以毫秒为单位(纪元时间)。 That way you won't loose precision.
这样你就不会失去精确度。
preparedStatement.setTimestamp(colNum,new Timestamp(timestamp.getTime()));
myPreparedStatement.setObject(
… ,
myXGC.toGregorianCalendar()
.toZonedDateTime()
) ;
The modern approach uses the java.time classes that supplanted the troublesome old classes Date
, Calendar
, and GregorianCalendar
. 现代方法使用java.time类来取代麻烦的旧类
Date
, Calendar
和GregorianCalendar
。
Convert from the legacy classes to java.time. 从遗留类转换为java.time。
GregorianCalendar gc = myXGC.toGregorianCalendar() ;
ZonedDateTime zdt = gc.toZonedDateTime();
Pass date-time values to your database as date-time objects rather than as strings . 将日期时间值作为日期时间对象而不是字符串传递给数据库。
If your JDBC driver complies with JDBC 4.2 and later, you can deal directly with java.time types. 如果JDBC驱动程序符合JDBC 4.2及更高版本,则可以直接处理java.time类型。
myPreparedStatement.setObject( … , zdt ) ;
If your driver is not yet compliant, convert briefly to a java.sql type. 如果您的驱动程序尚不兼容,请简要转换为java.sql类型。
myPreparedStatement.setTimestamp( … , java.sql.Timestamp.from ( zdt.toInstant() ) ) ;
Use the DateFormat class in Java. 在Java中使用DateFormat类。 This should be helpful: http://docs.oracle.com/javase/6/docs/api/java/text/DateFormat.html
这应该是有用的: http : //docs.oracle.com/javase/6/docs/api/java/text/DateFormat.html
You can convert XMLGregorianCalendar to java.util.Date Object by using these two lines of code :- 您可以使用以下两行代码将XMLGregorianCalendar转换为java.util.Date对象: -
Date date = xmlDate.toGregorianCalendar().getTime();
System.out.println("java.util.date :- " + date);
To convert to java.slq.Date Object use this code :- 要转换为java.slq.Date对象,请使用以下代码: -
long time = xmlDate.toGregorianCalendar().getTime().getTime();
java.sql.Date sqlDate = new java.sql.Date(time);
Please use the function below - just pass the XMLGregorianCalendar instance and date format you want (format example: "DD MMMM yyyy" -> 01 January 2017) 请使用下面的函数 - 只需传递您想要的XMLGregorianCalendar实例和日期格式(格式示例:“DD MMMM yyyy” - > 2017年1月1日)
public String parseDate(String format,XMLGregorianCalendar XMLdate){
Date date = XMLdate.toGregorianCalendar().getTime();
DateFormat formatter = new SimpleDateFormat(format);
String formattedDate = formatter.format(date);
return formattedDate;
}
这将返回java.util.Date:
java.util.Date tempDate = issueDate.toGregorianCalendar().getTime();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.