将数据库中的时间戳格式化为 Java

Format timestamp from database to Java

我的 Oracle 上有这种奇怪的日期和时间格式 SQL 开发人员:

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();
    }

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

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 为毫秒