如何在Java中格式化Oracle日期和时间戳类型值? [英] How to format Oracle date and timestamp type values in Java?

查看:265
本文介绍了如何在Java中格式化Oracle日期和时间戳类型值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在oracle db中有日期和时间戳类型字段,我需要检索这些值并将它们映射到我的对象字段.虽然我格式化值,但没有得到预期的结果.这是我的代码段.

I have date and timestamp type fields in oracle db, I need to retrieve these values and map them to my object field. Though I format the values I do not get the expected result. Here is the my code snippet.

import java.util.Date;
public class Operation{
private Date created;
private Date valueDate;

public Date getValueDate() {
    return this.valueDate;
}
public void setValueDate(Date valueDate) {
this.valueDate = valueDate;
}
public Date getCreated() {
    return this.valueDate;
}
public void setCreated(Date created) {
this.created= created;
}
}


//here starts code snippet to call db method

SimpleDateFormat df1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSSSSS");
SimpleDateFormat df2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

String formatCreated = df1.format(result.getTimestamp(22)); //Input from db: 23-FEB-18 06.17.42.302680 PM 
//OutputFormat 2018-02-23 18:17:42.000302  
String formatValueDate = df2.format(result.getTimestamp(23));//Input from db:23.02.2018 18:17:42 
//OutputFormat 2018-02-23 18:17:42
Operation op = new Operartion();
op.setCreated(df1.parse(formatCreated)) //Output Fri Feb 23 18:17:42 GMT+04:00 2018
op.setCreated(df1.parse(formatValuedate)) //Output Fri Feb 23 18:17:42 GMT+04:00 2018

任何帮助表示赞赏!

推荐答案

Stack Overflow已经对此进行了很多讨论.发布前先搜索.

This has been covered many times already on Stack Overflow. Search before posting.

那么简短……

使用现代的 java.time 类,而不要使用麻烦的旧式传统日期时间类,例如Date/Calendar.

Use modern java.time classes, rather than the troublesome old legacy date-time classes such as Date/Calendar.

从JDBC 4.2开始,直接与数据库交换 java.time 对象.

As of JDBC 4.2, exchange java.time objects directly with the database.

  • Instant用于TIMESTAMP WITH TIME ZONE
  • LocalDateTime用于TIMESTAMP WITHOUT TIME ZONE
  • LocalDate用于DATE
  • Use Instant for TIMESTAMP WITH TIME ZONE
  • Use LocalDateTime for TIMESTAMP WITHOUT TIME ZONE
  • Use LocalDate for DATE

呼叫PreparedStatement::setObjectResultSet::getObject.

myPreparedStatement.setObject( … , instant ) ;

然后……

Instant instant = myResultSet.getObject( … , Instant.class ) ;

请注意,根本没有使用任何字符串.

Note that no strings were used at all.

//来自数据库的输入:23-FEB-18 06.17.42.302680 PM

//Input from db: 23-FEB-18 06.17.42.302680 PM

不正确.您的假设是错误的.数据库使用其自己的内部定义的二进制格式来存储日期时间值,而不是字符串/文本.不要将日期时间值与其文本表示形式混淆.换句话说,日期时间值没有格式".

Incorrect. Your assumption is false. The database uses its own internally-defined binary format to store date-time values, not strings/text. Do not conflate date-time values with their textual representations. In other words, date-time values do not have a "format".

通过在java.time对象上调用toString生成标准ISO 8601格式的字符串.

Generate strings in standard ISO 8601 format by calling toString on the java.time objects.

String output = instant.toString() ;

对于其他格式,请使用DateTimeFormatter而不是SimpleDateFormat.已经在Stack Overflow上做了很好的介绍,因此请进行搜索.

For other formats, use DateTimeFormatter instead of SimpleDateFormat. Already covered well on Stack Overflow, so search.

java.time 框架内置于Java 8及更高版本中.这些类取代了麻烦的旧版日期时间类,例如 SimpleDateFormat .

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

Joda-Time 项目,现在位于<一个href ="https://en.wikipedia.org/wiki/Maintenance_mode" rel ="nofollow noreferrer">维护模式,建议迁移到要了解更多信息,请参见 Oracle教程 .并在Stack Overflow中搜索许多示例和说明.规范为 JSR 310 .

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

您可以直接与数据库交换 java.time 对象.使用符合 JDBC驱动程序 /jeps/170"rel =" nofollow noreferrer> JDBC 4.2 或更高版本.不需要字符串,不需要java.sql.*类.

You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.* classes.

在哪里获取java.time类?

Where to obtain the java.time classes?

  • Java SE 8 Java SE 9 和更高版本
    • 内置.
    • 具有捆绑的实现的标准Java API的一部分.
    • Java 9添加了一些次要功能和修复.
    • Java SE 8, Java SE 9, and later
      • Built-in.
      • Part of the standard Java API with a bundled implementation.
      • Java 9 adds some minor features and fixes.
      • Later versions of Android bundle implementations of the java.time classes.
      • For earlier Android, the ThreeTenABP project adapts ThreeTen-Backport (mentioned above). See How to use ThreeTenABP….

      > ThreeTen-Extra 项目扩展了java.time与其他班级.该项目为将来可能在java.time中添加内容提供了一个试验场.您可能会在这里找到一些有用的类,例如 Interval YearWeek YearQuarter 更多.

      这篇关于如何在Java中格式化Oracle日期和时间戳类型值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆