【问题标题】:Format timestamp from database to Java将时间戳从数据库格式化为 Java
【发布时间】:2015-06-27 23:54:37
【问题描述】:

我的 Oracle SQL Developer 上有这种奇怪的日期和时间格式:

2015-4-14.1.39. 33. 870000000

我尝试将给定日期格式化为MM-dd-yyyy HH:mm:ss,但它给了我例外:

java.text.ParseException: Unparseable date: "2015-4-14.1.39. 33. 870000000"

以下是代码:

    SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy");

    try {
        String ts = "2015-4-14.1.39. 33. 870000000";
        Date date = formatter.parse(ts);
        String S = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss").format(date);

        System.out.println(S);
    } catch (Exception e) {
        e.printStackTrace();
    }

【问题讨论】:

  • formatter 是如何定义/配置的?
  • 请分享formatter的声明。
  • “MM/dd/yyyy”将不匹配“2015-4-14.1.39.33.870000000”

标签: java oracle date format timestamp


【解决方案1】:

问题是给定的日期字符串与指定的格式不匹配。尝试使用以下格式:

SimpleDateFormat df = new SimpleDateFormat("yyyy-M-dd.H.m. s. S");
String ts = "2015-4-14.1.39. 33. 870000000";
df.parse(ts);

在哪里

  • yyyy 一年
  • M 一年中的一个月
  • dd 表示月份中的某天
  • H 表示日期 (0-23) 小时
  • m 每小时分钟
  • s 以秒为单位
  • S 毫秒

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-26
    • 1970-01-01
    • 2012-02-17
    相关资源
    最近更新 更多