时间戳最后带有.0

时间:2013-07-09 09:50:22

标签: java string datetime spring-mvc google-visualization

我正在尝试在AnnotatedTimeLine(Google Chart)中添加一些时间戳,以及它的req。采用Datetime格式。 当我重新格式化(到Timestamp格式)我从类中收到的字符串时,它给了我这个:

  

2013-06-28 10:08:35.0

我想要做的是删除最后的.0。我怎样才能做到这一点? 代码如下所示:

    public List<Survey> getAllSurvey(List<Status> statusList) {
    String sqlValues = "SELECT * FROM status WHERE idCategory = (SELECT idCategory FROM category WHERE name = '"
            + statusList.get(0).getCategoryName() + "');";
    List<Survey> survies = new ArrayList<Survey>();
    try {
        List<Map<String, Object>> rows = getJdbcTemplate().queryForList(
                sqlValues);
        for (Map row : rows) {
            Survey survey = new Survey();
            survey.setCategoryName(statusList.get(0).getCategoryName());
            survey.setValue((String) (row.get("value")));
            survey.setUnit((String) row.get("unit"));
            survey.setTimestamp(row.get("timeStamp") + "");
            survies.add(survey);
            System.out.println(survey.toString());
        }
    } catch (BadSqlGrammarException e) {
        e.printStackTrace();
    } catch (Exception e) {
        System.out.println(sqlValues);
        e.printStackTrace();
    }
    return survies;
}

提前致谢!

4 个答案:

答案 0 :(得分:2)

您必须将时间戳转换为您在课程中所需的日期格式。 Timestamp的默认字符串表示形式包括最后的纳秒数。如果你想改变它,你总是可以这样做:

    Timestamp t = Your timestamp;
    SimpleDateFormat df = new SimpleDateFormat("YYYY.MM.dd HH:mm:ss");
    String s = df.format(t);

答案 1 :(得分:1)

TL;DR

  1. 您似乎混淆了您应该尝试区分的两件事:时间戳的值及其格式。
  2. 您不应该希望使用 Timestamp 类。它的设计很差,而且已经过时了。而是使用 java.time 中的 OffsetDateTimeInstantLocalDatetime
  3. 同样在您的 Survey 类中,不要将时间戳保留为 String。仍然使用提到的 java.time 类之一。仅当需要给出字符串输出时,将其格式化为所需格式的字符串。为此,根据您对字符串的需求,可能比避免在末尾使用 .0 做得更好。

您的问题线

问题的核心在这里:

        survey.setTimestamp(row.get("timeStamp") + "");

您正在连接查询中的 TimestampString。为了做到这一点,Java 也通过调用其 Timestamp 方法将 String 转换为 toStringTimestamp.toString() 返回一个字符串,秒数至少有一位小数(如果有更多的非零小数,则可以更多,最多 9 位)。

解决方案:java.time

时间戳意味着(表示)一个时间点。用于某个时间点的最佳类是 java.time 中的 Instant。因此,在您的 Survey 类中将时间戳定义为 Instant

有一段时间我假设您被 Spring List<Map<String,Object>> 返回的 queryForList() 困住了,并且我们无法避免存在 java.sql.Timestamp 的时间戳。如果是这样,一旦获得 Timestamp 就将其转换为 Instant

        survey.setTimestamp(((Timestamp) row.get("timeStamp")).toInstant());

使用更现代的查询工具,您可以直接从数据库中获取 OffsetDateTimeInstantLocalDateTIme,并完全避免使用 Timestamp 类。

如何将您的 Instant(或其他 java.time 对象)格式化为字符串以呈现给用户或与另一个系统交换在很大程度上取决于上下文和要求。这里只是一个简单的例子:

    DateTimeFormatter formatter = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM);
    String formattedTimestamp = survey.getTimetamp()
            .atZone(ZoneId.systemDefault())
            .format(formatter);
    System.out.println(formattedTimestamp);

示例输出:

<块引用>

2020 年 9 月 13 日下午 2:26:40

示例代码的输出不仅取决于时区,还取决于语言环境。

链接

答案 2 :(得分:0)

您的代码中包含row.get("timeStamp") + ""行,它将有效地调用row.get("timeStamp").toString() + ""。假设该行包含java.sql.Timestamp对象,它将返回格式为yyyy-mm-dd hh:mm:ss.fffffffff的时间戳。

如果不需要,请创建一个SimpleDateFormat对象来表示所需的格式,然后调用SimpleDateFormat#format(row.get("timeStamp"))以正确格式化您的值。

SimpleDateFormat类描述了如何定义日期和时间模式。确保只创建一次SimpleDateFormat对象,因为创建它的成本相对较高。许多人将其定义为private static final变量,因此可以多次重复使用。

答案 3 :(得分:0)

尝试这一行解决方案

survey.setTimestamp(row.get("timeStamp").toString().replaceAll("\\.\\d+", ""));

如果性能至关重要,则此解决方案更好

String ts = row.get("timeStamp").toString();
ts = ts.substring(0, str.indexOf('.'));