Search code examples
javastringdatetimespring-mvcgoogle-visualization

Timestamp comes with .0 at the end


I'm trying to put in some time stamps in a AnnotatedTimeLine (Google Chart), and its req. to be in the Datetime format. When I reformat (to Timestamp format) the string that I receive from the class, it gives me this:

2013-06-28 10:08:35.0

What I want to do is to remove the .0 at the end. How can I do this? The code looks like this:

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

Thanks in advance!


Solution

  • try this one line solution

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

    if performance is critical then this solution is better

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