Java Code Examples for java.sql.Timestamp#toString()

The following examples show how to use java.sql.Timestamp#toString() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DateUtils.java    From roncoo-pay with Apache License 2.0 6 votes vote down vote up
/**
 * 根据指定的格式转换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;
}
 
Example 2
Source File: DateUtils.java    From mumu with Apache License 2.0 6 votes vote down vote up
/**
 * 根据指定的格式转换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;
}
 
Example 3
Source File: MovieController.java    From movie-db-java-on-azure with MIT License 6 votes vote down vote up
/**
 * 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;
}
 
Example 4
Source File: ResultConvert.java    From Albianj2 with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
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();
        }
    }
}
 
Example 5
Source File: DateTimeUtils.java    From pulsar-flink with Apache License 2.0 5 votes vote down vote up
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;
    }
}
 
Example 6
Source File: TimestampUtil.java    From dkpro-jwpl with Apache License 2.0 5 votes vote down vote up
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();
}
 
Example 7
Source File: DateHelper.java    From MissZzzReader with Apache License 2.0 5 votes vote down vote up
/**
 * @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();
}
 
Example 8
Source File: DateHelper.java    From MissZzzReader with Apache License 2.0 5 votes vote down vote up
/**
 *
 *
 * @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 "";
    }
}
 
Example 9
Source File: PGCopyPreparedStatement.java    From phoebus with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public void setTimestamp(int parameterIndex, Timestamp x)
        throws SQLException {
    if (x == null) {
        rowValues[columnOrderMapping[parameterIndex]] = null;
    } else {
        rowValues[columnOrderMapping[parameterIndex]] = x.toString();
    }
}
 
Example 10
Source File: DateTimeFormatUtilsTest.java    From pinpoint with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example 11
Source File: LocalizedResource.java    From spliceengine with GNU Affero General Public License v3.0 5 votes vote down vote up
public String getTimestampAsString(Timestamp t){
	if (!enableLocalized){
		return t.toString();
	}
	return formatTimestamp.format
		(t, new StringBuffer(),
		 new java.text.FieldPosition(0)).toString();
}
 
Example 12
Source File: LocalizedResource.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
public String getTimestampAsString(Timestamp t){
	if (!enableLocalized){
		return t.toString();
	}
	return formatTimestamp.format
		(t, new StringBuffer(),
		 new java.text.FieldPosition(0)).toString();
}
 
Example 13
Source File: Utils.java    From BotLibre with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * 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();
}
 
Example 14
Source File: CurrentTime.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example 15
Source File: CurrentTime.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example 16
Source File: DateUtils.java    From DoingDaily with Apache License 2.0 4 votes vote down vote up
public static String getTimeStampString() {
    Date date = new Date();
    Timestamp timestamp = new Timestamp(date.getTime());
    return timestamp.toString();
}
 
Example 17
Source File: SQLChar.java    From gemfirexd-oss with Apache License 2.0 4 votes vote down vote up
/** @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);
}
 
Example 18
Source File: TesterObject.java    From spliceengine with GNU Affero General Public License v3.0 4 votes vote down vote up
public String getTimestamp() {
	Timestamp ts = new Timestamp(System.currentTimeMillis());
	return ts.toString();
}
 
Example 19
Source File: SQLChar.java    From gemfirexd-oss with Apache License 2.0 4 votes vote down vote up
/** @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);
}
 
Example 20
Source File: ValueMetaBaseTest.java    From hop with Apache License 2.0 4 votes vote down vote up
@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 ) );
}