
com.feilong.lib.xstream.converters.extended.ISO8601SqlTimestampConverter Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of feilong Show documentation
Show all versions of feilong Show documentation
feilong is a suite of core and expanded libraries that include utility classes, http, excel,cvs, io classes, and much much more.
/*
* Copyright (C) 2005 Joe Walnes.
* Copyright (C) 2006, 2007, 2017, 2018 XStream Committers.
* All rights reserved.
*
* The software in this package is published under the terms of the BSD
* style license a copy of which has been included with this distribution in
* the LICENSE.txt file.
*
* Created on 03. October 2005 by Joerg Schaible
*/
package com.feilong.lib.xstream.converters.extended;
import java.sql.Timestamp;
import java.util.Date;
/**
* A SqlTimestampConverter conforming to the ISO8601 standard.
* http://www.iso.ch/iso/en/CatalogueDetailPage.CatalogueDetail?CSNUMBER=26780
*
* @author Jörg Schaible
* @since 1.1.3
*/
public class ISO8601SqlTimestampConverter extends ISO8601DateConverter{
private final static String PADDING = "000000000";
@Override
public boolean canConvert(Class type){
return type == Timestamp.class && super.canConvert(Date.class);
}
@Override
public Object fromString(String str){
final int idxFraction = str.lastIndexOf('.');
int nanos = 0;
if (idxFraction > 0){
int idx;
for (idx = idxFraction + 1; Character.isDigit(str.charAt(idx)); ++idx){
;
}
nanos = Integer.parseInt(str.substring(idxFraction + 1, idx));
str = str.substring(0, idxFraction) + str.substring(idx);
}
final Date date = (Date) super.fromString(str);
final Timestamp timestamp = new Timestamp(date.getTime());
timestamp.setNanos(nanos);
return timestamp;
}
@Override
public String toString(Object obj){
final Timestamp timestamp = (Timestamp) obj;
String str = super.toString(new Date((timestamp.getTime() / 1000) * 1000));
final String nanos = String.valueOf(timestamp.getNanos());
final int idxFraction = str.lastIndexOf('.');
str = str.substring(0, idxFraction + 1) + PADDING.substring(nanos.length()) + nanos + str.substring(idxFraction + 4);
return str;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy