下面列出了java.sql.Timestamp#toString ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* 根据指定的格式转换java.sql.Timestamp到String
*
* @param dt
* java.sql.Timestamp instance
* @param sFmt
* Date 格式,DATE_FORMAT_DATEONLY/DATE_FORMAT_DATETIME/
* DATE_FORMAT_SESSION
* @return
* @since 1.0
* @history
*/
public static String toSqlTimestampString(Timestamp dt, String sFmt) {
String temp = null;
String out = null;
if (dt == null || sFmt == null) {
return null;
}
temp = dt.toString();
if (sFmt.equals(DateUtils.DATE_FORMAT_DATETIME) || // "YYYY/MM/DD
// HH24:MI:SS"
sFmt.equals(DateUtils.DATE_FORMAT_DATEONLY)) { // YYYY/MM/DD
temp = temp.substring(0, sFmt.length());
out = temp.replace('/', '-');
// }else if( sFmt.equals (DateUtils.DATE_FORMAT_SESSION ) ){
// //Session
// out =
// temp.substring(0,4)+temp.substring(5,7)+temp.substring(8,10);
// out += temp.substring(12,14) + temp.substring(15,17);
}
return out;
}
/**
* Upload image file to Azure blob and save its relative path to database.
*
* @param file image file
* @param id movie id
* @return updated movie detail page
*/
@RequestMapping(value = "/upload", method = RequestMethod.POST)
public String updateMovieImage(@RequestParam("file") MultipartFile file, @RequestParam("id") Long id) {
logger.debug(file.getOriginalFilename());
String newName = id + "." + FilenameUtils.getExtension(file.getOriginalFilename());
String imageUri = this.azureStorageUploader.uploadToAzureStorage(applicationContext, file, newName.toLowerCase());
if (imageUri != null) {
Timestamp timestamp = new Timestamp(Calendar.getInstance().getTime().getTime());
String timestampQuery = "?timestamp=" + timestamp.toString();
Movie movie = new Movie();
movie.setImageUri(imageUri.toLowerCase() + timestampQuery);
movieRepository.patchMovie(Long.toString(id), movie);
}
return "redirect:/movies/" + id;
}
public static String sqlValueToString(int sqlType, Object v) {
if (null == v) return "";
switch (sqlType) {
case Types.DATE: {
Date d = (Date) v;
return AlbianDateTime.getDateTimeString(d);
}
case Types.TIME: {
Time t = (Time) v;
return t.toString();
}
case Types.TIMESTAMP: {
Timestamp ts = (Timestamp) v;
return ts.toString();
}
default: {
return v.toString();
}
}
}
/**
* 根据指定的格式转换java.sql.Timestamp到String
*
* @param dt
* java.sql.Timestamp instance
* @param sFmt
* Date 格式,DATE_FORMAT_DATEONLY/DATE_FORMAT_DATETIME/
* DATE_FORMAT_SESSION
* @return
* @since 1.0
* @history
*/
public static String toSqlTimestampString(Timestamp dt, String sFmt) {
String temp = null;
String out = null;
if (dt == null || sFmt == null) {
return null;
}
temp = dt.toString();
if (sFmt.equals(DateUtils.DATE_FORMAT_DATETIME) || // "YYYY/MM/DD
// HH24:MI:SS"
sFmt.equals(DateUtils.DATE_FORMAT_DATEONLY)) { // YYYY/MM/DD
temp = temp.substring(0, sFmt.length());
out = temp.replace('/', '-');
// }else if( sFmt.equals (DateUtils.DATE_FORMAT_SESSION ) ){
// //Session
// out =
// temp.substring(0,4)+temp.substring(5,7)+temp.substring(8,10);
// out += temp.substring(12,14) + temp.substring(15,17);
}
return out;
}
public static String timestampToString(long us, TimeZone timeZone) {
Timestamp ts = toJavaTimestamp(us);
String timestampString = ts.toString();
DateFormat timestampFormat = getThreadLocalTimestampFormat(timeZone);
String formatted = timestampFormat.format(ts);
if (timestampString.length() > 19 && !timestampString.substring(19).equals(".0")) {
return formatted + timestampString.substring(19);
} else {
return formatted;
}
}
public static String toMediaWikiString(Timestamp timestamp){
String original = timestamp.toString();
StringBuffer result = new StringBuffer();
result.append(original.substring(0,4));//year
result.append(original.substring(5,7));//month
result.append(original.substring(8,10));//date
result.append(original.substring(11,13));//hour
result.append(original.substring(14,16));//minute
result.append(original.substring(17,19));//second
return result.toString();
}
/**
* @param
* @return
* ==>2012-05-02 09:10:46.436
*/
public static String getSeconds2(){
Date date1 = new Date();
Timestamp stamp2 = new Timestamp(date1.getTime());
return stamp2.toString();
}
/**
*
*
* @param str
* ==>yyyy-MM-dd HH:mm:ss
* @param
* @return
* ==>2009-12-10 05:12:02.0
*/
public static String changeStringToDate3(String str){
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date6 = null;
try {
date6 = sdf.parse(str);
java.sql.Date date7 = new java.sql.Date(date6.getTime());
Timestamp stamp9 = new Timestamp(date7.getTime());
return stamp9.toString();
} catch (ParseException e) {
e.printStackTrace();
return "";
}
}
@Override
public void setTimestamp(int parameterIndex, Timestamp x)
throws SQLException {
if (x == null) {
rowValues[columnOrderMapping[parameterIndex]] = null;
} else {
rowValues[columnOrderMapping[parameterIndex]] = x.toString();
}
}
@Test(expected = DateTimeParseException.class)
public void parseSimple_sqltimestamp_error() throws ParseException {
Timestamp timestamp = new Timestamp(System.currentTimeMillis());
// "2100-12-31 23:59:59.111"
String simpleDate = timestamp.toString();
SimpleDateFormat format = new SimpleDateFormat(DateTimeFormatUtils.SIMPLE_DATE_FORMAT);
long time = format.parse(simpleDate).getTime();
DateTimeFormatUtils.parseSimple(simpleDate);
}
public String getTimestampAsString(Timestamp t){
if (!enableLocalized){
return t.toString();
}
return formatTimestamp.format
(t, new StringBuffer(),
new java.text.FieldPosition(0)).toString();
}
/**
* Print the date in the form, "yyyy-MM-dd HH:mm:ss.N".
*/
public static String printTimestamp(Timestamp timestamp) {
if (timestamp == null) {
return "";
}
return timestamp.toString();
}
public static String getTime()
{
// Get the current time and convert to a String
long millis = System.currentTimeMillis();
Timestamp ts = new Timestamp(millis);
String s = ts.toString();
s = s.substring(0, s.lastIndexOf("."));
return s;
}
public static String getTime()
{
// Get the current time and convert to a String
long millis = System.currentTimeMillis();
Timestamp ts = new Timestamp(millis);
String s = ts.toString();
s = s.substring(0, s.lastIndexOf("."));
return s;
}
public String getTimestampAsString(Timestamp t){
if (!enableLocalized){
return t.toString();
}
return formatTimestamp.format
(t, new StringBuffer(),
new java.text.FieldPosition(0)).toString();
}
@Test
public void testGetDataXML() throws IOException {
BigDecimal bigDecimal = BigDecimal.ONE;
ValueMetaBase valueDoubleMetaBase =
new ValueMetaBase( String.valueOf( bigDecimal ), IValueMeta.TYPE_BIGNUMBER );
assertEquals(
"<value-data>" + Encode.forXml( String.valueOf( bigDecimal ) ) + "</value-data>" + SystemUtils.LINE_SEPARATOR,
valueDoubleMetaBase.getDataXml( bigDecimal ) );
boolean valueBoolean = Boolean.TRUE;
ValueMetaBase valueBooleanMetaBase =
new ValueMetaBase( String.valueOf( valueBoolean ), IValueMeta.TYPE_BOOLEAN );
assertEquals(
"<value-data>" + Encode.forXml( String.valueOf( valueBoolean ) ) + "</value-data>" + SystemUtils.LINE_SEPARATOR,
valueBooleanMetaBase.getDataXml( valueBoolean ) );
Date date = new Date( 0 );
ValueMetaBase dateMetaBase =
new ValueMetaBase( date.toString(), IValueMeta.TYPE_DATE );
SimpleDateFormat formaterData = new SimpleDateFormat( ValueMetaBase.DEFAULT_DATE_FORMAT_MASK );
assertEquals(
"<value-data>" + Encode.forXml( formaterData.format( date ) ) + "</value-data>" + SystemUtils.LINE_SEPARATOR,
dateMetaBase.getDataXml( date ) );
InetAddress inetAddress = InetAddress.getByName( "127.0.0.1" );
ValueMetaBase inetAddressMetaBase =
new ValueMetaBase( inetAddress.toString(), IValueMeta.TYPE_INET );
assertEquals( "<value-data>" + Encode.forXml( inetAddress.toString() ) + "</value-data>" + SystemUtils.LINE_SEPARATOR,
inetAddressMetaBase.getDataXml( inetAddress ) );
long value = Long.MAX_VALUE;
ValueMetaBase integerMetaBase = new ValueMetaBase( String.valueOf( value ), IValueMeta.TYPE_INTEGER );
assertEquals( "<value-data>" + Encode.forXml( String.valueOf( value ) ) + "</value-data>" + SystemUtils.LINE_SEPARATOR,
integerMetaBase.getDataXml( value ) );
String stringValue = "TEST_STRING";
ValueMetaBase valueMetaBase = new ValueMetaString( stringValue );
assertEquals( "<value-data>" + Encode.forXml( stringValue ) + "</value-data>" + SystemUtils.LINE_SEPARATOR,
valueMetaBase.getDataXml( stringValue ) );
Timestamp timestamp = new Timestamp( 0 );
ValueMetaBase valueMetaBaseTimeStamp = new ValueMetaTimestamp( timestamp.toString() );
SimpleDateFormat formater = new SimpleDateFormat( ValueMetaBase.DEFAULT_TIMESTAMP_FORMAT_MASK );
assertEquals(
"<value-data>" + Encode.forXml( formater.format( timestamp ) ) + "</value-data>" + SystemUtils.LINE_SEPARATOR,
valueMetaBaseTimeStamp.getDataXml( timestamp ) );
byte[] byteTestValues = { 0, 1, 2, 3 };
ValueMetaBase valueMetaBaseByteArray = new ValueMetaString( byteTestValues.toString() );
valueMetaBaseByteArray.setStorageType( IValueMeta.STORAGE_TYPE_BINARY_STRING );
assertEquals(
"<value-data><binary-string>" + Encode.forXml( XmlHandler.encodeBinaryData( byteTestValues ) )
+ "</binary-string>" + Const.CR + "</value-data>",
valueMetaBaseByteArray.getDataXml( byteTestValues ) );
}
/** @exception StandardException Thrown on error */
public void setValue(
Timestamp theValue,
Calendar cal)
throws StandardException
{
String strValue = null;
if( theValue != null)
{
if( cal == null)
strValue = theValue.toString();
else
{
cal.setTime( theValue);
StringBuilder sb = new StringBuilder();
formatJDBCDate( cal, sb);
sb.append( ' ');
formatJDBCTime( cal, sb);
int micros =
(theValue.getNanos() + SQLTimestamp.FRACTION_TO_NANO/2) /
SQLTimestamp.FRACTION_TO_NANO;
if( micros > 0)
{
sb.append( '.');
String microsStr = Integer.toString( micros);
if(microsStr.length() > SQLTimestamp.MAX_FRACTION_DIGITS)
{
sb.append(
microsStr.substring(
0, SQLTimestamp.MAX_FRACTION_DIGITS));
}
else
{
for(int i = microsStr.length();
i < SQLTimestamp.MAX_FRACTION_DIGITS ; i++)
{
sb.append( '0');
}
sb.append( microsStr);
}
}
strValue= sb.toString();
}
}
setValue( strValue);
}
public String getTimestamp() {
Timestamp ts = new Timestamp(System.currentTimeMillis());
return ts.toString();
}
/** @exception StandardException Thrown on error */
public void setValue(
Timestamp theValue,
Calendar cal)
throws StandardException
{
String strValue = null;
if( theValue != null)
{
if( cal == null)
strValue = theValue.toString();
else
{
cal.setTime( theValue);
StringBuilder sb = new StringBuilder();
formatJDBCDate( cal, sb);
sb.append( ' ');
formatJDBCTime( cal, sb);
int micros =
(theValue.getNanos() + SQLTimestamp.FRACTION_TO_NANO/2) /
SQLTimestamp.FRACTION_TO_NANO;
if( micros > 0)
{
sb.append( '.');
String microsStr = Integer.toString( micros);
if(microsStr.length() > SQLTimestamp.MAX_FRACTION_DIGITS)
{
sb.append(
microsStr.substring(
0, SQLTimestamp.MAX_FRACTION_DIGITS));
}
else
{
for(int i = microsStr.length();
i < SQLTimestamp.MAX_FRACTION_DIGITS ; i++)
{
sb.append( '0');
}
sb.append( microsStr);
}
}
strValue= sb.toString();
}
}
setValue( strValue);
}
public static String getTimeStampString() {
Date date = new Date();
Timestamp timestamp = new Timestamp(date.getTime());
return timestamp.toString();
}